(Updated July 26, 2024)
Overview
When building a simple text game, we want the process to be smooth and elegant. There should be few places to trip up the user or make the play awkward. In this write-up, we look at how we can craft the building blocks of a valuable and user-friendly game.
User Input
The hardest part of any game is getting the player to follow directions. But first, we have to start the interaction.
System.out.print("Enter a value from 1 to 20: ");
guess = kb.nextInt();
We provide clear instructions, and hopefully, the user complies. If not, we can add some additional code to try and corral them:
do {
System.out.print("Enter a value from 1 to 20: ");
guess = kb.nextInt();
} while (guess < 1 || guess > 20);
Now, with the do
loop, we can ensure they are not going out of bounds. But just a bit more is needed as we also want them to know they did not follow directions. Adding the boolean
variable first
allows us to detect how many times the user has tried and failed to produce a proper number.
boolean first = true;
do {
if (!first)
System.out.println("\nPlease follow directions!");
System.out.print("Enter a value from 1 to 20: ");
guess = kb.nextInt();
first = false;
} while (guess < 1 || guess > 20);
The first time through the loop, we accept the user’s input. If this is an attempt beyond the first, we tell them they must follow directions.
Let’s set the user input aside for the moment and work on the computer setting up its part of the game.
Random Numbers and Guessing
Let’s begin to rough out the main portion of the program. We use Scanner
for input.
import java.util.Scanner;
public class GuessingGame {
static Scanner kb = new Scanner(System.in);
static final int MAX = 20;
public static void main(String[] args) {
int guess;
int comp = (int)(Math.random() * MAX + 1);
System.out.println("I am thinking of a random number!");
while ( true ) {
System.out.print("Enter a value from 1 to "+MAX+": ");
guess = kb.nextInt();
if (guess == comp) {
System.out.println("Yay! You did it!");
break;
}
}
}
}
This code sets up a bit more for the overall program. Here are some of the highlights:
- We’ve traded the hard coding of
20
for a named constantMAX
. - We generate a random number using
Math.random()
. - We allow infinite guesses and a
break
statement to exit the loop when the player wins.
We have not yet reintroduced the code to keep their guesses in range. Before we do that, let’s look at extending the range. Setting MAX
to 1000
makes the game more interesting but also more difficult. By adding hints to lead the player to potential success, we can keep their attention as they begin to see progress.
if (guess == comp) {
System.out.println("Yay! You did it!");
break;
} else if ( guess < comp )
System.out.println("That's not it. Try higher.");
else
System.out.println("That's not it. Try lower.");
The multi-way if
test still allows us to detect a win while providing some help for the player. We can also do better with our while loop and remove the break
statement in favor of a boolean
. Although the break
works, our boolean
provides more understanding of the logic.
boolean found = false;
while ( !found ) {
System.out.print("Enter a value from 1 to "+MAX+": ");
guess = kb.nextInt();
if (guess == comp) {
System.out.println("Yay! You did it!");
found = true;
} else if ( guess < comp )
System.out.println("That's not it. Try higher.");
else
System.out.println("That's not it. Try lower.");
}
Now that we have a loop that is more idiomatic (flag-based), we can feel confident that this code is easily understood and accomplishes that task more logically.
All that is left is to reintroduce the code to control user input.
And We're Done
Here is the final version of our game:
import java.util.Scanner;
public class UsingRandom {
static Scanner kb = new Scanner(System.in);
static final int MAX = 1000;
public static void main(String[] args) {
int guess;
int comp = (int)(Math.random() * MAX + 1);
System.out.println("I am thinking of a random number!");
boolean found = false;
while ( !found ) {
boolean first = true;
do {
if (!first)
System.out.println("\nPlease follow directions!");
System.out.print("Enter a value from 1 to "+MAX+": ");
guess = kb.nextInt();
first = false;
} while (guess < 1 || guess > MAX);
if (guess == comp) {
System.out.println("Yay! You did it!");
found = true;
} else if ( guess < comp )
System.out.println("That's not it. Try higher.");
else
System.out.println("That's not it. Try lower.");
}
}
}
How could you change this program to allow only a limited number of guesses? Would you still keep the hints?