Chapter 03 Lab Part 1:
IF and FOR loop
The “Guess the number” Game
1. Introduction
Now that you have the knowledge of the IF and FOR loop structures in your toolbox, there are lots of cool things that you can start programming. Just to warm up, let’s create a “Guess the number” Game. In this game, we set a “hidden” number and ask the player to guess what the number is. The player has 4 changes to guess it right, otherwise it is game over.
2. General Overview
Let’s think about what must be done and then we implement it step-by-step.
a) The program creates a “secret number” from 1 through 10.
b) The program creates a variable to keep track of the number of trials (so that if this number is bigger than 4 it will stop the game)
c) The program asks the player to guess a number.
d) The player enters the number via keyboard.
e) The program checks if the guessed number is outside the range (1-10)
i) If it is outside the range, the program reminds the player that only number from 1 through 10 are allowed, and the program goes back to item (c) above.
f) Having a valid guessed number, the program shall check if the guessed number is equal to the secret number
i) If it is so the program let the player know that the number is right and stop the game.
ii) Otherwise the program:
* Tells the player that the guessed number is not right.
* Increases by one the number of trials
* Checks if the number of trials is bigger than the maximum number of trials that the player has
– If the current trial number is not bigger than maximum number of trials, the program shall go back to item (c) above
– Otherwise, the program shall let the player know that the game is over and show the secret number in the terminal window.
– Stop the program
3. Game implementation
Implementing even a simple program like this one can be a huge and complicated task for someone that has never program before. The best way to attach it is going item-by-item from Section 2 and slowly implementing it. I will not give you the code, but I will help you creating it in this section.
a) Create a Java Application in NetBeans project. Give whatever name you want.
b) How many numbers does this program must keep track? (answer: at least four: the secret number, the guessed number, the current trial number, and finally the maximum allowed number of trials).
c) What TYPE of variables are them? Doubles, integers, Booleans????
d) Create those four variables inside the main method that NetBeans has already created for you in your new project. Set the secret number to any valid number, let’s say set it to 7.
e) How can the program ask the player to enter a number in the terminal window? By using a System.out.print() statement. Create this statement in your program.
f) How can your program get the player guessed number that was typed in the keyboard? By using a Scanner keyboard object. This is done in two steps: first you create the scanner object (just once in your code, so create it close to the other variables that you have already created in item (d) above. Secondly you use keyboard.nextInt() just after the System.out.print() command that you created in item (e) above.
g) Now that the player has entered the number, your program shall check if it is a valid number (valid numbers are from 1 to 10). I said “CHECK”. What Java structure we use to do checks? An IF statement. Now pay attention here: two things must happen to verify if the number is invalid: if the number is smaller than 1 OR the number is bigger than 10! If either condition is true, then the number is invalid. Implement that IF statement.
h) If the number is NOT valid, print a message in the terminal window stating that. Just for now, issue a System.exit(0) after your print statement, so the program will terminate. Later on we will change this.
i) If the guessed number is valid (this is the ELSE portion of the IF statement of item (g) above), then your program shall CHECK if the guessed number is equal to the secret number. How do implement this type of check? Using an IF statement! Implement it!
j) If the guessed number is equal to the secret number, then your program shall print a statement in the terminal window congratulating the player (System.out.println()) and stop the program (System.exit(0)).
k) If the guessed number is not equal to the secret number (the ELSE portion of the IF statement from item (i)), your program shall increment by 1 the number of trials (something like numberOfTrials++)
l) After incrementing the number of trials, your program shall CHECK if the player has reached the maximum number of trials. Use an IF statement for that.
m) If the player has reached the maximum number of trials, print a statement in the terminal window (System.out.println()) stating that the game is over and then exit the program (System.exit(0)).
n) If the player has not reach the maximum number of trials, your program shall repeat items (e) through (m) again. How can we force JVM to repeat a block of commands like these ones 4 times? Using a FOR loop! So, let’s ask ourselves: “What must be inside the FOR loop? What must be repeated over and over? Well, defining variables as described in (b), (c), and (d) are not necessary to be recreated over and over. Creating just once is enough, so these Java commands shall be outside the FOR loop.
o) Put the FOR loop statement just before item (e) above. You can use the variable that you are using to keep track of the current trial number as your IF internal variable, e.g.,
if (int currentTrialNumber = 0; currentTrialNumber < 4; currentTrialNumber++)
if you do that, you do not need to specify this variable in item (c). Just remove it from that item.
p) Remember that we have used a System.exit() method in item (h) above when the number was not valid? Well we do not want that; we want the program to not proceed to the items below (h) but rather go back to item (e) to ask a new number, right? How can we force JVM to not proceed in a regular FOR loop flow and go back to its beginning point? (item (e) in this case). Can we accomplish this by using “break” or “continue” statements??? Think about and replace the System.exit() with the one that is the right one.
4. Test your program
Play with it with at least two situations: one you guess the secret number under 4 trials and the other where the maximum number of trials is reached and you should get a “game over”.
5. Program improvements
There are at least two improvements that you could probably do in your code implementation:
a) Replacing magic numbers from the FOR loops (such as the “4” in item (n) above), by using the variable that you have defined in (d) above. The same thing when you are checking the validity of the number entered by the player in item (g). Do those changes.
b) At this point this game is useless if I want to put it in internet, since the “secret number” is always 7. So if the players re-run this program they can win the game in the first guess! We can improve the code by creating a randomly generated number from 1 through 10 using a Java API Random class. To figure out how to do it, do an internet search on this topic “how to generate a random number from 1 to 10 in Java” or something like that. Study the examples that you find and adapt them to your program.
c) Finally, don’t forget to right click on your source code screen and select “Format” option to get your code style corrected by NetBeans automatically for you.
d) 4 trials are not many to win this game. To improve the player odds, your program should add a little help (hint) when asking for another number: higher or lower, such as shown below:
Enter a number: 7
Enter a number: (hint: lower) 3
Enter a number: (hint: higher) 5
Enter a number: (hint: lower) 4
Player guessed right! Congratulations!
6. Final Thoughts
After the completion of this lab/assignment you might not have realized but you have practiced tons of Java concepts that you’ve learned in this class so far:
- FOR loops
- IF statements
- Logical Operators (OR)
- Scanner objects
- Creating and Assigning values to Variables
- Magic numbers
- Input checking
- Magic numbers
- Code style (format)
- General code logic (Section 2) and implementation (Section 3)
- Java API classes (Random and Scanner)
- How to look for solutions in the internet