Last active
August 29, 2015 14:12
-
-
Save igmarin/1be80fb7221613aad0c9 to your computer and use it in GitHub Desktop.
This a Guess Game in Ruby just for fun and taking the Chap1 in Head First Ruby as a reference :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cierto, no se por que puse tantos puts "fuchi" me encantan los comentarios mil gracias