-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNimsys.java
More file actions
611 lines (420 loc) · 14.7 KB
/
Copy pathNimsys.java
File metadata and controls
611 lines (420 loc) · 14.7 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/************************************************************
* Name:Yizhou ZHU
* login name:yizzhu@nutmeg.eng.unimelb.edu.au
* student number:1034676
*
* description: this is the main class to run the Nimsys and store the array of NimPlayer
*
* last updated:06/05/2019
*
*
*
************************************************************/
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Nimsys {
private static NimPlayer[] NimPlayerArray; // set the Array static to make it the ONE!
private Nimsys() {
NimPlayerArray = new NimPlayer [100];
for (int i=0; i< NimPlayerArray.length;i++) { // constructor, initialize the ONLY NimPlayerArray
NimPlayerArray[i] = new NimHumanPlayer();
}
}
public NimPlayer[] getArray() {
return NimPlayerArray;// accessor
}
public static NimPlayer getPlayer(int i) {
return NimPlayerArray[i]; // accessor
}
public static int existPlayer(String username) { // judge the existence of the player. if exist, return the index, otherwise return -1
for (int i=0; i< NimPlayerArray.length;i++)
if(NimPlayerArray[i].UserNameequals(username)) {
return i;
}
return -1;
}
private void addAIPlayer(String username, String Firstname, String Lastname) {// add one player to the array
for (int i=0; i< NimPlayerArray.length;i++) {// go through the array to see duplicated username. Once found, print information and end the method
if (NimPlayerArray[i].UserNameequals(username)) {
System.out.println("The player already exists.");
return;
}
}
for (int i=0; i< NimPlayerArray.length;i++) {
if (NimPlayerArray[i].UserNameequals(" ")) {// add player to the first vacancy
NimPlayerArray[i] = new NimAIPlayer();
NimPlayerArray[i].setPlayer(username,Firstname,Lastname);
break;
}
}
}
private void addPlayer(String username, String Firstname, String Lastname) {// add one player to the array
for (int i=0; i< NimPlayerArray.length;i++) {// go through the array to see duplicated username. Once found, print information and end the method
if (NimPlayerArray[i].UserNameequals(username)) {
System.out.println("The player already exists.");
return;
}
}
for (int i=0; i< NimPlayerArray.length;i++) {
if (NimPlayerArray[i].UserNameequals(" ")) {// add player to the first vacancy
NimPlayerArray[i] = new NimHumanPlayer();
NimPlayerArray[i].setPlayer(username,Firstname,Lastname);
break;
}
}
}
private void removePlayer(String username) {
for (int i=0; i< NimPlayerArray.length;i++) {
if (NimPlayerArray[i].UserNameequals(username)) {// find the matching username to restore it to the initial value
NimPlayerArray[i].setPlayer(" ","no name","no name");
return;
}
}
// if the last player in the array is not matched, print the information(although I know it will be redundant condition)
System.out.println("The player does not exist.");
}
private void removePlayer() {
System.out.println("Are you sure you want to remove all players? (y/n)");// double-check
String judge =keyboard.nextLine();
if (judge.equals("y"))
for (int i=0; i< NimPlayerArray.length;i++) {
NimPlayerArray[i].setPlayer(" ","no firstname","no lastname");// set all of them to the initial value
}
}
private void displayPlayer(String username) {
for (int i =0; i < NimPlayerArray.length;i++) {
if (NimPlayerArray[i].UserNameequals(username)) {// find the matching one and display the information
System.out.println(NimPlayerArray[i].getUsername()+","+NimPlayerArray[i].getLastname()+","+
NimPlayerArray[i].getFirstname()+","
+NimPlayerArray[i].getGamesPlayed()+" games,"+NimPlayerArray[i].getGamesWon()+" wins");
return;// end the method as there will only be one matching object in this case
}
}
System.out.println("The player does not exist.");
}
private void editPlayer(String username, String newfirstname, String newlastname) {
for (int i =0; i < NimPlayerArray.length;i++) {
if (NimPlayerArray[i].UserNameequals(username)) {
NimPlayerArray[i].setFirstname(newfirstname);
NimPlayerArray[i].setLastname(newlastname);// set theme with the new name but retain the username
return;
}
}
System.out.println("The player does not exist.");
}
private void Insertionsort() {// selection sort part1
int i,iofNextSmallest;
for (i=0; i<NimPlayerArray.length-1; i++) {
iofNextSmallest = iofSmallest(i);
interchange(i,iofNextSmallest);
}
}
private int iofSmallest(int index) {// find the minimum object in this sorting condition and return the index of it
NimPlayer min = NimPlayerArray[index];
int iofMin = index;
int i;
for(i = index + 1; i<NimPlayerArray.length;i++) {
if(NimPlayerArray[i].getUsername().compareTo(min.getUsername())<0){
min = NimPlayerArray[i];
iofMin = i;
}
}
return iofMin;
}
private void interchange(int i,int j) {
NimPlayer temp;
temp = NimPlayerArray[i];
NimPlayerArray[i] = NimPlayerArray[j];
NimPlayerArray[j] = temp;
}
private int max(int a,int b) {//return the bigger number in the two integers
if(a>=b) {
return a;
}
else {
return b;
}
}
private void InsertionsortVicDsc(){// following 4 methods are all parts of the sorting algorithm for the rankings, refer to the alphabetical sorting
int iofNextSmallestrank;
for (int i=0; i<NimPlayerArray.length-1; i++) {
iofNextSmallestrank = iofSmallestrankvsc(i);
interchange(i,iofNextSmallestrank);
}
}
private int iofSmallestrankvsc(int index) {
NimPlayer min = NimPlayerArray[index];
int iofMin = index;
int i;
for(i = index + 1; i<NimPlayerArray.length;i++) {
double vici = ((double)NimPlayerArray[i].getGamesWon()/max(NimPlayerArray[i].getGamesPlayed(),1));
double vicimin =((double)min.getGamesWon()/max(min.getGamesPlayed(),1)); /* As the initial vacancy have 0 games played, the insertion will return a problem if vici = 0/0,
so i set the 0 one to zero but retain all the other games played.
*/
if(vici>vicimin) {
min = NimPlayerArray[i];
iofMin = i;
}
else if(vici == vicimin) {
if(NimPlayerArray[i].getUsername().compareTo(min.getUsername())<0) {
min = NimPlayerArray[i];
iofMin = i;
}
}
}
return iofMin;
}
private void InsertionsortVicAsc(){
int iofNextSmallestrank;
for (int i=0; i<NimPlayerArray.length-1; i++) {
iofNextSmallestrank = iofSmallestrankasc(i);
interchange(i,iofNextSmallestrank);
}
}
private int iofSmallestrankasc(int index){
NimPlayer min = NimPlayerArray[index];
int iofMin = index;
int i;
for(i = index + 1; i<NimPlayerArray.length;i++) {
double vici = ((double)NimPlayerArray[i].getGamesWon()/max(NimPlayerArray[i].getGamesPlayed(),1));
double vicimin =((double)min.getGamesWon()/max(min.getGamesPlayed(),1));
if(vici<vicimin) {
min = NimPlayerArray[i];
iofMin = i;
}
else if(vici == vicimin) {
if(NimPlayerArray[i].getUsername().compareTo(min.getUsername())<0) {
min = NimPlayerArray[i];
iofMin = i;
}
}
}
return iofMin;
}
private void displayPlayer() {
Insertionsort();// sort the array first
for (int i =0; i < NimPlayerArray.length; i++) {
if (NimPlayerArray[i].getUsername().compareTo(" ") != 0) { // As long as the object is not the initial value , display it;
System.out.println(NimPlayerArray[i].getUsername()+","+NimPlayerArray[i].getLastname()+","+
NimPlayerArray[i].getFirstname()+","
+NimPlayerArray[i].getGamesPlayed()+" games,"+NimPlayerArray[i].getGamesWon()+" wins");
}
}
}
private void resetPlayer(String username) {
for (int i =0; i < NimPlayerArray.length;i++) {
if (NimPlayerArray[i].UserNameequals(username)) {
NimPlayerArray[i].setPlayer(username,NimPlayerArray[i].getFirstname(),NimPlayerArray[i].getLastname());// As the mutator in the NimPlayer will set the games stats to the 0, it works!
return;
}
}
System.out.println("The player does not exist.");
}
private void resetPlayer() {
System.out.println("Are you sure you want to reset all player statistics? (y/n)");
String judge =keyboard.nextLine();
if (judge.equals("y"))
for (int i=0; i< NimPlayerArray.length;i++) {
NimPlayerArray[i].setPlayer(NimPlayerArray[i].getUsername(),NimPlayerArray[i].getFirstname(),NimPlayerArray[i].getLastname());
}
}
private void displayrank() {
for (int i =0; i < NimPlayerArray.length; i++) {
if (NimPlayerArray[i].UserNameequals(" "))//skip the initial value in
continue;
if (NimPlayerArray[i] != null) {
double vicper = ((double)NimPlayerArray[i].getGamesWon()/max(NimPlayerArray[i].getGamesPlayed(),1))*100;
System.out.printf("%-1.0f%%",vicper);
if (vicper==100) {
System.out.print(" | "); // for the different percentage returned , to keep the 5 characters spaces
}
else if(vicper<100 && vicper>= 10) {
System.out.print(" | ");
}
else {
System.out.print(" | ");
}
System.out.printf("%02d",NimPlayerArray[i].getGamesPlayed());
System.out.print(" games | "+
NimPlayerArray[i].getLastname()+" "+NimPlayerArray[i].getFirstname());
System.out.println();
}
}
}
private void rankings() {
InsertionsortVicDsc();
displayrank();
}
private void rankingsasc() {
InsertionsortVicAsc();
displayrank();
}
public static Scanner keyboard = new Scanner(System.in);
private void prompt() {
System.out.println();
System.out.print("$");
}
private void incorrectArgsNumber() throws Exception{
throw new Exception("Incorrect number of arguments supplied to command.");
}
private void record() {
try
{
ObjectInputStream record = new ObjectInputStream(new FileInputStream("player.dat"));
Object NimPlayerArrayrecord;
try
{
NimPlayerArrayrecord = record.readObject();
NimPlayerArray = (NimPlayer[]) NimPlayerArrayrecord;
}
catch(ClassNotFoundException e)
{
System.out.println("1");
}
catch(EOFException e)
{
System.out.println("2");
}
record.close();
}
catch(FileNotFoundException e)
{
}
catch (IOException e)
{
String message = e.getMessage();
System.out.println(message);
}
}
private void save() {
try
{
ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("player.dat"));
save.writeObject(NimPlayerArray);
save.flush();
save.close();
}
catch(IOException e)
{
String message = e.getMessage();
System.out.println(message);
}
}
public static void main(String[] args) { // the main method is to judge what your input is to return the corresponding command
System.out.println("Welcome to Nim");
Nimsys newNimsys = new Nimsys();
newNimsys.record();
while(true) {
newNimsys.prompt();
String commandtoDo = keyboard.nextLine();
String[] commandseq = commandtoDo.split(" ");
try {
if (commandseq[0].equals("exit"))
{
newNimsys.save();
System.out.println();
System.exit(0);
}
else if(commandseq[0].equals("startadvancedgame")) {
NimGame newNimGame = new NimGame();
String[] parameterSeq = commandseq[1].split(",");
if(parameterSeq.length == 3)
newNimGame.startadvancedgame(Integer.parseInt(parameterSeq[0]),parameterSeq[1], parameterSeq[2]);
else
newNimsys.incorrectArgsNumber();
}
else if(commandseq[0].equals("addplayer"))
{
if (commandseq.length == 2) {
String[] parameterSeq = commandseq[1].split(",");
if(parameterSeq.length == 3)
newNimsys.addPlayer(parameterSeq[0], parameterSeq[1], parameterSeq[2]);
else
newNimsys.incorrectArgsNumber();
}
}
else if(commandseq[0].equals("addaiplayer"))
{
if (commandseq.length == 2) {
String[] parameterSeq = commandseq[1].split(",");
if(parameterSeq.length == 3)
newNimsys.addAIPlayer(parameterSeq[0], parameterSeq[1], parameterSeq[2]);
else
newNimsys.incorrectArgsNumber();
}
}
else if(commandseq[0].equals("removeplayer"))
{
if (commandseq.length == 2)
newNimsys.removePlayer(commandseq[1]);
else
newNimsys.removePlayer();
}
else if(commandseq[0].equals("editplayer"))
{
if (commandseq.length ==2) {
String[] parameterSeq = commandseq[1].split(",");
if(parameterSeq.length == 3)
newNimsys.editPlayer(parameterSeq[0], parameterSeq[1], parameterSeq[2]);
else
newNimsys.incorrectArgsNumber();
}
}
else if(commandseq[0].equals("resetstats"))
{
if(commandseq.length == 2) {
newNimsys.resetPlayer(commandseq[1]);
}
else
newNimsys.resetPlayer();
}
else if(commandseq[0].equals("displayplayer"))
{
if(commandseq.length == 2) {
newNimsys.displayPlayer(commandseq[1]);
}
else
newNimsys.displayPlayer();
}
else if(commandseq[0].equals("rankings"))
{
if(commandseq.length == 2) {
if(commandseq[1].equals("desc")) {
newNimsys.rankings();
}
else if(commandseq[1].equals("asc")) {
newNimsys.rankingsasc();
}
}
else
newNimsys.rankings();
}
else if(commandseq[0].equals("startgame"))
{
if(commandseq.length == 2){
NimGame newNimGame = new NimGame();
String[] parameterSeq = commandseq[1].split(",");
if(parameterSeq.length == 4)
newNimGame.startGame(Integer.parseInt(parameterSeq[0]), Integer.parseInt(parameterSeq[1]), parameterSeq[2], parameterSeq[3]);
else
newNimsys.incorrectArgsNumber();
}
}
else
{
throw new Exception("'"+commandseq[0]+"' "+"is not a valid command.");
}
}
catch(Exception e) {
String message = e.getMessage( );
System.out.println(message);
}
}
}
}