-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinCalc.java
More file actions
360 lines (325 loc) · 12 KB
/
WinCalc.java
File metadata and controls
360 lines (325 loc) · 12 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package Lab4;
/*
* Win Calculator:
* This class calculates and displays the winner of the round, and also
* tracks pertinent game information, such as winning streaks, moves used
* total wins, etc.
*/
import javax.swing.JOptionPane;
/***Move Key***
* 'S' or 1=Scissors
* 'P' or 2=Paper
* 'R' or 3=Rock
***Result Key***
* Win == (1, 2, 3) A tie + move used
* Win == (4, 5, 6) User win + move used
* Win == (7, 8, 9) Computer win + move used
*/
public class WinCalc {
//Constructors for Random Number Generator, Speech, Animation, and Main classes
static RandomGen gen = new RandomGen();
static Speech speak = new Speech();
static Animation animate = new Animation();
static Main sys = new Main();
//Variable for the User's Name
static String userName;
//Variable for tracking rounds
static int round;
//Variable for the winner
static int Win;
//Variables to keep track of the last three wins/moves (for Hard)
static int winOne=0, winTwo=0, winThree=0;
static int uStreak=0, cStreak=0, tieStreak=0;
static int[] cMovStreak={0, 0, 0};
//Variable to determine if computer won last game
static boolean npcWin;
static boolean tie;
//Variables to track number of times user/comp used a move
private static int uRock=0, uPap=0, uSci=0, cRock=0, cPap=0, cSci=0;
//Variables to track number of times user/comp won with a move
private static int uRockW=0, uPapW=0, uSciW=0, cRockW=0, cPapW=0, cSciW=0;
//Variables to track total wins/ties for user/comp
private static int uWins=0, cWins=0, ties=0;
/* Was planning on turning all of the above tracking variables into arrays,
* but I ended up running out of time.
*/
// Hub method
public void start(int uMove, int cMove, int round, String name, boolean hard)
{
//Update trackers
this.round=round;
userName=name;
//Call find to calculate winner
find(uMove, cMove);
//Call streakCounter to update streak records
streakCounter(cMove);
display(uMove, cMove, hard);
winAccountant();
}
// Method to "find" the winner
public void find(int uMove, int cMove)
{
switch (uMove)
{case 1 : //Scissors
switch (cMove)
{case 1: Win=1; //Tie with Scissors
break;
case 2: Win=4; //User wins with Scissors
break;
case 3: Win=9; //Comp wins with Rock
break;
}
break;
case 2 : //Paper
switch (cMove)
{case 1: Win=7; //Comp wins with Scissors
break;
case 2: Win=2; //Tie with Paper
break;
case 3: Win=5; //User wins with Paper
break;
}
break;
case 3 ://Rock
switch (cMove)
{case 1: Win=6; //User wins with Rock
break;
case 2: Win=8; //Comp wins with Paper
break;
case 3: Win=3; //Tie with Rock
break;
}
break;
default: JOptionPane.showMessageDialog(null,
"Error: Unknown Move Input - WinCalc.find 97",
"digiRPS - Error",
JOptionPane.ERROR_MESSAGE);
break;
}
//Update boolean trackers for last game result
npcWin=((Win==7) ^ (Win==8) ^ (Win==9));
tie=((Win==1) ^ (Win==2) ^ (Win==3));
}
// Method to display the winner on Easy difficulty
// Original dispEasy Method; replaced by display();
public void dispEasy()
{
//Null block of code was original display method; keeping just in case
/* switch (Win)
{
case 1:
case 2:
case 3: JOptionPane.showMessageDialog(null,
"It's a tie!",
"digiRPS - Results",
JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
case 5:
case 6: JOptionPane.showMessageDialog(null,
userName + " won!",
"digiRPS - Results",
JOptionPane.INFORMATION_MESSAGE);
break;
case 7:
case 8:
case 9: JOptionPane.showMessageDialog(null,
"Computer won!",
"digiRPS - Results",
JOptionPane.INFORMATION_MESSAGE);
break;
}
*/
}
// Method to display the winner on either difficulty
public void display(int uMove, int cMove, boolean hard)
{
animate.start(uMove, cMove, hard);
}
// Original dispHard Method; replaced by display();
public void dispHardNull()
{
//Null block of code was original display method; keeping just in case
/*switch (Win)
{
case 1:
case 2:
case 3: JOptionPane.showMessageDialog(null,
"It's a tie!",
"digiRPS - Results",
JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
case 5:
case 6: JOptionPane.showMessageDialog(null,
userName + " won!",
"digiRPS - Results",
JOptionPane.INFORMATION_MESSAGE);
break;
case 7:
case 8:
case 9: JOptionPane.showMessageDialog(null,
"Computer won!",
"digiRPS - Results",
JOptionPane.INFORMATION_MESSAGE);
break;
}*/
}
// Method to update win-tracking variables
public void winAccountant()
{
switch (Win)
{case 1: //A tie with scissors
ties++; uSci++; cSci++;
break;
case 2: //A tie with paper
ties++; uPap++; cPap++;
break;
case 3: //A tie with rock
ties++; uRock++; cRock++;
break;
case 4: //A win for user with Scissors
uWins++; uSci++; uSciW++;
cPap++;
break;
case 5: //A win for user with Paper
uWins++; uPap++; uPapW++;
cRock++;
break;
case 6: //A win for user with Rock
uWins++; uRock++; uRockW++;
cSci++;
break;
case 7: //A win for Comp with Scissors
cWins++; cSci++; cSciW++;
uPap++;
break;
case 8: //A win for Comp with Paper
cWins++; cPap++; cPapW++;
uRock++;
break;
case 9: //A win for Comp with Rock
cWins++; cRock++; cRockW++;
uSci++;
break;
default: JOptionPane.showMessageDialog(null,
"Error: Unknown Win Value - WinCalc.winAccountant 207",
"digiRPS - Error",
JOptionPane.ERROR_MESSAGE);
break;}
// Update the last three wins
winThree=winTwo;
winTwo=winOne;
winOne=Win;
}
// Method to save the post-game results as a String, and send it back to Main
public String tally()
{
String output =
" _-Final Tally-_\n\n" +
"Total Rounds Played: " + round + "\n\n" +
"Total Ties: " + ties + "\n\n" +
"User Wins: " + uWins + "\n" +
" Rock win ratio: " + uRockW + " of " + uRock + "\n" +
" Paper win ratio: " + uPapW + " of " + uPap + "\n" +
" Scissors win ratio: " + uSciW + " of " + uSci + "\n\n" +
"Computer Wins: " + cWins + "\n" +
" Rock win ratio: " + cRockW + " of " + cRock + "\n" +
" Paper win ratio: " + cPapW + " of " + cPap + "\n" +
" Scissors win ratio: " + cSciW + " of " + cSci + "\n";
return output;
}
// Method to get the User or Computer's cumulative winning moves/move uses
public int getMoves(int choose)
{
int uWinMov=0, uFavMov=0, cWinMov=0;
switch (choose)
{case 1: //AI uses code number "1" to get User's winning moves
if ((uRockW>uPapW)&&(uRockW>uSciW))
{uWinMov=3;}
else if ((uPapW>uRockW)&&(uPapW>uSciW))
{uWinMov=2;}
else
{uWinMov=1;}
return uWinMov;
case 2: //AI uses code number "2" to get User's most used move
if ((uRock>uPap)&&(uRock>uSci))
{uFavMov=3;}
else if ((uPap>uRock)&&(uPap>uSci))
{uFavMov=2;}
else
{uFavMov=1;}
return uWinMov;
case 3: //AI uses code number "3" to get its own winning moves
if ((cRockW>cPapW)&&(cRockW>cSciW))
{cWinMov=3;}
else if ((cPapW>cRockW)&&(cPapW>cSciW))
{cWinMov=2;}
else
{cWinMov=1;}
return cWinMov;
default: //Error message for erronous input by AI
{JOptionPane.showMessageDialog(null,
"Error: unknown chooser value - WinCalc.getMoves 272",
"digiRPS - ERROR",
JOptionPane.ERROR_MESSAGE);
break;
}
}
return uWinMov;
}
// Getter method for user's wins
public int get_uWins()
{
return uWins;
}
// Getter method for computer's wins
public int get_cWins()
{
return cWins;
}
// Method to count winning streaks
public void streakCounter(int cMove)
{
switch (Win)
{case 1:
case 2:
case 3:
tieStreak++;
uStreak=0;
cStreak=0;
break;
case 4:
case 5:
case 6:
tieStreak=0;
uStreak++;
cStreak=0;
break;
case 7:
case 8:
case 9:
tieStreak=0;
uStreak=0;
cStreak++;
break;
}
switch (cMove)
{case 1:
cMovStreak[0]++;
cMovStreak[1]=0;
cMovStreak[2]=0;
break;
case 2:
cMovStreak[0]=0;
cMovStreak[1]++;
cMovStreak[2]=0;
break;
case 3:
cMovStreak[0]=0;
cMovStreak[1]=0;
cMovStreak[2]++;
break;
}
}
}