chapter 9
Closed this issue · 2 comments
AntonioDaria commented
HI all i m having some trouble concluding the run_game method as well. it asks for the card and stops when busted. the trouble is it doesn't recognise the "stick", it will go on and on until busted it doesn't matter if the input is stick or hit:
def random_card
cards = ["two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten",
"jack", "queen", "king", "ace"]
cards[rand(13)]
end
def move
puts " hit or stick"
user_move = ''
until user_move == "hit" || user_move == "stick"
user_move = gets.chomp
end
user_move
end
def score(list_of_cards)
cards_to_number = {
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
ten: 10,
jack: 10,
queen: 10,
king: 10,
ace: 11
}
sum = 0
list_of_cards.each { |card| sum += cards_to_number[card.to_sym] }
sum
end
def run_game
list_of_cards = []
input = move
while true do
if input == "hit"
list_of_cards.push(random_card)
puts "Score so far: #{score(list_of_cards)}"
move
elsif input == "stick"
puts "You scored: #{score(list_of_cards)}"
break
end
if score(list_of_cards) > 20
puts "You busted with: #{score(list_of_cards)}"
break
end
end
end
m-rcd commented
Hi Antonio, the problem is you are only asking the input once so it is always gonna be the first one asked for. You should put your move inside the while loop.
Let me know if it works :)
AntonioDaria commented
i sorted it : i have saved the move into input within the loop and it worked :)
def run_game
list_of_cards = []
input = move
while true do
if input == "hit"
list_of_cards.push(random_card)
puts "Score so far: #{score(list_of_cards)}"
input = move
elsif input == "stick"
puts "You scored: #{score(list_of_cards)}"
break
end
if score(list_of_cards) > 20
puts "You busted with: #{score(list_of_cards)}"
break
end
end
end