当我第二次输入摇杆时,它不像第一次那样打印分数,不知道为什么它会跳过此操作

问题描述 投票:0回答:1

任务

编写一个允许用户播放个人游戏的程序

[二十一点。该程序应:

*询问玩家是要“击中”还是“坚持”。

*如果玩家击中,请在其手上加一张牌。

*如果玩家坚持,则结束游戏。

*不断询问玩家是否要“击中”或“粘住”,直到他们

说“棍子”。

*每次玩家击中时,计算玩家的分数

手和puts Score so far:和比分。

*例如Score so far: 23

*分数是通过将每个]的值相加得出的>

玩家手中的卡。

*每张卡的价值:

*“两个”:2

*“三”:3

* *四个:4

*“五个”:5

*“六个”:6

* *七个:7

*“八”:8

*“九”:9

*“十”:10

* *杰克:10

*“女王”:10

*“国王”:10

* * ace:11(这与真正的21点略有不同。]

*游戏结束时,puts游戏结果。

*如果玩家的得分是<= 21puts You scored:

最终分数

*例如You scored: 20

*如果玩家的分数是> 21puts You busted with:

最终分数。

*例如You busted with: 25

*作为解决方案的一部分,应该有四种特定方法:

* random_card:已为您编写。您不

需要更改。

* move:要求玩家移动。如果输入hit

stick,它返回移动。如果他们输入其他内容,则]

保持询问他们,直到他们输入hitstick

* score:拿一张纸牌并返回]的分数>

作为整数的手。

* run_game:使用其他方法来运行二十一点游戏。

*注意:运行自动化测试时,请确保从中删除

文件的顶层,对run_test或其他的任何调用>

方法

*注意:要在用户坚持时停止游戏,请不要使用exit to

退出程序,因为这将破坏自动测试。收件人

提早退出while循环,使用break关键字。

您不需要更改此方法!

CODE

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函数,区别在于每个循环仅调用一次move。

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
ruby methods blackjack
1个回答
0
投票

您的问题在这里:

© www.soinside.com 2019 - 2024. All rights reserved.