# Java Guessing Game

## Introduction

Note: You'll need to know about for loops and if statements for this guessing game, so you'll need to have read all but the last of the beginner tutorials or already know all of these concepts.

In this guessing game, the computer will come up with a random number between 1 and 1000. The player must then continue to guess numbers until the player guesses the correct number. For every guess, the computer will either say "Too high" or "Too low", and then ask for another input. At the end of the game, the number is revealed along with the number of guesses it took to get the correct number. Ready to follow along to create this guessing game? All right.

## Coding Up the Guessing Game

First, we're going to start by creating a new class, or Java file. Call your new program GuessingGame, keeping the capitalization the same. If you're using Eclipse (and I strongly urge you to!) then make sure to checkmark the box to have it put in your main method for you. You should start out like this:

Ok, we need the computer to generate random numbers. Just add this code inside your main method so you have this:

Don't worry much about how Random works. All you need to know for this guessing game is that the variable numberToGuess holds the integer that the player has to guess. Notice you'll get an error when you try to use Random. This is the same problem that Scanner has. All you have to do is right-click your work area, go to source, and select Organize Imports. This will take care of the problem for you. If you want to just import manually, type in import java.util.Random; at the very top of the page.

Now, we need to stop and figure out exactly what we need our game to do and how we're going to accomplish this goal. It's best to do this planning BEFORE you beign coding, so let's start by listing what the guessing game needs to do, also known as the requirements of the program.

Needs of the guessing game:

• Creates a random number to guess
• Keeps track of number of guesses
• Asks us to guess a number
• Let user input a number
• Tells us whether we're guessing too high or too low
• Keeps playing until we guess the correct number
• Tells us the correct number and the number of tries
• This is a small list, but it does say everything we need to do for our guessing game to work. We already took care of the first need, which was to create a random number. So, let's move on to the next requirement, keeping track of the number of guesses.

To keep track of anything, you need a variable. In this case, since we're keeping track of guesses, a simple integer variable will do. Add an int variable to your code, and start it off at 0, since at the beginning the player has made no guesses.

So far we have the variable, but it still does not keep track of the number of guesses. At this point it doesn't make sense to make it do so because the user isn't being asked to make any guesses yet.

Let's have the computer ask us to guess a number. This is pretty simple, and something you've known how to do since the Hello World tutorial if you've been following along.

Ok, so that requirement is completely done and you can scratch it off your to-do list. Now, we need the player to be able to input the number. This means we're going to need a Scanner.

I like to define all my variables as high up in the code as possible, and I suggest you try to do the same. It helps you keep track of all the different variables you have and makes sure that you don't accidentally use the same variable twice. So, create a Scanner at right under your variable that keeps track of the number of guesses. You know how to create a Scanner by now, right? :)

Now that we have a scanner to use, we need to actually have a variable that stores the input from the user. You can create this variable at the top too, but don't make it equal anything yet. Remember how this is done?

Now with this variable, under where the computer asks for input, have your new variable store the input from the scanner. Remember, the player will be guessing integers, so having the variable be an integer is a must. Also, remember that using nextLine() with Scanner probably isn't the best approach here. Do you remember what to use instead for integers?

Once you've written the code to accept input, you can scratch that off of your requirements list.

The computer then needs to tell us if this guess was too high or too low. Notice how in that sentence I used the word if. That's a big clue as to what you need to use to accomplish this. Let's break down the if statement.

• If the number guessed is higher than the real number, tell us its too high.
• If the number guessed is smaller than the real number, tell us its too low.
• If the number guessed is the same as the real number, tell us that we won.

You could do this in three different if statements, but it's best to use else if in this case to tie them all together. It helps to show that all those if's are related to each other, and that only one of those if's will ever be true at one time (the guessed number can never be too high and too low at the same time, for example).

This is what your code should look like at this point:

Great, we can cross of another requirement off of our list. This is what we have left to do:

• Keeps track of number of guesses (remember we only finished part of it)
• Keeps playing until we guess the correct number
• Tells us the correct number and the number of tries

Let's tackle the first item on that list and get rid of it once and for all. When should we track the number of guesses? Right after the player guesses the number of course! Just add one to the variable we created to do this after the player guesses a number. That's all there is to it!

Okay, so if we were to run our guessing game right now, the program would go one time, and then stop. This is because we need it to keep going until the user wins. We will have to think about this a little bit before we code it.

First of all, what ways do we know to make Java do something over and over again? In the tutorials we went over two ways, the for loop and the while loop. If you remember the difference, then you know that the for loop loops for a certain number of times. Unfortunately the number of times this program could loop depends on the player! So, a for loop is probably not the best way to handle this.

A while loop is the perfect choice. It keeps going until a condition is no longer true. So, while the player hasn't won yet, keep going. But how do we keep track of whether or not the player has won?

Simple. We use a boolean variable. We can create a boolean variable called win near the top of our code with all the other variables. If we set win to false, then it means the player hasn't won yet. When win is true, then the player won the game.

The last thing we need to figure out is which code to put inside of this while loop. I recommend everything starting from the computer asking the player to guess a number all the way down to the if statements.

See how inside the while loop parenthesis the condition is when win is equal to false? This means it will continue to loop until something sets the win variable to true.

But what will set the win variable to true? The third part of the if statement seems like a good choice. You know, the part that asks if the player guessed the correct number. Here is what this looks like:

Phew, so now we've gotten rid of all the requirements except one. We need the game statistics when the game is over. Ok, after your while loop, we can add the code in. It should just be a series of printlns that tell us everything we need to know, such as number of guesses made.

Your guessing game program is now complete!

Go ahead and play it. I wouldn't try guessing letters or anything like that, as your program will crash. However, the game does work, so enjoy or show it off!

Note: By the way, to make the guessing game harder or easier, simply change the number inside of the parenthesis of the Random variable we created. You can change it from 1000 to 10 so it creates a number from 1 to 10, or you can make the number larger. Have fun!

**Editor's Note: The game actually picks a number between 0 and 999, not 1 and 1000. You can add one to the numberToGuess variable to fix that issue.