A Number Guessing Game
[tpcode]
// Runnable code Demo:
public class NumberGuessingGameDemo {
public static void main(String[] args) {
java.util.Scanner myKB = new java.util.Scanner(System.in);
System.out.println(" THE NUMBER GUESSING GAME");
System.out.println(" ========================
");
int gamesPlayed = 0;
boolean quit = false;
while (!quit) {
int allowableGuesses;
boolean allowHighLowHints;
gamesPlayed++;
System.out.println("NEW GAME....#" + gamesPlayed + "
");
String guessesAllowable = "";
while (guessesAllowable.isEmpty()) {
System.out.println("A mystery number from 1 to 100 will be automatically");
System.out.println("generated. How many guesses do you think it will take");
System.out.print("you to guess this mystery number (q to quit)? --> ");
guessesAllowable = myKB.nextLine().trim();
if (!guessesAllowable.matches("\d+|(?i)[q]")) {
System.out.println("Invalid Entry (" + guessesAllowable + ")! Try again...
");
guessesAllowable = "";
}
}
if (guessesAllowable.equalsIgnoreCase("q")) {
System.out.println("
GAME OVER - QUITING!");
break;
} else {
allowableGuesses = Integer.parseInt(guessesAllowable);
}
System.out.println();
// Does the User want Hints?
String allowHintsYN = "";
while (allowHintsYN.isEmpty()) {
System.out.print("Want to enable Low/High hints (y/n)? -> ");
allowHintsYN = myKB.nextLine().trim();
if (!allowHintsYN.matches("(?i)[ynq]")) {
System.out.println("Invalid Entry (" + allowHintsYN + ")! Try again...
");
allowHintsYN = "";
}
}
if (allowHintsYN.equalsIgnoreCase("q")) {
System.out.println("
GAME OVER - QUITING!");
break;
} else {
allowHighLowHints = (allowHintsYN.equalsIgnoreCase("y"));
}
java.util.Random r = new java.util.Random();
int numberToGuess = r.nextInt(100) + 1;
java.util.List<String> alreadyGuessed = new java.util.ArrayList<>();
// System.out.println(numberToGuess); // For testing...The random number to guess
System.out.println();
String guess = "";
int guessCounter = 0;
int guessesRemaining;
while (guess.isEmpty()) {
guessesRemaining = allowableGuesses - guessCounter;
if (guessesRemaining == 0) {
System.out.println("
<< GAME OVER - YOU LOSE >>");
System.out.println("You have run out of chances to guess the mystery number!");
System.out.println("The number you needed to guess was: " + numberToGuess + "
");
break;
}
System.out.println("Guess a number between 1 and 100 (or q to quit).");
System.out.print("You have " + guessesRemaining + " guesses remaining: --> ");
guess = myKB.nextLine().trim();
if (guess.matches("(?i)[q]")) {
System.out.println("Quitting this particular game!
");
break;
}
guessCounter++;
int guessedNum;
if (guess.matches("-?\d+")) {
guessedNum = Integer.parseInt(guess);
} else {
System.out.println("Oh Oh...'" + guess + "' is not a number! That cost you one guess.
");
guess = "";
continue;
}
if (alreadyGuessed.contains(guess)) {
System.out.println("Bad news...You already guessed '" + guess + "' previously!");
System.out.println("That cost you one guess. Try again...
");
guess = "";
continue;
} else {
alreadyGuessed.add(guess);
}
if (guessedNum == numberToGuess) {
System.out.println();
System.out.println(" << CONGRATULATIONS! YOU'RE A WINNER! >>");
System.out.println(guessedNum + " was the mystery number and you managed to guess");
System.out.println("that number in " + guessCounter
+ (guessCounter == 1 ? " guess" : " guesses")
+ " which is " + (guessCounter == allowableGuesses ? "what" : "below")
+ " the allotted");
System.out.println(allowableGuesses + " guesses you said "
+ "you could do it in.
");
break;
} else if (guessedNum < 1 || guessedNum > 100) {
System.out.println("Ha Ha Ha...Bad news friend, you have to pick a number between 1 and 100.");
System.out.println("Pay attention! That cost you one guess. Try again...
");
guess = "";
} else if (guessedNum > numberToGuess) {
System.out.println("Hard luck."
+(allowHighLowHints ? " Your guess is a little too high." : " Incorrect!")
+ " Try again...
");
guess = "";
} else if (guessedNum < numberToGuess) {
System.out.println("Hard luck."
+(allowHighLowHints ? " Your guess is a little too low." : " Incorrect!")
+ " Try again...
");
guess = "";
}
}
String playAgain = "";
while (true) {
System.out.println("<< Press ENTER to play again >>");
System.out.println("<< or q to quit >>");
playAgain = myKB.nextLine().trim();
if (playAgain.equalsIgnoreCase("q") || playAgain.isEmpty()) {
break;
}
}
if (playAgain.equalsIgnoreCase("q")) {
quit = true;
}
}
}
}
[/tpcode]
// Runnable code Demo:
public class NumberGuessingGameDemo {
public static void main(String[] args) {
java.util.Scanner myKB = new java.util.Scanner(System.in);
System.out.println(" THE NUMBER GUESSING GAME");
System.out.println(" ========================
");
int gamesPlayed = 0;
boolean quit = false;
while (!quit) {
int allowableGuesses;
boolean allowHighLowHints;
gamesPlayed++;
System.out.println("NEW GAME....#" + gamesPlayed + "
");
String guessesAllowable = "";
while (guessesAllowable.isEmpty()) {
System.out.println("A mystery number from 1 to 100 will be automatically");
System.out.println("generated. How many guesses do you think it will take");
System.out.print("you to guess this mystery number (q to quit)? --> ");
guessesAllowable = myKB.nextLine().trim();
if (!guessesAllowable.matches("\d+|(?i)[q]")) {
System.out.println("Invalid Entry (" + guessesAllowable + ")! Try again...
");
guessesAllowable = "";
}
}
if (guessesAllowable.equalsIgnoreCase("q")) {
System.out.println("
GAME OVER - QUITING!");
break;
} else {
allowableGuesses = Integer.parseInt(guessesAllowable);
}
System.out.println();
// Does the User want Hints?
String allowHintsYN = "";
while (allowHintsYN.isEmpty()) {
System.out.print("Want to enable Low/High hints (y/n)? -> ");
allowHintsYN = myKB.nextLine().trim();
if (!allowHintsYN.matches("(?i)[ynq]")) {
System.out.println("Invalid Entry (" + allowHintsYN + ")! Try again...
");
allowHintsYN = "";
}
}
if (allowHintsYN.equalsIgnoreCase("q")) {
System.out.println("
GAME OVER - QUITING!");
break;
} else {
allowHighLowHints = (allowHintsYN.equalsIgnoreCase("y"));
}
java.util.Random r = new java.util.Random();
int numberToGuess = r.nextInt(100) + 1;
java.util.List<String> alreadyGuessed = new java.util.ArrayList<>();
// System.out.println(numberToGuess); // For testing...The random number to guess
System.out.println();
String guess = "";
int guessCounter = 0;
int guessesRemaining;
while (guess.isEmpty()) {
guessesRemaining = allowableGuesses - guessCounter;
if (guessesRemaining == 0) {
System.out.println("
<< GAME OVER - YOU LOSE >>");
System.out.println("You have run out of chances to guess the mystery number!");
System.out.println("The number you needed to guess was: " + numberToGuess + "
");
break;
}
System.out.println("Guess a number between 1 and 100 (or q to quit).");
System.out.print("You have " + guessesRemaining + " guesses remaining: --> ");
guess = myKB.nextLine().trim();
if (guess.matches("(?i)[q]")) {
System.out.println("Quitting this particular game!
");
break;
}
guessCounter++;
int guessedNum;
if (guess.matches("-?\d+")) {
guessedNum = Integer.parseInt(guess);
} else {
System.out.println("Oh Oh...'" + guess + "' is not a number! That cost you one guess.
");
guess = "";
continue;
}
if (alreadyGuessed.contains(guess)) {
System.out.println("Bad news...You already guessed '" + guess + "' previously!");
System.out.println("That cost you one guess. Try again...
");
guess = "";
continue;
} else {
alreadyGuessed.add(guess);
}
if (guessedNum == numberToGuess) {
System.out.println();
System.out.println(" << CONGRATULATIONS! YOU'RE A WINNER! >>");
System.out.println(guessedNum + " was the mystery number and you managed to guess");
System.out.println("that number in " + guessCounter
+ (guessCounter == 1 ? " guess" : " guesses")
+ " which is " + (guessCounter == allowableGuesses ? "what" : "below")
+ " the allotted");
System.out.println(allowableGuesses + " guesses you said "
+ "you could do it in.
");
break;
} else if (guessedNum < 1 || guessedNum > 100) {
System.out.println("Ha Ha Ha...Bad news friend, you have to pick a number between 1 and 100.");
System.out.println("Pay attention! That cost you one guess. Try again...
");
guess = "";
} else if (guessedNum > numberToGuess) {
System.out.println("Hard luck."
+(allowHighLowHints ? " Your guess is a little too high." : " Incorrect!")
+ " Try again...
");
guess = "";
} else if (guessedNum < numberToGuess) {
System.out.println("Hard luck."
+(allowHighLowHints ? " Your guess is a little too low." : " Incorrect!")
+ " Try again...
");
guess = "";
}
}
String playAgain = "";
while (true) {
System.out.println("<< Press ENTER to play again >>");
System.out.println("<< or q to quit >>");
playAgain = myKB.nextLine().trim();
if (playAgain.equalsIgnoreCase("q") || playAgain.isEmpty()) {
break;
}
}
if (playAgain.equalsIgnoreCase("q")) {
quit = true;
}
}
}
}
[/tpcode]