Back to 111 Main Page

Mobius strip

Assignment 9


Write a simple text-based number-guessing game.



You are to write a game that generates a random number between 1 and 80. The game then prompts the user to guess the number. If the user's guess is correct, the game congratulates the user and ends.

If the user's guess is not correct, the program will report whether the guess is too high or too low. It will also draw a simple text-based line, showing the user's guess (marked with a '|'), the target random number (marked with a '^'), and the distance between them (marked with '-'s).

Here is the output of a sample game, with user input marked in green:

I'm thinking of a number between 1 and 80. Can you guess what it is?
Pick a number: 1000
Sorry, but 1000 is not a number between 1 and 80. Try again.
Pick a number: 5
You are too LOW:
Pick a number: 65
You are too HIGH:
Pick a number: 61
You are too HIGH:
Pick a number: 60
CORRECT!  You guessed it.

Most console windows are 80 characters wide by default (hence the 1 to 80 range). Depending on your current Eclipse settings you might need to resize your console window a bit to prevent the line from wrapping.

There are no code structure requirements for this assignment. You can just make one big main method if you wish. However, as you may have noticed in the last assignment, long methods can get rather unwieldy. It's hard to have them all on the screen at once, as well as hold them all in your head at once. So, it makes sense to try to break the problem down into methods.

If you're stuck and would like some guidance, here's the structure I used when writing this program:

public class NumberGuesser {

  //class and instance variables, including:
  public static final int MAX_NUM = 80;
  private Scanner input = new Scanner(;
  private int number;  //the random number to be guessed
  private int guess;   //the user's current guess

  public static void main(String[] args) {
    NumberGuesser game = new NumberGuesser();

  public void playGame() {
    //explain the game rules to the user

    //generate a new number to guess:
    this.number = (int) (Math.random() * MAX_NUM) + 1;

    loop (until user guesses the correct answer) {
      this.guess = this.getUserGuess();
      if (guess == number) {
        //user got the correct answer (so tell them so)
      }else {
        //need to report whether they are too HIGH or too LOW;
        //then graphically display their guess:

  public int getUserGuess() {
    int entered = 0;
    loop (until user enters a valid number b/w 1 and MAX_NUM) {
      //prompt user to enter a number
        entered = input.nextInt();
        input.nextLine();  //clear input stream
        if (entered < 1 || entered > MAX_NUM) {
          //report error and let the loop prompt user again
      }catch (InputMismatchException ime) {
        System.out.println("Sorry, that is not a valid integer.  Try again.");
        input.nextLine(); //clear input stream of bad data
    return entered;

  public void displayGuess() {
    loop (for each character in the line, so i goes from 1 to MAX_NUM) {
      if (i equals guess) {
        //print a ^ character
      }else if (i equals number) {
        //print a | character
      }else if (i is less than both guess and number OR
                i is greater than both guess and number) {
        //print a ' ' (space) character
      }else {
        //i is between guess and number,
        //so print a '-' character
    //add a final newline character to finish the line


I've highlighted the NumberGuesser members--variables and methods--in blue. Also, you will need to decide how to write the loops--whether to use a while, do-while, or for loop in each case.

Again, this pseudo-code just presents the way I went out about solving this problem. There is always more than one way to do it! So if a different algorithm makes more sense to you, go ahead and do it your way. You may still be able to pilfer a few tips from the code above, such as how to generate the random number or how to draw the guess line.

Also, in getUserGuess, I included the try/catch code for catching the exception thrown should the user enter something other than a number. This is optional/extra credit.

How to write your program

Do not just cut and paste all the code above into a file and start editing it. If you do that, you're not going to be able to compile and run it until you've written the complete program. This will make it much harder for you to ever get it working. (Really. Trust me.)

You should program step-by-step. After each step, compile and run your program to make sure everything you've done so far works correctly. To get started, I'd suggest you do the following:

  1. Copy the above code through the instance variables and the main method. Create an empty playGame method. Get the code to compile. Run it. (You should get no errors, though nothing will be printed yet.)
  2. Now work on writing playGame. Add print statements that display the game rules. (Compile and run it.)
  3. Now generate the random number. Print it out, and run the program a few times to make sure the random number generation is working.
  4. Now, before you start writing the loop, just add the line this.guess = this.getUserGuess();. Go write the getUserGuess method (step-by-step).
  5. Once you can read in what the user entered, print out what they entered in playGame. Good, everything works so far. Now you're ready to work on the loop in playGame that tells the user whether they guessed high or low.
  6. Eventually, you'll have everything working except the guess-line drawing. Write this method last.

Good luck!

What to Submit

Attach your file to an email.


Will there be any sample code for this assignment?
No. Instead, I explained the algorithm in detail above.
Can we change what characters are used in drawing the guessing graph/line?
Okay. For instance, if you use underscores (_) rather than spaces ( ), and = instead of -, the output looks like this:
Pick a number: 40
You are too HIGH.

Dashes rather than underscores looks okay too:

Pick a number: 5
You are too LOW.

So you can customize your line characters. Just make sure the line between the guess and the target number uses a different character (originally '-') than the default line character (originally ' ').


Out of 10 points:

1 - Submission
Follows required submission policies.
1 - Coding standards
2 - Game user interface
The game basically explains what the user needs to do, and reports errors on poor input (such as numbers not within the acceptable range).
1 - Game generates a random number between 1 and 80
2 - Game asks the user to guess a number
Should continue to ask the user until they enter a valid number.
1 - Should report whether the user's guess is higher or lower than the random number
2 - Should "graphically" represent the user's guess compared to the random number
Do this is a single line of text, as described above

There's also a little extra credit possible for this assignment:

+0.5 - Preventing crashes on bad input
Catch the InputMismatchException that occurs if the user enters letters or other characters when asked to pick a number.
+1.5 - Play again
After each game, ask the user if they want to play again. Start another game (with a new random number, etc.) if they select yes; quit if they select no.

~ztomasze Index : TA Details: ICS111: Assignment 9
Last Edited: 03 Nov 2007
©2007 by Z. Tomaszewski.