-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeminiRPSGame.java
More file actions
195 lines (163 loc) · 7.5 KB
/
Copy pathGeminiRPSGame.java
File metadata and controls
195 lines (163 loc) · 7.5 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import java.util.Random;
import java.util.Scanner;
public class GeminiRPSGame {
// --- Enum for Moves ---
enum Move {
ROCK, PAPER, SCISSORS, NONE
}
// --- Global Variables ---
static int playerScore = 0;
static int computerScore = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// --- 1. Distinct Gemini Intro (Asterisk Box) ---
System.out.println("*************************************************");
System.out.println("* *");
System.out.println("* This is created by Gemini 3 *");
System.out.println("* Multithreaded Rock, Paper, Scissors *");
System.out.println("* *");
System.out.println("*************************************************");
System.out.println();
// --- 2. Explain the Rules Clearly ---
System.out.println("WELCOME, CHALLENGER!");
System.out.println("Here are the rules of engagement:");
System.out.println("1. Rock beats Scissors.");
System.out.println("2. Scissors beats Paper.");
System.out.println("3. Paper beats Rock.");
System.out.println();
System.out.println("HOW TO PLAY:");
System.out.println("When prompted, type one of the following letters and press ENTER:");
System.out.println(" - Type 'R' for Rock");
System.out.println(" - Type 'P' for Paper");
System.out.println(" - Type 'S' for Scissors");
System.out.println(" - Type 'Q' to Quit the game");
System.out.println("*************************************************");
System.out.println();
// --- 3. Ask for User Name ---
System.out.print("Please enter your name to begin: ");
String playerName = scanner.nextLine();
// Handle empty name
if (playerName == null || playerName.trim().isEmpty()) {
playerName = "Player 1";
}
System.out.println("\nReady, " + playerName + "? Let's start!");
boolean keepPlaying = true;
// --- 4. Game Loop ---
while (keepPlaying) {
System.out.println("\n-------------------------------------------------");
System.out.println("CURRENT SCORE -> " + playerName + ": " + playerScore + " | Computer: " + computerScore);
System.out.print("Your move (R/P/S) or Q to quit: ");
String input = scanner.nextLine().toUpperCase().trim();
// Handle Quit
if (input.equals("Q")) {
keepPlaying = false;
System.out.println("Quitting game...");
continue;
}
Move playerMove = parseInput(input);
// Handle Invalid Input
if (playerMove == Move.NONE) {
System.out.println("!!! Invalid input. Please type R, P, or S only.");
continue;
}
// --- 5. Multithreading Implementation ---
// We create a separate thread for the Computer to 'think'
ComputerAI computerThread = new ComputerAI();
computerThread.start();
// While the thread works, we show a loading animation on the main thread
try {
System.out.print("Computer is thinking");
for(int i=0; i<3; i++) {
Thread.sleep(400); // Main thread pauses purely for effect
System.out.print(".");
}
System.out.println();
// CRITICAL: We wait for the computer thread to finish before moving on
computerThread.join();
} catch (InterruptedException e) {
System.out.println("Game interrupted!");
}
// Retrieve the move from the thread
Move computerMove = computerThread.getMove();
// Determine and display the winner
determineWinner(playerName, playerMove, computerMove);
}
// --- 6. End Game Summary ---
System.out.println("\n*************************************************");
System.out.println("* FINAL RESULTS *");
System.out.println("*************************************************");
System.out.printf("* %-15s: %d *\n", playerName, playerScore);
System.out.printf("* %-15s: %d *\n", "Computer", computerScore);
System.out.println("*************************************************");
if (playerScore > computerScore) {
System.out.println("* CONGRATULATIONS! YOU WON! *");
} else if (computerScore > playerScore) {
System.out.println("* GAME OVER. COMPUTER WINS. *");
} else {
System.out.println("* IT'S A DRAW! *");
}
System.out.println("*************************************************");
scanner.close();
}
// --- Helper Method to Parse Input ---
private static Move parseInput(String input) {
// We only check the first character just in case user types "Rock" instead of "R"
if (input.length() < 1) return Move.NONE;
char firstChar = input.charAt(0);
switch (firstChar) {
case 'R': return Move.ROCK;
case 'P': return Move.PAPER;
case 'S': return Move.SCISSORS;
default: return Move.NONE;
}
}
// --- Helper Method to Determine Winner ---
private static void determineWinner(String name, Move pMove, Move cMove) {
System.out.println("\n> " + name + " chose: " + pMove);
System.out.println("> Computer chose: " + cMove);
System.out.println("-------------------------------------------------");
if (pMove == cMove) {
System.out.println("RESULT: It's a Tie!");
} else if (
(pMove == Move.ROCK && cMove == Move.SCISSORS) ||
(pMove == Move.PAPER && cMove == Move.ROCK) ||
(pMove == Move.SCISSORS && cMove == Move.PAPER)
) {
System.out.println("RESULT: " + getWinningPhrase(pMove) + " You Win this round!");
playerScore++;
} else {
System.out.println("RESULT: " + getWinningPhrase(cMove) + " Computer Wins this round!");
computerScore++;
}
}
// --- Helper Method for Flavor Text ---
private static String getWinningPhrase(Move move) {
switch (move) {
case ROCK: return "Rock crushes Scissors!";
case PAPER: return "Paper covers Rock!";
case SCISSORS: return "Scissors cuts Paper!";
default: return "";
}
}
// --- The Computer Thread Class ---
// This runs the AI logic on a separate execution thread
static class ComputerAI extends Thread {
private Move selectedMove;
private Random random = new Random();
@Override
public void run() {
// The AI logic happens here, completely separate from user input handling
// We simulate a 'thinking' delay and then pick a random move
try {
Thread.sleep(500); // Simulate processing time
int pick = random.nextInt(3);
selectedMove = Move.values()[pick];
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public Move getMove() {
return selectedMove;
}
}
}