Skip to content

Instantly share code, notes, and snippets.

@JohanneA
Created September 20, 2019 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohanneA/686fa221cf5ab7b57aef0c294255a17a to your computer and use it in GitHub Desktop.
Save JohanneA/686fa221cf5ab7b57aef0c294255a17a to your computer and use it in GitHub Desktop.
A number guessing game in ruby, built during the Girl Code Amsterdam Meetup on the 18. September 2019
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