-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
124 lines (104 loc) · 4.6 KB
/
GuessingGame.java
File metadata and controls
124 lines (104 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* Tymofiy Grebrnyuk, Roe McKernan, Dylan Torres
10/1/2024
CS& 145
Lab 1: Guessing Game
This program allows a player to participate in a number guessing game,
where they attempt to guess a randomly chosen number. */
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
// Constants
public static final int MAX_NUMBER = 100; // Maximum number for guessing
public static final String BOLD = "\033[1m"; //Making text bold
public static final String UNDERLINE = "\033[4m"; //Making text underline
public static final String RESET = "\033[0m"; //Resets formatting
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
introduction(); // Game introduction
int totalGames = 0;
int totalGuesses = 0;
int bestGame = Integer.MAX_VALUE;
String playAgain;
do {
// Play one game
int guessesThisGame = playOneGame(console);
totalGames++;
totalGuesses += guessesThisGame;
if (guessesThisGame < bestGame) {
bestGame = guessesThisGame;
}
// Ask if the user wants to play again with input validation
playAgain = getValidYesNoResponse(console, "Do you want to play again? ");
} while (playAgain.toLowerCase().startsWith("y"));
// Report overall statistics
reportResults(totalGames, totalGuesses, bestGame);
}
// Method to introduce the game to the user
public static void introduction() {
System.out.println("Welcome to the Guessing Game!");
System.out.println("I am thinking of a number between 1 and " + MAX_NUMBER + ".");
System.out.println("Try to guess it, and I'll tell you if your guess is too high or too low.");
}
// Method to play one game of guessing with input validation
public static int playOneGame(Scanner console) {
Random rand = new Random();
int targetNumber = rand.nextInt(MAX_NUMBER) + 1; // Random number between 1 and MAX_NUMBER
int guess = -1;
int numGuesses = 0;
System.out.println("\nI'm thinking of a number...");
while (guess != targetNumber) {
guess = getValidIntInput(console, "Your guess? ");
numGuesses++;
if (guess < targetNumber) {
System.out.println("It's higher.");
} else if (guess > targetNumber) {
System.out.println("It's lower.");
} else {
if (numGuesses == 1) {
System.out.println("You got it right in 1 guess!");
} else {
System.out.println("You got it right in " + numGuesses + " guesses.");
}
}
}
return numGuesses; // Return the number of guesses made in this game
}
// Method to report overall results to the user
public static void reportResults(int totalGames, int totalGuesses, int bestGame) {
System.out.println("\nOverall results:");
System.out.println("\ttotal games \t= " + totalGames);
System.out.println("\ttotal guesses \t= " + totalGuesses);
System.out.printf("\ttuesses/game \t= %.1f\n", (double) totalGuesses / totalGames);
System.out.println("\tbest game \t= " + bestGame);
}
// Method to validate integer input
public static int getValidIntInput(Scanner console, String prompt) {
System.out.print(prompt);
System.out.print(BOLD + UNDERLINE); //Do font changes
while (!console.hasNextInt()) {
System.out.print(RESET); //Resets font changes
System.out.print(prompt);
System.out.println("\nInvalid input. Please enter an integer.");
console.next(); // Clear the invalid input
System.out.print(prompt);
System.out.print(BOLD + UNDERLINE);
}
System.out.print(RESET);
return console.nextInt();
}
// Method to validate "yes" or "no" response
public static String getValidYesNoResponse(Scanner console, String prompt) {
System.out.print(prompt);
System.out.print(BOLD + UNDERLINE);
String response = console.next().toLowerCase();
System.out.print(RESET);
while (!response.startsWith("y") && !response.startsWith("n")) {
System.out.println("Invalid input. Please enter 'yes' or 'no'.");
System.out.print(prompt);
System.out.print(BOLD + UNDERLINE);
response = console.next().toLowerCase();
System.out.print(RESET);
}
return response;
}
}