ruby 这是Ruby中的Guess游戏,只是为了好玩,并将Head First Ruby中的Chap1作为参考:)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 这是Ruby中的Guess游戏,只是为了好玩,并将Head First Ruby中的Chap1作为参考:)相关的知识,希望对你有一定的参考价值。
class GuessGame
GUESS = 10
def initialize(player_name)
@player_name = player_name
@number = generate_number
@attempts = 1
play
end
def try_to_guess
puts "This is your attempt number #{@attempts}"
puts "Please enter number:"
@guess_number = gets.chomp.to_i
@attempts += 1
end
def check_guess?
@guess_number == @number
end
def check_if_less_or_greater
@guess_number < @number ? puts("Ooops is less than") : puts("Is bigger than")
end
def you_win
puts "Congrats! #{@player_name} you won!!!"
@attempts = 11
end
def exceded_attempts?
@attempts >= GUESS
end
def generate_number
rand(100)
end
def play
begin
try_to_guess
check_guess? ? you_win : check_if_less_or_greater
end while not exceded_attempts?
end
end
以上是关于ruby 这是Ruby中的Guess游戏,只是为了好玩,并将Head First Ruby中的Chap1作为参考:)的主要内容,如果未能解决你的问题,请参考以下文章