-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNimGame.java
More file actions
310 lines (220 loc) · 7.51 KB
/
Copy pathNimGame.java
File metadata and controls
310 lines (220 loc) · 7.51 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
/************************************************************
* 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
*
*
*
************************************************************/
public class NimGame {
private int stonenum;
private int bound;
private NimPlayer Player1;
private NimPlayer Player2;
public NimGame() {
stonenum = 0;
bound = 0;
Player1 = new NimHumanPlayer();
Player2 = new NimHumanPlayer();
}
public void startGame(int stonenum, int bound, String Player1username, String Player2username) {
if (Nimsys.existPlayer(Player1username) == (-1) || Nimsys.existPlayer(Player2username) == (-1)) {// judge the Player existence in the array
System.out.println("One of the players does not exist.");
return;
}
this.stonenum =stonenum;
this.bound = bound;
this.Player1 = Nimsys.getPlayer(Nimsys.existPlayer(Player1username));
this.Player2 = Nimsys.getPlayer(Nimsys.existPlayer(Player2username));
System.out.println();
System.out.println("Initial stone count: "+this.stonenum);
System.out.println("Maximum stone removal: "+this.bound);
System.out.println("Player 1: "+Player1.getLastname()+" "+Player1.getFirstname());
System.out.println("Player 2: "+Player2.getLastname()+" "+Player2.getFirstname());
while(this.stonenum>=0)
{
gameprocess(Player1, Player2);
if (this.stonenum == 0)
break;
gameprocess(Player2, Player1);
if (this.stonenum == 0)
break;
}
}
private int min(int a,int b) {
if(a>=b) {
return b;
}
else {
return a;
}
}
private void gameprocess(NimPlayer Player, NimPlayer otherPlayer)
{
while (true) {
System.out.println();
System.out.print(stonenum+" stones left:");
for (int sta = 1; sta<=stonenum; sta++)// print the asterik
System.out.print(" *");
System.out.println();
System.out.println(Player.getLastname() +"'s turn - remove how many?");
if (Player instanceof NimHumanPlayer) {
String movedStoneSt = Nimsys.keyboard.nextLine();
int movedStone = Integer.parseInt(movedStoneSt);// get the number of the moved stones
try {
if ((movedStone<=bound)&&(movedStone>0)) {
if (movedStone<=stonenum) {
stonenum = stonenum-Player.removeStone(movedStone,stonenum,bound);
break;
}
}
else {
System.out.println();
throw new Exception("Invalid move. You must remove between 1 and "+min(bound,stonenum)+" stones.");// print warning informaiton if out of bound
}
}
catch(Exception e) {
String message = e.getMessage( );
System.out.println(message);
}
}
else {
Player = (NimAIPlayer)Player;
stonenum = stonenum - Player.removeStone(0,min(bound,stonenum), stonenum);
break;
}
}
if (stonenum == 0) { // if there is no stone left, end the loop and end the game. Judge the winner and add the number related.
System.out.println();
System.out.println("Game Over");
System.out.println(otherPlayer.getLastname()+" "+otherPlayer.getFirstname()+" wins!");
otherPlayer.winGame();
Player.loseGame();
}
}
public void startadvancedgame(int initialstones , String Player1username, String Player2username )
{
if (Nimsys.existPlayer(Player1username) == (-1) || Nimsys.existPlayer(Player2username) == (-1)) {// judge the Player existence in the array
System.out.println("One of the players does not exist.");
return;
}
this.stonenum = initialstones;
this.Player1 = Nimsys.getPlayer(Nimsys.existPlayer(Player1username));
this.Player2 = Nimsys.getPlayer(Nimsys.existPlayer(Player2username));
boolean[] stones = new boolean[initialstones];
for (int i = 0; i < initialstones; i++) {
stones[i] = true;
}
System.out.println();
System.out.println("Initial stone count: "+this.stonenum);
System.out.print("Stones display:");
int index = 1;
for (int i = 0; i < initialstones; i++) {
if(stones[i]) {
System.out.print(" <" + index + ",*>");
index++;
}
}
System.out.println();
System.out.println("Player 1: "+Player1.getLastname()+" "+Player1.getFirstname());
System.out.println("Player 2: "+Player2.getLastname()+" "+Player2.getFirstname());
String movedStoneSt =" ";
while(allfalse(stones,stonenum)) {
gameprocessadvanced(Player1,Player2,stones,movedStoneSt) ;
if(!allfalse(stones,stonenum))
break;
gameprocessadvanced(Player2,Player1,stones,movedStoneSt) ;
if(!allfalse(stones,stonenum))
break;
}
}
private void stoneDisplay(boolean[] thelist){
int count = 0;
for (int i = 0; i < stonenum; i++) {
if (thelist[i])
count = count + 1;
}
System.out.print(count + " stones left:");
int index = 1;
for (int i = 0; i < stonenum; i++){
if(thelist[i])
{
System.out.print(" <" + index + ",*>");
}
else
{
System.out.print(" <" + index + ",x>");
}
index++;
}
System.out.println();
}
private void gameprocessadvanced(NimPlayer Player, NimPlayer losingPlayer, boolean[] stones,String movedStoneSt)
{ boolean done = false;
while(!done)
{
System.out.println();
stoneDisplay(stones);
System.out.println(Player.getLastname()+"'s turn - which to remove?");
if (Player instanceof NimHumanPlayer) {
try
{
movedStoneSt = Nimsys.keyboard.nextLine();
String[] movdeStoneStpara = movedStoneSt.split(" ");
if (movdeStoneStpara[1].equals("1"))
{
if (stones[Integer.parseInt(movdeStoneStpara[0])-1]) {
stones[Integer.parseInt(movdeStoneStpara[0])-1] = false;
done = true;
}
else
throw new Exception();
}
else if (movdeStoneStpara[1].equals("2"))
{
if (stones[Integer.parseInt(movdeStoneStpara[0])-1] &&
stones[Integer.parseInt(movdeStoneStpara[0])])
{
stones[Integer.parseInt(movdeStoneStpara[0])-1] = false;
stones[Integer.parseInt(movdeStoneStpara[0])] = false;
done = true;
}
else
throw new Exception();
}
else
throw new Exception();
}
catch(Exception e)
{
System.out.println();
System.out.println("Invalid move.");
}
}
if(Player instanceof NimAIPlayer) {
}
}
for (int i = 0; i < stonenum; i++)
{
if (stones[i])
return;
}
System.out.println();
System.out.println("Game Over");
System.out.println(Player.getLastname()+" "+Player.getFirstname()+" wins!");
Player.winGame();
losingPlayer.loseGame();
}
private boolean allfalse(boolean[] stones, int index)
{
for (int i = 0; i < index; i++) {
if (stones[i])
return true;
}
return false;
}
}