【问题标题】:when i input stick the second time it doesnt print score so far like the first time, not sure why it skips this当我第二次输入棒时,它没有像第一次那样打印分数,不知道为什么它会跳过这个
【发布时间】:2020-05-28 08:28:52
【问题描述】:

任务

编写一个程序,让用户玩单人游戏 二十一点。程序应该:

  • 询问玩家是想“击打”还是“坚持”。
    • 如果玩家击中,则将一张牌添加到他们的手上。
    • 如果玩家坚持,则结束游戏。
  • 不断询问玩家是想“击打”还是“坚持”,直到他们 说“坚持”。
  • 玩家每次击球,计算玩家的得分 手和puts Score so far: 和分数。
    • 例如Score so far: 23
    • 通过将每个 玩家手中的牌。
    • 每张卡的价值:
    • “二”:2
    • “三”:3
    • “四”:4
    • “五”:5
    • “六”:6
    • “七”:7
    • “八”:8
    • “九”:9
    • “十”:10
    • “杰克”:10
    • “女王”:10
    • “国王”:10
    • “ace”:11(这与真正的二十一点略有不同。)
  • 游戏结束后,puts 游戏的结果。

    • 如果玩家的分数是<= 21, puts You scored: 和 最终成绩
    • 例如You scored: 20
    • 如果玩家的分数是> 21, puts You busted with: 和 最终得分。
    • 例如You busted with: 25
  • 作为解决方案的一部分,应该有四种特定的方法:

    • random_card:这已经为你写好了。你不 需要改变它。
    • move: 要求玩家移动。如果他们输入hitstick,它返回移动。如果他们输入别的东西,它 一直问他们,直到他们输入hitstick
    • score:获取一组卡片并返回分数 手作为整数。
    • run_game:使用其他方法运行二十一点游戏。
  • 注意:运行自动化测试时,请确保从 文件的顶层对run_test 或其他的任何调用 方法。

  • 注意:要在用户卡住时停止游戏,请勿使用exit 退出程序,因为这会破坏自动化测试。到 提前退出while循环,使用break关键字。

你不需要改变这个方法!

代码

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

【问题讨论】:

  • 问题的标题旨在让读者了解问题的内容。你的不这样做。您的标题应该是问题陈述的一部分,但要改写以纠正两个问题。一是用你的代码澄清问题并陈述你的问题。另一个是使用正确的英语。句子以大写字母开头,第一人称代词“I”大写,“doesn't”包含撇号。 SO 不是聊天室。请编辑。

标签: ruby methods blackjack


【解决方案1】:

你的问题在这里:

if move == "stick"
   puts "You scored: #{score(hand)}"
   break

你又在调用你的 move 函数了。

试试这个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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 2017-06-21
    相关资源
    最近更新 更多