Created
June 2, 2012 23:43
-
-
Save YuheiNakasaka/2860523 to your computer and use it in GitHub Desktop.
blackjack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
#ブラックジャックゲーム | |
#手札の数字を21に近づけた方の勝ち。 | |
#カードがはじめに1枚配られる。そのあと2回までカードを引くことができる。 | |
#パスあり。パスする時はpassと入力する。 | |
mycard = [] | |
cmpcard = [] | |
#手札を配る | |
card = (1..13).to_a | |
mycard << card[rand(card.length)] | |
puts "あなたの最初のカードは#{mycard[0]}です。" | |
#フラグを基準にプレイヤーの手札を決定していく | |
flag = 2 | |
while flag | |
puts "カードを引きますか?(yes/pass)" | |
say = gets.chomp | |
if say == "yes" | |
mycard << card[rand(card.length)] | |
puts "あなたの手札は#{mycard.join(', ')}です" | |
flag -=1 | |
if flag == 0 | |
break | |
end | |
elsif say == "pass" and flag != 0 | |
flag -= 1 | |
break | |
end | |
end | |
3.times do | |
cmpcard << card[rand(card.length)] | |
end | |
myans = mycard.inject{|a,b| a+b} | |
cmpans = cmpcard.inject{|a,b| a+b} | |
result = myans >= cmpans ? myans==cmpans ? "#{myans}対#{cmpans}で引き分けです" : "#{myans}対#{cmpans}であなたの勝ちです" : "#{myans}対#{cmpans}でコンピューターの勝ちです" | |
puts result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment