我决定做一个数字猜谜游戏。这是代码。
print "Guess a number from 1-20. You have 5 guesses!"
guess1=gets.chomp
guess1=guess1.to_i
random=1 + rand(20)
random=random.to_i
if guess1 == random
puts "Correct! You win!!!"
sleep(5)
Kernel.exit
elsif guess1 > random
puts "Wrong! Too high. Try again!"
else
puts "Wrong! Too low. Try again!"
end
guess2=gets.chomp
guess2=guess2.to_i
if guess2 == random
puts "Correct! You win!!!"
sleep(5)
Kernel.exit
elsif guess2 > random
puts "Wrong! Too high. Try again!"
else
puts "Wrong! Too low. Try again!"
end
guess3=gets.chomp
guess3=guess3.to_i
if guess3 == random
puts "Correct! You win!!!"
sleep(5)
Kernel.exit
elsif guess3 > random
puts "Wrong! Too high. Try again!"
else
puts "Wrong! Too low. Try again!"
end
guess4=gets.chomp
guess4=guess4.to_i
if guess4 == random
puts "Correct! You win!!!"
sleep(5)
Kernel.exit
elsif guess4 > random
puts "Wrong! Too high. Try again!"
else
puts "Wrong! Too low. Try again!"
end
guess5=gets.chomp
guess5=guess5.to_i
if guess5 == random
puts "Correct! You win!!!"
sleep(5)
Kernel.exit
elsif guess5 > random
puts "Wrong! Too high. Game over!"
sleep(5)
Kernel.exit
else
puts "Wrong! Too low. Game over!"
sleep(5)
Kernel.exit
end
如何在重启游戏时添加try-again选项?
答案 0 :(得分:2)
你想要做的是让游戏的主线程成为循环:
import com.facebook.CallbackManager;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
... doing some not-facebook-related stuff here
case 2:
... doing some other not-facebook-related stuff here
// case ??? :
// callbackManager.onActivityResult(requestCode, resultCode, data);
这将无休止地执行其中的任何内容,直到引发异常或程序中断。
现在,为了使其有效,您需要开始在对象中包装游戏逻辑。
例如
loop do
# Prompt
end
现在,当你启动游戏外观时,你只需要class GuessingGame
def initialize
@secret = 1 + rand(20)
@guesses_remaining = 5
end
def is_it?(number)
@guesses_remaining -+ 1
@secret == number
end
end
,在游戏结束后,通过猜测或完成正确,你可以提示重试并进行新的猜测游戏
答案 1 :(得分:2)
首先,将重复的部分提取到方法中:
def correctly_guessed?(target)
guess = gets.to_i
if guess == target then puts "Correct! You win!!!"; sleep(5)
elsif guess > target then puts "Wrong! Too high. Try again!"
else puts "Wrong! Too low. Try again!"
end
end
然后,重构您的代码:
loop do
print "Guess a number from 1-20. You have 5 guesses!"
target = 1 + rand(20)
5.times{break if correctly_guessed?(target)}
print "Restart the game? (Y to restart)"
break unless gets.chomp == "Y"
end
答案 2 :(得分:2)
正如塞尔吉奥所说,一个非常干净的解决方案是学习功能,并划分你的游戏逻辑,以便你可以在需要时调用这些功能。
另一种解决方案是使用loops,特别是do...while loop。
旁注:我发布的教程链接中的begin...while
循环风格不是Ruby的创建者Matz推荐的。他推荐loop do...break end
样式,这是我要演示的内容。
我会让你阅读这些教程,以了解更多有关最新进展的信息,但要点是这种特殊的循环风格将运行你的代码一次,它将循环返回并再次运行,或退出循环并且程序将结束,具体取决于我们的“控制”变量的结果。
不幸的是,由于您编写代码的方式,将程序包装在一个简单的do...while
中是非常尴尬的,因为Kernel.exit
行,以及您硬编码的事实5猜测是按顺序运行的。由于我的回答涉及循环,我会很快向您展示重构代码的好方法,而不会有太多痛苦。请注意代码中的注释,以了解正在发生的事情。
loop do # The start of the main game loop
random=1 + rand(20)
random=random.to_i
guess_count = 0 # Tracks the number of times the user has guessed
print "Guess a number from 1-20. You have 5 guesses!"
while guess_count < 5 # The guess loop; keep looping up to a max of 5 times
guess=gets.chomp
guess=guess.to_i
if guess == random
puts "Correct! You win!!!"
sleep(5) # 5 seconds is a very long time, I would reduce this to 1 at most
break # This causes the 'guess loop' to end early
elsif guess > random
puts "Wrong! Too high. Try again!"
else
puts "Wrong! Too low. Try again!"
end
guess_count+= 1 # increment the guess count if the user guessed incorrectly
end
puts "Would you like to play again? (y/n)"
play_again = gets.chomp
break if play_again != "y" # exit the main loop if the user typed anything except "y"
end # The end of the main loop, and thus the entire program
# No need for Kernal.exit. The program is done at this point.
注意:我删除了您的评论,因此不会干扰说明。请随意将它们放回您的版本中。
查看教程中的循环以获取更多详细信息。希望这很清楚。