A number guessing game in ruby, built during the Girl Code Amsterdam Meetup on the 18. September 2019
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
def play_game(start_range, end_range) | |
computer_guess = rand(start_range..end_range) | |
user_guesses = [] | |
puts "Guess a number" | |
user_guess = gets.chomp.to_i | |
while user_guess != computer_guess | |
if user_guesses.include?(user_guess) | |
puts "You've already tried this number, try again" | |
else | |
puts "Wrong! Guess again" | |
user_guesses << user_guess # Add user guess to the array of previous guesses | |
end | |
user_guess = gets.chomp.to_i | |
end | |
puts "Congrats, you guessed it" | |
end | |
def run() | |
puts "Welcome to the number guessing game!" | |
sleep(2) | |
puts "Do you want to play a game? [y/n]" | |
start_game = gets.chomp.downcase # Ignore the case by always using lower case | |
while(start_game == "y") # Allow the user to keep playing if they finish a game | |
puts "What range do you want to guess between?" | |
start_range = gets.chomp.to_i | |
puts "and" | |
end_range = gets.chomp.to_i | |
play_game(start_range, end_range) | |
puts "Do you want to play another game? [y/n]" | |
start_game = gets.chomp | |
end | |
puts "Boo, stopping game" | |
end | |
run() # Start the game |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment