【问题标题】:How to write a rock, paper, scissors game in Ruby如何用 Ruby 编写石头、纸、剪刀游戏
【发布时间】:2020-06-14 09:18:50
【问题描述】:

我正在尝试添加两个功能:第一个是当玩家按下 x 时,它会显示分数,第二个是当玩家按下 q 时退出游戏。

我想将x => ':SCORE' 添加到ENTRY_TO_SYM 哈希中,但是当玩家按下x 时,我不希望计算机选择某些内容。

ENTRY_TO_SYM = { 'p'=>:PAPER, 'r'=>:ROCK, 's'=>:SCISSORS, 'x' => :SCORE , 'q' => :QUIT}
VALID_ENTRIES = ENTRY_TO_SYM.keys
COMPUTER_CHOICES = ENTRY_TO_SYM.values
# WINNERS and LOSERS from the player's perspective, the first value of each
# pair being the player's choice, the second, the computer's choice.
WINNERS = [[:SCISSORS, :PAPER], [:PAPER, :ROCK], [:ROCK, :SCISSORS]]
LOSERS =  WINNERS.map { |i,j| [j,i] }

class RockPaperScissors
  def initialize
    @player_score = @computer_score = @ties = 0
  end

  def play(winning_score)
    while @player_score < winning_score && @computer_score < winning_score
      puts "Player score: #{@player_score}, " +
           "Computer score: #{@computer_score}, Ties: #{@ties}"

      player = player_choice 
      computer = COMPUTER_CHOICES.sample
      puts "\nPlayer chooses #{player.to_s}"
      puts "Computer chooses #{computer.to_s}"

      case player_outcome [player, computer]
      when :WIN
        puts "#{player.to_s} beats #{computer.to_s}, player wins the round"
        @player_score += 1
      when :LOSE
        puts "#{computer.to_s} beats #{player.to_s}, computer wins the round"
        @computer_score += 1
      else
        puts "Tie, choose again"
        @ties += 1
      end
    end
    puts "\nFinal score: player: #{@player_score}, " +
         "computer: #{@computer_score} (ties: #{@ties})"
    puts (@player_score == 2) ? "Player wins!" : "Yea! Computer wins!"
  end


  private

  def player_choice
    loop do
      print "Choose rock (r), paper (p) or scissors (s) || OPTION choose (x) if you want to display the score and (q) to quit the game : "
      choice = gets.chomp.downcase
      return ENTRY_TO_SYM[choice] if ENTRY_TO_SYM.key?(choice)
      puts "That entry is invalid. Please re-enter"
    end
  end

  def player_outcome(plays)
    return :WIN  if WINNERS.include?(plays)
    return :LOSE if LOSERS.include?(plays)
    :TIE
  end
end


RockPaperScissors.new.play(3)

【问题讨论】:

  • 考虑写WINNERS = { 's'=&gt;'p', 'p'=&gt;'r', 'r'=&gt;'s' }。那么如果变量playercomputer各有'r''p''s',则如果WINNERS[player] == computer #=&gt; true,则玩家获胜,如果WINNERS[computer] == player #=&gt; true,则计算机获胜;否则他们会再次抛出。你看到这如何简化你的问题吗?您不需要符号 :ROCK:PAPER:SCISSORS
  • 我很困惑,您是否将ruby-on-railssinatra 结合使用?您的问题与这两者有何关系?

标签: ruby-on-rails ruby sinatra


【解决方案1】:

我会这样做来计算结果:

  values = {
  'Paper' => {
               'Paper'   => 'Ties'
               'Rock'    => 'Wins'
               'Scissors'=> 'Loses'
             }
  'Rock' =>  {
               'Paper'   => 'Loses'
               'Rock'    => 'Ties'
               'Scissors'=> 'Wins'
             }
  'Scissors' => {
                 'Paper'   => 'Wins'
                 'Rock'    => 'Loses'
                 'Scissors'=> 'Ties'
                }
  }
  ENTRY_TO_SYM = { 'p'=>'Paper', 'r'=>'Rock', 's'=>'Scissors', 'x' => :SCORE , 'q' => :QUIT}

  def player_choice
     loop do
       print "Choose rock (r), paper (p) or scissors (s) || OPTION choose (x) 
       if you want to display the score and (q) to quit the game : "
       choice = gets.chomp.downcase
       return ENTRY_TO_SYM[choice] if ENTRY_TO_SYM.key?(choice)
       puts "That entry is invalid. Please re-enter"
     end
  end

措辞并不完美,而且未经测试,需要对结果部分进行调整,直到它适用于您想要的;)

【讨论】:

    猜你喜欢
    • 2015-05-16
    • 2013-12-09
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2023-03-21
    • 2020-05-22
    相关资源
    最近更新 更多