Skip to content

Instantly share code, notes, and snippets.

@dpickett
Created January 20, 2014 22:50
Show Gist options
  • Save dpickett/8530979 to your computer and use it in GitHub Desktop.
Save dpickett/8530979 to your computer and use it in GitHub Desktop.

Guess The Number

In this assignment you'll create a simple guessing game using some of the Ruby fundamentals you've learned.

Learning Goals

  • Create and run a simple Ruby program.
  • Extract requirements from the given user stories.
  • Utilize Ruby's looping constructs.
  • Read user input from the command-line.

Instructions

Let's first setup a directory to work in:

mkdir -p ~/Dropbox/launchacademy/guess_the_number
cd ~/Dropbox/work/guess_the_number

Here you created a directory using mkdir (the -p flag will create the necessary parent directories) and changed it to be the current directory using cd.

Now you can start writing the program in your text editor. Running the following command will open up Sublime Text 2 and create an empty file called game.rb:

subl game.rb

You can add your Ruby code in the editor and save it. When you're ready to test, you can run the following command:

ruby game.rb

This will run the code found in the game.rb file using the Ruby interpreter.

User Stories

The requirements for the game are defined here as user stories.

Prompt for a guess

As a user
I want to be prompted for a guess with the valid range
So that I know what input is valid

Acceptance Criteria:

  • After starting the program, user is prompted with Guess a number between 0 and #{MAX}: where MAX is the largest number it could be.
  • The user's guess should appear on the same line as the prompt (e.g. Guess a number between 0 and 1000: 542).

Submit a guess

As a user
I want to submit a guess
So that I can try to win the game

Acceptance Criteria:

  • Entering a number and hitting enter will submit the user's guess.

Display feedback

As a user
I want to receive feedback from my guess
So that I know how to adjust my next guess if needed

Acceptance Criteria:

  • Print Congratulations, you've guessed the number if the guess was correct.
  • Print Too high, try again... if the guess was greater than the number and re-prompt for user input.
  • Print Too low, try again... if the guess was less than the number and re-prompt for user input.

Advanced - Check for valid input

As a user
I want to receive feedback if my input was invalid
So that I how to adjust my input

Acceptance Criteria:

  • Print Invalid input, must enter a number between 0 and #{MAX}. if user submits a non-integer value.

Sample Output

Here is what a sample run of the game should look like:

$ ruby game.rb
Guess a number between 0 and 1000: 500
Too high, try again.
Guess a number between 0 and 1000: 250
Too high, try again.
Guess a number between 0 and 1000: 125
Too high, try again.
Guess a number between 0 and 1000: 67
Too high, try again.
Guess a number between 0 and 1000: 33
Too low, try again.
Guess a number between 0 and 1000: 50
Too low, try again.
Guess a number between 0 and 1000: 57
Congratulations, you guessed the number!

With input validation:

$ ruby game.rb
Guess a number between 0 and 1000: foo
Invalid input, must enter a number between 0 and 1000.

Implementation Notes

  • You can generate a random integer in Ruby using the rand method.
  • puts and print will output the given string but only puts will include a newline at the end.
  • You can read a user's input from stdin (standard input, which in this case will be whatever the user types before hitting enter) using gets.chomp (chomp removes the newline at the end).
  • You can convert a string to an integer using the to_i method.

© 2014 Launch Academy under the Creative Commons With Attribution License

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment