-
Notifications
You must be signed in to change notification settings - Fork 0
/
Othello.java
executable file
·419 lines (414 loc) · 12.7 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
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
// Othello Applet (graphics code adapted from Postfix Calculator Applet
// 12/M3/2007
// CS 201 Final Project - Kazuaki Okumura & Michael Luby
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Othello extends Applet implements ActionListener {
//-----------------------------Initialization------------------------------------------
//***numRanks and numFiles control the dimensions of the gameboard
//***the four starting pieces are initialized in "initializeGame()"
//***the computer player can be set to black(1), white(2) or off(0)
//***by changing the integer value in "if(currentPlayer == 0)" in "placeTile()"
// instance variables
Label player1sum; // label used to show player 1's total tiles
Label gameState; // label used to show game state
Label player2sum; // label used to show player 2's total tiles
int numRanks = 8; // ranks are horizontal
int numFiles = 8; // files are vertical
int currentPlayer; //1 = black, 2 = white
int boardArray[][] = new int[numRanks][numFiles];
int tempBoard[][] = new int[numRanks][numFiles];
boolean flippable;
Panel centerPanel = new Panel();
// local color constants
static final Color black = Color.black;
static final Color white = Color.white;
static final Color green = Color.green;
static final Color dgreen = new Color(0, 120, 90);
//-------------------------------------------------------------------------------------
public void init()
//code to set up the applet, including layout
{
setLayout(new BorderLayout());
setBackground(black);
add("North", gameStatePanel());
add("Center", board());
initializeGame();
}
//-------------------------------------------------------------------------------------
public Panel gameStatePanel()
//dynamic text fields that display the state of the game (in progress, black won,
// white won, draw) and sum of player 1's tiles, sum of player 2's tiles
{
Panel resultPanel = new Panel();
resultPanel.setBackground(dgreen);
resultPanel.setLayout(new GridLayout(1, 3));
//displays the sum of player 1 (black)'s tiles in the upper left
player1sum = new Label("Black: ", Label.LEFT);
player1sum.setFont(new Font("TimesRoman", Font.BOLD, 30));
player1sum.setBackground(white);
player1sum.setForeground(black);
resultPanel.add("", player1sum);
//displays game state in upper center: in progress, black won, white won, or draw)
gameState = new Label("Black's Turn", Label.CENTER);
gameState.setFont(new Font("TimesRoman", Font.BOLD, 30));
gameState.setBackground(dgreen);
gameState.setForeground(green);
resultPanel.add("", gameState);
//displays the sum of player 2 (white)'s tiles in the upper right
player2sum = new Label("White: ", Label.RIGHT);
player2sum.setFont(new Font("TimesRoman", Font.BOLD, 30));
player2sum.setBackground(black);
player2sum.setForeground(white);
resultPanel.add("", player2sum);
return resultPanel;
}
//-------------------------------------------------------------------------------------
public Panel board()
//creates the main board
{
centerPanel.setLayout(new GridLayout(numRanks, numFiles));
for(int i = 0; i < numRanks; i++)
{
for(int j = 0; j < numFiles; j++)
{
boardArray[i][j] = 0;
centerPanel.add(CButton(""+(i)+(j), dgreen, dgreen));
}
}
return centerPanel;
}
//-------------------------------------------------------------------------------------
public void initializeGame()
//places the starting four pieces, and sets currentPlayer = 1 (black)
{
currentPlayer = 1;
boardArray[3][3] = 1;
boardArray[3][4] = 2;
boardArray[4][3] = 2;
boardArray[4][4] = 1;
sumTiles(1);
sumTiles(2);
rePaint();
}
//-----------------------------Button Methods------------------------------------------
//-------------------------------------------------------------------------------------
public void actionPerformed(ActionEvent e)
//This method handles button clicks
{
if (e.getSource() instanceof Button)
{
String label = ((Button)e.getSource()).getLabel();
char c = label.charAt(0);
char k = label.charAt(1);
int rank = Character.getNumericValue(c);
int file = Character.getNumericValue(k);
if(boardArray[rank][file]==0)
{
placeTile(rank,file);
}
}
}
//-------------------------------------------------------------------------------------
protected Button CButton(String s, Color fg, Color bg)
//this method creates colored buttons
{
Button b = new Button(s);
b.setBackground(bg);
b.setForeground(fg);
b.addActionListener(this);
return b;
}
//-----------------------------Functionality Methods-----------------------------------
//-------------------------------------------------------------------------------------
public void placeTile(int rank, int file )
//places a tile (black or white) depending on the currentPlayer)
//at position (rank, file), and toggles currentPlayer
{
if(moveExists(rank,file))
{
boardArray[rank][file] = currentPlayer;
flipTilesFrom(rank, file);
rePaint();
sumTiles(currentPlayer);
checkVictory();
togglePlayer();
}
if(currentPlayer == 0)
{ computerPlay(); }
}
//-------------------------------------------------------------------------------------
public void rePaint()
{
for(int i = 0; i < numRanks; i++)
{
for(int j = 0; j < numFiles; j++)
{
if(boardArray[i][j]==1)
{ centerPanel.getComponent(i*numRanks+j).setBackground(black);
centerPanel.getComponent(i*numRanks+j).setForeground(black); }
if(boardArray[i][j]==2)
{ centerPanel.getComponent(i*numRanks+j).setBackground(white);
centerPanel.getComponent(i*numRanks+j).setForeground(white); }
}
}
}
//-------------------------------------------------------------------------------------
public void togglePlayer()
{
if( currentPlayer==1 )
{
currentPlayer = 2;
gameState.setText("White's Turn");
}
else
{
currentPlayer = 1;
gameState.setText("Black's Turn");
}
}
//-------------------------------------------------------------------------------------
public void checkVictory()
//if the game is over, declare a winner (or a draw)
{
if( sumTiles(1)+sumTiles(2) == numRanks*numFiles )
{//Game Over
if(sumTiles(1)==sumTiles(2))
{ gameState.setText("Draw..."); }
else if(sumTiles(1)>sumTiles(2))
{ gameState.setText("Black Wins!"); }
else
{ gameState.setText("White Wins!"); }
}
}
//-------------------------------------------------------------------------------------
public int sumTiles(int currentPlayer)
//determine the total number of tiles of currentPlayer's color on the board
{
int playerSum = 0;
for(int i = 0; i < numRanks; i++)
{
for(int j = 0; j < numFiles; j++)
{
if( boardArray[i][j] == currentPlayer )
{ playerSum++; }
}
}
if(currentPlayer == 1)
{ player1sum.setText("Black: " + playerSum ); }
else
{ player2sum.setText("White: " + playerSum ); }
return playerSum;
}
//-------------------------------------------------------------------------------------
public boolean moveExists(int rank, int file)
// checks to see if the currentPlayer has a valid move, IE placing a piece would
// result in at least one flip for currentPlayer
{
for(int i = 0; i < numRanks; i++)
{
for(int j = 0; j < numFiles; j++)
{ tempBoard[i][j] = boardArray[i][j]; }
}
flippable = false;
flipTilesFrom(rank, file);
for(int i = 0; i < numRanks; i++)
{
for(int j = 0; j < numFiles; j++)
{ boardArray[i][j] = tempBoard[i][j]; }
}
if(flippable == true)
{
return true;
}
else
{
return false;
}
}
//-------------------------------------------------------------------------------------
public void computerPlay()
//searches (exhaustively) for greatest number of tiles flipped, then makes that move
{
int originalSum = sumTiles(currentPlayer);
int bestGain = 0; // best currentSum-originalSum
int bestRank = 0; // best move rank
int bestFile = 0; // best move file
for(int i = 0; i < numRanks; i++)
{
for(int j = 0; j < numFiles; j++)
{
//create temp board from original board
for(int k = 0; k < numRanks; k++)
{
for(int l = 0; l < numFiles; l++)
{ tempBoard[k][l] = boardArray[k][l]; }
}
//hypothetical: more tiles flipped by playing here?
flipTilesFrom(i,j);
if( sumTiles(currentPlayer)-originalSum > bestGain)
{
bestGain = sumTiles(currentPlayer)-originalSum;
bestRank = i;
bestFile = j;
}
//restore original board from temp board
for(int m = 0; m < numRanks; m++)
{
for(int n = 0; n < numFiles; n++)
{ boardArray[m][n] = tempBoard[m][n]; }
}
}
}
placeTile(bestRank, bestFile);
}
//-------------------------------------------------------------------------------------
public void flipTilesFrom(int rank, int file)
//toggles a tile's color at position (rank, file)
{
int newRank;
int newFile;
//Flip Eastward
newRank = rank;
newFile = file + 1;
while(newRank!=numRanks && newFile!=numFiles && newRank!=-1 && newFile!=-1
&& boardArray[rank][newFile]!=currentPlayer && boardArray[rank][newFile]!= 0)
{
newFile=newFile+1;
if(boardArray[rank][newFile]==currentPlayer)
{
for(int i = newFile-1; i != file; i--)
{
boardArray[rank][i] = currentPlayer;
flippable = true;
}
}
}
//Flip Southward
newRank=rank+1;
newFile=file;
while(newRank!=numRanks && newFile!=numFiles && newRank!=-1 && newFile!=-1
&& boardArray[newRank][file]!=currentPlayer && boardArray[newRank][file]!= 0)
{
newRank=newRank+1;
if(boardArray[newRank][file]==currentPlayer)
{
for(int i = newRank-1; i != rank; i--)
{
boardArray[i][file] = currentPlayer;
flippable = true;
}
}
}
//Flip Westward
newRank = rank;
newFile = file - 1;
while(newRank!=numRanks && newFile!=numFiles && newRank!=-1 && newFile!=-1
&& boardArray[rank][newFile]!=currentPlayer && boardArray[rank][newFile]!= 0)
{
newFile=newFile-1;
if(boardArray[rank][newFile]==currentPlayer)
{
for(int i = newFile+1; i != file; i++)
{
boardArray[rank][i] = currentPlayer;
flippable = true;
}
}
}
//Flip Northward
newRank=rank-1;
newFile=file;
while(newRank!=numRanks && newFile!=numFiles && newRank!=-1 && newFile!=-1
&& boardArray[newRank][file]!=currentPlayer && boardArray[newRank][file]!= 0)
{
newRank=newRank-1;
if(boardArray[newRank][file]==currentPlayer) //<-- Occasionally OutOfBounds
{
for(int i = newRank+1; i != rank; i++)
{
boardArray[i][file] = currentPlayer;
flippable = true;
}
}
}
//Flip Southeastward
//South East
newRank = rank + 1;
newFile = file + 1;
while(newRank<numRanks && newFile<numFiles && newRank>=1 && newFile>=1 &&
boardArray[newRank][newFile]!=currentPlayer && boardArray[newRank][newFile]!= 0)
{
newRank=newRank+1;
newFile=newFile+1;
gameState.setText("e@"+newRank+","+newFile);
if(boardArray[newRank][newFile]==currentPlayer) //<-- Occasionally OutOfBounds
{
for(int i = newRank-1; i != rank; i--)
{
newFile = newFile-1;
boardArray[i][newFile] = currentPlayer;
flippable = true;
}
}
}
//Flip Northeastward
newRank = rank - 1;
newFile = file + 1;
while(newRank<numRanks && newFile<numFiles && newRank>=1 && newFile>=1 &&
boardArray[newRank][newFile]!=currentPlayer && boardArray[newRank][newFile]!= 0)
{
newRank=newRank-1;
newFile=newFile+1;
gameState.setText("e@"+newRank+","+newFile);
if(boardArray[newRank][newFile]==currentPlayer) //<-- Occasionally OutOfBounds
{
for(int i = newRank+1; i != rank; i++)
{
newFile = newFile-1;
boardArray[i][newFile] = currentPlayer;
flippable = true;
}
}
}
//Flip Northwestward
newRank = rank - 1;
newFile = file - 1;
while(newRank<numRanks && newFile<numFiles && newRank>=1 && newFile>=1 &&
boardArray[newRank][newFile]!=currentPlayer && boardArray[newRank][newFile]!= 0)
{
newRank=newRank-1;
newFile=newFile-1;
gameState.setText("e@"+newRank+","+newFile);
if(boardArray[newRank][newFile]==currentPlayer) //<-- Occasionally OutOfBounds
{
for(int i = newRank+1; i != rank; i++)
{
newFile = newFile+1;
boardArray[i][newFile] = currentPlayer;
flippable = true;
}
}
}
//Flip Southwestward
newRank = rank + 1;
newFile = file - 1;
while(newRank<numRanks && newFile<numFiles && newRank>=1 && newFile>=1 &&
boardArray[newRank][newFile]!=currentPlayer && boardArray[newRank][newFile]!= 0)
{
newRank=newRank+1;
newFile=newFile-1;
gameState.setText("e@"+newRank+","+newFile);
if(boardArray[newRank][newFile]==currentPlayer)
{
for(int i = newRank-1; i != rank; i--)
{
newFile = newFile+1;
boardArray[i][newFile] = currentPlayer;
flippable = true;
}
}
}
}
} //-----------------------------end-------------------------------------------