我试图建立一个秘密数字游戏,其中:红宝石 - 循环不打破
- 1之间的数字 - 10随机挑选
- 球员已经3次尝试猜测秘密数字
- 如果玩家在所有尝试中猜测错误,或猜测出正确的数字,他们会被提示是否要重玩游戏。
我的问题是:
- 的 “企图” 循环不会打破时playerInput == secret_number。它只会在所有3次尝试都被使用时破坏。
- 当所有3个猜测用完时,无法打印消息“对不起,但你没有更多的尝试”。
- 我觉得我的数学是关闭太...与尝试
的代码如下。在此先感谢你们!
puts "Welcome to the Secret Number Game! Please tell me your name"
player_name = gets.strip
puts "Welcome #{player_name}!"
puts "Guess a number between 1 - 10. You only have 3 attempts!"
restart = true
def guess_check(playerInput, secret_number, attempts)
if playerInput > secret_number
puts "Too high! try again!"
elsif playerInput < secret_number
puts "Too low! try again!"
elsif
playerInput == secret_number
puts "Congratulations, you guessed the secret number! [#{secret_number}]"
elsif
attempts == 0
puts "Sorry, you're out of guesses, the secret number is [#{secret_number}]"
else
puts secret_number
end
end
while restart
guesses = []
attempts = 3
secret_number = 1 + rand(10)
while attempts
attempts = attempts - 1
puts "Guess the secret number, you have #{attempts} tries left"
playerInput = gets.to_i
guesses.push(playerInput)
guess_check(playerInput, secret_number, attempts)
puts "You've guessed #{guesses}"
break if playerInput == secret_number || break if attempts == 0
end
puts "Do you want to play again? (y/n)"
answer = gets.strip
restart = false if answer == "n"
end
你们真棒。非常感谢您的参与。不能相信错误是额外的“打破”。并感谢您将其分解。即时从现在开始,我的条件从最不可能的最有可能 – giantqtipz