Ruby 红宝石21点和循环
我正在使用方法构建一个命令行ruby blackjack游戏。我已经到了玩家可以击球或持球的地步(在发了两张牌之后)。现在,我似乎无法从逻辑上思考如何将我的球员限制在四支安打以内。制造 这告诉我我的问题是循环-也就是说,我以错误的方式接近程序的循环部分 以下是我目前的代码:Ruby 红宝石21点和循环,ruby,loops,methods,logic,blackjack,Ruby,Loops,Methods,Logic,Blackjack,我正在使用方法构建一个命令行ruby blackjack游戏。我已经到了玩家可以击球或持球的地步(在发了两张牌之后)。现在,我似乎无法从逻辑上思考如何将我的球员限制在四支安打以内。制造 这告诉我我的问题是循环-也就是说,我以错误的方式接近程序的循环部分 以下是我目前的代码: def blackjack promt end def promt puts "Welcome! Would you like to play a game of blackjack? Enter Yes or N
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
def二十一点
提示
结束
def promt
输入“欢迎!您想玩21点游戏吗?输入是或否”
play=get.chomp.downcase
如果播放==“是”
游戏计划
elsif播放==“否”
写着“太糟糕了,想玩的时候再来”
其他的
输入“抱歉,我不明白您的回答。请键入并输入“是”以播放或“否”以退出”
21点
结束
结束
def游戏计划
想要玩=真
手=[]
总计=第一次移动(手)
想要玩=打我(手)
如果想要玩==真
打我(手)
结束
结束
def首次移动(手动)
成交(手)
成交(手)
总数(手)
结束
def交易(手)
卡=兰特(12)
放入“您已收到一张价值为#{card}的卡”
你在找这样的东西吗
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