Well it has been awhile since I made an entry so I thought I would make one while also answering someone’s question. The user named “Cunningham” asked about an example high-low number guessing game to get started with learning Ruby. The game is essentially guessing the random number generated by the computer to see how quick you can guess it. The computer picks a random number in a range of numbers and the user then tries to guess that number. The more turns it takes you, the more you are a loser I guess. But we will cover a simple example of this game in Ruby right here on the Programming Underground!
So as I explained to Cunningham, there are some basic parts essential to the game. We need to be able to ask the user for input, generate a random number hidden from the user, compare the numbers to know if the user is too high, too low or if they guessed the number and do this as many times as necessary until they either quit the game or guess the number and say “no” to playing again.
The example below is really basic and of course doesn’t go into the type of validation that it should to make it a real bulletproof example. The one major upgrade would be to evaluate the user’s input to make sure it is a number. Either way, to_i is going to return an integer of some sort.
puts "Welcome to a game of high-low." # A simple flag variable to know if we want to play again. # It is started with "y" so that the game immediately goes into the first iteration. playagain = "y" # We keep playing while they say "y" while playagain == "y" # Our win flag, to know if they won yet win = false # Generate random number between 1 - 100 randNum = rand(100) + 1 puts "Please enter a number (1-100) or -1 to exit: " # While we haven't won yet, keep prompting for numbers and comparing. while !win # Get number from user guessedNumber = gets.to_i # Wants to quit if guessedNumber == -1 then break end # Lets compare our number to computer number if guessedNumber > randNum then puts "Too high, try again." elsif guessedNumber < randNum then puts "Too low, try again." else puts "You win!" win = true end end # If we reached here, the game has ended, ask the user if they want to play again. puts "Want to play again? (y/n)" playagain = gets.chomp! end puts "Thanks for playing!"
The code comments really say it all in this short example. We start the game by first setting up a flag variable to let us know if the user wants to play another game. It is defaulted to “y” so that when we first start it will enter the loop and begin a game. Once in the loop we start another flag, but this one is to let us know if the user won or not. It is defaulted to false because obviously the user hasn’t even played yet, so they can’t be a winner. Well, unless you are a leet h4x0r in which case you are just full of win!
This is followed by our code to generate our secret computer number on a range from 1 – 100. We then go into the major game loop where we ask the user for a number, see if it is the escape “sentinel” value of -1 and if not, we compare it against the one generated by the computer. If it is too low or too high, we print out the appropriate message and around the loop we go once more. If it is the right number, we print a winner message and set our “win” flag to “true” in which case we escape our while loop and go into asking the user if they wish to play another game. If they choose “y” then they will play again. Otherwise, the games end and we all get back to real business of hacking the CIA or something.
Hope you enjoy the small example and keep in mind… with ruby, you can code the same thing a bajillion different ways and always make it more “compact”. Hope this helps you beginner ruby users out there! Thanks for reading! 🙂