当我第二次输入 stick 时,它不像第一次那样打印分数,不知道为什么它会跳过这个
when i input stick the second time it doesnt print score so far like the first time, not sure why it skips this
任务
Write a program that lets a user player a solo game of
Blackjack. The program should:
- Ask the player if they want to "hit" or "stick".
- If the player hits, add a card to their hand.
- If the player sticks, end the game.
- Keep asking the player if they want to "hit" or "stick" until they
say "stick".
- Each time the player hits, calculate the score for the player's
hand and
puts
Score so far:
and the score.
- e.g.
Score so far: 23
- A score is calculated by adding up the values of each of the
cards in the player's hand.
- Value for each card:
- "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 (This is slightly different from real Blackjack.)
When the game is over, puts
the outcome of the game.
- If the player's score is
<= 21
, puts
You scored:
and the
final score
- e.g.
You scored: 20
- If the player's score is
> 21
, puts
You busted with:
and
the final score.
- e.g.
You busted with: 25
As part of your solution, there should be four specific methods:
random_card
: This has already been written for you. You don't
need to change it.
move
: Asks the player for a move. If they enter hit
or
stick
, it returns the move. If they enter something else, it
keeps asking them until they enter hit
or stick
.
score
: Takes an array of cards and returns the score for the
hand as an integer.
run_game
: uses the other methods to run a game of Blackjack.
Note: When you run the automated tests, make sure to remove from
the top level of the file any calls to run_test
or other
methods.
Note: To stop the game when the user sticks, don't use exit
to
quit the program because this will break the automated tests. To
exit a while loop early, use the break
keyword.
You don't need to change this method!
代码
def random_card
cards = ["two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten",
"jack", "queen", "king", "ace"]
cards[rand(13)]
end
random_card
def move
puts "hit or stick?"
turn = gets.chomp
if turn == "hit"
"hit"
elsif turn == "stick"
"stick"
else
move
end
end
def score(array)
values = {
"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
}
total = 0
array.each do |card|
total += values[card]
end
total
end
def run_game
hand = []
while move == "hit"
hand.push(random_card)
if score(hand) <= 21
puts "Score so far: #{score(hand)}"
else
puts "you busted with #{score(hand)}"
break
end
if move == "stick"
puts "You scored: #{score(hand)}"
break
end
end
end
run_game
您的问题在这里:
if move == "stick"
puts "You scored: #{score(hand)}"
break
您又在调用移动函数了。
试试这个 run_game 函数,不同之处在于每个循环只调用一次移动。
def run_game
hand = []
currMove = move
while currMove == "hit"
hand.push(random_card)
if score(hand) <= 21
puts "Score so far: #{score(hand)}"
else
puts "you busted with #{score(hand)}"
break
end
if currMove == "stick"
puts "You scored: #{score(hand)}"
break
end
currMove = move
end
end
任务
Write a program that lets a user player a solo game of Blackjack. The program should:
- Ask the player if they want to "hit" or "stick".
- If the player hits, add a card to their hand.
- If the player sticks, end the game.
- Keep asking the player if they want to "hit" or "stick" until they say "stick".
- Each time the player hits, calculate the score for the player's hand and
puts
Score so far:
and the score.
- e.g.
Score so far: 23
- A score is calculated by adding up the values of each of the cards in the player's hand.
- Value for each card:
- "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 (This is slightly different from real Blackjack.)
When the game is over,
puts
the outcome of the game.
- If the player's score is
<= 21
,puts
You scored:
and the final score- e.g.
You scored: 20
- If the player's score is
> 21
,puts
You busted with:
and the final score.- e.g.
You busted with: 25
As part of your solution, there should be four specific methods:
random_card
: This has already been written for you. You don't need to change it.move
: Asks the player for a move. If they enterhit
orstick
, it returns the move. If they enter something else, it keeps asking them until they enterhit
orstick
.score
: Takes an array of cards and returns the score for the hand as an integer.run_game
: uses the other methods to run a game of Blackjack.Note: When you run the automated tests, make sure to remove from the top level of the file any calls to
run_test
or other methods.Note: To stop the game when the user sticks, don't use
exit
to quit the program because this will break the automated tests. To exit a while loop early, use thebreak
keyword.You don't need to change this method!
代码
def random_card
cards = ["two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten",
"jack", "queen", "king", "ace"]
cards[rand(13)]
end
random_card
def move
puts "hit or stick?"
turn = gets.chomp
if turn == "hit"
"hit"
elsif turn == "stick"
"stick"
else
move
end
end
def score(array)
values = {
"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
}
total = 0
array.each do |card|
total += values[card]
end
total
end
def run_game
hand = []
while move == "hit"
hand.push(random_card)
if score(hand) <= 21
puts "Score so far: #{score(hand)}"
else
puts "you busted with #{score(hand)}"
break
end
if move == "stick"
puts "You scored: #{score(hand)}"
break
end
end
end
run_game
您的问题在这里:
if move == "stick"
puts "You scored: #{score(hand)}"
break
您又在调用移动函数了。
试试这个 run_game 函数,不同之处在于每个循环只调用一次移动。
def run_game
hand = []
currMove = move
while currMove == "hit"
hand.push(random_card)
if score(hand) <= 21
puts "Score so far: #{score(hand)}"
else
puts "you busted with #{score(hand)}"
break
end
if currMove == "stick"
puts "You scored: #{score(hand)}"
break
end
currMove = move
end
end