-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOthello.java
363 lines (299 loc) · 8.6 KB
/
Othello.java
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
import java.util.Scanner;
/**
* Othello game controller. Allows for Human v Human or Human v Computer
* gameplay
*
* @author Chae Jubb
* @version 1.0
*
*/
public class Othello implements Game {
// instance variables
private Scanner input;
protected Player p1;
protected Player p2;
private Board board;
/**
* Constructor: Assigns scanner object to Othello object
*/
public Othello() {
input = new Scanner(System.in);
}
/**
* Plays game. Initialized game and continues, alternating turns until the
* game ends when no more valid moves are available
*
* @return Winning player. Null in case of tie.
*/
public Player playGame() {
//this.initialize(this.p1, this.p2);
Location attempt;
int currentPlayer = 1;
// repeat until game ends (method will return)
while (true) {
if (this.moveAvailable(currentPlayer)) {
System.out.println(this.board.toString());
System.out.println("Player " + currentPlayer + ": Your Turn");
if (currentPlayer == 1) {
attempt = p1.placeTile(false);
} else {
attempt = p2.placeTile(false);
}
// repeat until valid move supplied
boolean retry = false;
do {
if (this.isValid(attempt, currentPlayer)) {
this.executeMove(attempt, currentPlayer);
if (currentPlayer == 1) {
p2.setResult(attempt);
} else {
p1.setResult(attempt);
}
retry = false;
} else {
retry = true;
if (currentPlayer == 1) {
attempt = p1.placeTile(true);
} else {
attempt = p2.placeTile(true);
}
}
} while (retry);
// if other player has move available, prompt to make another
// move
} else if (this.moveAvailable(currentPlayer % 2 + 1)) {
System.out.println("Player " + currentPlayer
+ " has no legal Moves.");
if (currentPlayer == 1) {
p2.setResult(null);
} else {
p1.setResult(null);
}
} else {
// game is over with no legal moves left
System.out.println(this.board.toString());
System.out.println("GAME OVER!");
return this.scoreGame();
}
// switch players and continue
currentPlayer = (currentPlayer % 2) + 1;
}
}
/**
* Checks if attempted move is valid for supplied player identifier
*
* @param attempt
* Location of potential move
* @param identifier
* Identifier for player making the move
* @return true if valid move; false if invalid move
*/
private boolean isValid(Location attempt, int identifier) {
int hor = attempt.getHorizontal();
int vert = attempt.getVertical();
// check if piece already exists
if ((hor < Game.SIZE) && (hor >= 0) && (vert < Game.SIZE)
&& (vert >= 0)) {
if (this.board.getTile(hor, vert).getColor() != 0) {
return false;
}
}
// check that new piece flips old piece of other color
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (((hor + i) < Game.SIZE) && ((hor + i) >= 0)
&& ((vert + j) < Game.SIZE) && ((vert + j) >= 0)) {
// check if one of paths has other color
if (this.board.getTile(hor + i, vert + j).getColor() == (identifier % 2 + 1)) {
int k = 2;
boolean found = false;
boolean end = false;
// move in all 8 directions out from attempted piece
// placement
while (k < Game.SIZE && !found && !end) {
// check next position to be checked in on the board
if (((hor + k * i) < Game.SIZE)
&& ((hor + k * i) >= 0)
&& ((vert + k * j) < Game.SIZE)
&& ((vert + k * j) >= 0)) {
// check for piece of own color
if (this.board.getTile(hor + k * i,
vert + k * j).getColor() == identifier) {
found = true;
return true;
} else if (this.board.getTile(hor + k * i,
vert + k * j).getColor() == 0) {
end = true;
}
}
k++;
}
}
}
}
}
// if above conditions not met, return invalid
return false;
}
/**
* Places piece at specified location and flips necessary pieces
*
* @param attempt
* Location of move
* @param identifier
* Identifier for player making move
*/
private void executeMove(Location attempt, int identifier) {
int hor = attempt.getHorizontal();
int vert = attempt.getVertical();
int oppID = (identifier % 2) + 1;
this.board.getTile(hor, vert).setColor(identifier);
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
// make sure direction is on board
if (((hor + i) < Game.SIZE) && ((hor + i) >= 0)
&& ((vert + j) < Game.SIZE) && ((vert + j) >= 0)) {
// check if one of paths has other color
if (this.board.getTile(hor + i, vert + j).getColor() == oppID) {
int k = 2;
boolean found = false;
boolean end = false;
// move in all 8 directions out from attempted piece
// placement
while ((k < Game.SIZE) && (!found) && (!end)) {
// check next position to be checked in on the board
if (((hor + k * i) < Game.SIZE)
&& ((hor + k * i) >= 0)
&& ((vert + k * j) < Game.SIZE)
&& ((vert + k * j) >= 0)) {
// check for piece of own color
if (this.board.getTile(hor + k * i,
vert + k * j).getColor() == identifier) {
// entering this portion of the loop means
// pieces are being "flipped"
for (int l = 1; l < k; l++) {
this.board.getTile(hor + l * i,
vert + l * j).setColor(
identifier);
}
found = true;
} else if (this.board.getTile(hor + k * i,
vert + k * j).getColor() == 0) {
end = true;
}
}
k++;
}
}
}
}
}
}
/**
* Checks if there exists a legal move on the board
*
* @param identifier
* Identifier for player who is attempting to move
* @return true if move available; false otherwise
*/
private boolean moveAvailable(int identifier) {
boolean found = false;
int i = 0;
int j = 0;
while (!found && i < Game.SIZE) {
j = 0;
while (!found && j < Game.SIZE) {
found = this.isValid(new Tile(i, j), identifier);
j++;
// System.out.println(found);
}
i++;
}
return found;
}
/**
* Sets up players and board according to user input from command line
*
* @param p1
* First player of game
* @param p2
* Second player of game
*/
public void initialize(Player p1, Player p2) {
// setup Players: H v. H or H v. C
System.out.println("Welcome. Choose Type of Game");
System.out.println("1. Human v. Human");
System.out.println("2. Human v. Computer");
System.out.println("3. Watch Dumb Player v. Computer");
System.out.println("4. Watch Computer v. Computer");
int choice;
boolean valid = false;
do {
try {
choice = input.nextInt();
if (choice == 1) {
this.p1 = new HumanPlayer(1);
this.p2 = new HumanPlayer(2);
valid = true;
} else if (choice == 2) {
this.p1 = new HumanPlayer(1);
this.p2 = new CompPlayer(2);
System.out
.println("Player 1 is Human; Player 2 is Computer");
valid = true;
} else if (choice == 3) {
this.p1 = new DumbPlayer(1);
this.p2 = new CompPlayer(2);
valid = true;
} else if (choice == 4) {
this.p1 = new CompPlayer(1);
this.p2 = new CompPlayer(2);
valid = true;
} else {
System.out.println("Bad Number");
System.out.println("Choose Type of Game");
System.out.println("1. Human v. Human");
System.out.println("2. Human v. Computer");
}
input.nextLine(); // eat line break
} catch (Exception e) {
System.out.println("Not a Number");
System.out.println("Choose Type of Game");
System.out.println("1. Human v. Human");
System.out.println("2. Human v. Computer");
input.nextLine();
}
} while (!valid);
// setup Board, including initial tiles
this.board = new Board(Game.SIZE, Game.SIZE);
this.board.setUpBoard();
}
/**
* Computes score of game by counting number of pieces on board
*
* @return Winning player. Null if tie.
*/
public Player scoreGame() {
int p1Counter = 0;
int p2Counter = 0;
// count tiles for each player
for (int i = 0; i < Game.SIZE; i++) {
for (int j = 0; j < Game.SIZE; j++) {
if (this.board.getTile(i, j).getColor() == 1) {
p1Counter++;
} else if (this.board.getTile(i, j).getColor() == 2) {
p2Counter++;
}
}
}
System.out.println("Player 1 Score: " + p1Counter);
System.out.println("Player 2 Score: " + p2Counter);
if (p1Counter > p2Counter) {
return this.p1;
} else if (p2Counter > p1Counter) {
return this.p2;
} else {
return null;
}
}
}