Ruby on rails 如何用Ruby编写石头、布、剪刀游戏
我试图添加两个特性:第一个是当玩家按下x时,它显示分数,第二个是当玩家按下q退出游戏时 我想将Ruby on rails 如何用Ruby编写石头、布、剪刀游戏,ruby-on-rails,ruby,sinatra,Ruby On Rails,Ruby,Sinatra,我试图添加两个特性:第一个是当玩家按下x时,它显示分数,第二个是当玩家按下q退出游戏时 我想将x=>':SCORE'添加到条目\u to \u SYM散列中,但当玩家按x时,我不希望计算机选择某些内容 ENTRY_TO_SYM = { 'p'=>:PAPER, 'r'=>:ROCK, 's'=>:SCISSORS, 'x' => :SCORE , 'q' => :QUIT} VALID_ENTRIES = ENTRY_TO_SYM.keys COMPUTER_CHO
x=>':SCORE'
添加到条目\u to \u 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)
ENTRY\u TO\u SYM={'p'=>:纸,'r'=>:石头,'s'=>:剪刀,'x'=>:得分,'q'=>:退出}
有效的\u条目=条目\u到\u SYM.keys
计算机选项=输入符号值
#从玩家的角度来看,赢家和输家,各有其价值
#配对是玩家的选择,第二个是电脑的选择。
胜利者=[[剪刀,布,布,石头,石头,剪刀]]
失败者=胜利者
类剪刀
def初始化
@玩家得分=@computer得分=@ties=0
结束
def播放(赢取分数)
而@player\u score
我会这样做来计算结果:
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
措辞并不完美,这是未经测试的,需要结果部分,所以请修改它,直到它符合您的要求为止;) 考虑编写
WINNERS={'s'=>'p',p'=>'r',r'=>'s'}
。然后,如果变量player
和computer
分别保持'r'
、'p'
或's'
,如果WINNERS[player]==computer[player]==true
,玩家获胜,计算机获胜;否则他们又扔了。你知道这是如何简化你的问题的吗?你不需要符号:石头
,:纸
和:剪刀
。我很困惑,你是在和?你的问题和这两个有什么关系?