红宝石二十一点和循环
问题描述:
我正在建造一个命令行红宝石二十一点游戏使用的方法。我已经到了玩家可以击中或坚持的地步(在被交易2张牌之后)。现在我似乎无法跳出逻辑思考如何限制我的球员只有四个命中。 make红宝石二十一点和循环
这告诉我,我的问题在循环 - 那就是我正以错误的方式接近程序的循环部分。
这是到目前为止我的代码:
def blackjack
promt
end
def promt
puts "Welcome! Would you like to play a game of blackjack? Enter Yes or No"
play = gets.chomp.downcase
if play == "yes"
game_plan
elsif play =="no"
puts "That's too bad. Come back when you feel like playing"
else
puts "Sorry but I don't understand your respones. Please type and enter yes to play Or no to to quit"
blackjack
end
end
def game_plan
wants_to_play = true
hand = []
total = first_move(hand)
wants_to_play = hit_me(hand)
if wants_to_play == true
hit_me(hand)
end
end
def first_move(hand)
deal(hand)
deal(hand)
total(hand)
end
def deal(hand)
card = rand(12)
puts "You have been dealt a card with a value of #{card}"
hand << card
end
def total(hand)
total = 0
hand.each do |count|
total += count
end
puts "The sum of the cards you have been dealt is #{total}"
total
end
def hit_me(hand)
puts "Would you like to hit or stick?"
yay_or_nah = gets.chomp.downcase
if yay_or_nah == "stick" && total(hand) < 21
puts "Sorry! The sum of the cards you have been dealt is less than 21. You lost this round!"
else
deal(hand)
total(hand)
playing = true
end
end
blackjack
我想要做的是限制我的球员命中2(初始先打,其中涉及2张卡后)。我知道这是一个令人讨厌的新手问题,但我真的很感谢任何能够帮助我以正确的方式来思考解决方案的反馈。
PS:虽然我理解循环的工作方式,但我知道如何以及何时实施它们......所以任何反馈都将非常感激。谢谢!
答
你在找那种东西吗?
MAX_HITS = 2
hits = 0
loop do
break if hits > MAX_HITS
puts "Would you like to hit or stick?"
…
else
hits += 1
…
end
end