-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticTacToe.java
583 lines (521 loc) · 17.2 KB
/
ticTacToe.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
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
//Ken Schroeder
// August 2nd, 2015, 1101
// Starbucks, 5th Ave Portland, OR
// TicTacToe
// ticTacToe.java
/*------------------------------------------------------------------------------
Tic-Tac-Toe:
Program Purpose:
This program is intended to create and maintain the board for a game of
tic-tac-toe. It checks for legal moves and keeps track of won/drawn games.
Much of the code for this program was taken from a project I did at Seattle
University in August 2014.
Instructions:
This program is a class designed to be run with playTicTacToe.java and
CPUPlayer.java. With a few simple modifiations it could be run without the
CPU.
Program Includes The Following Methods
displayBoard -> Prints the current state of the board to the screen
resetBoard -> Clears the board and readies it for a new game
gameStats -> Displays the runnin game stats
welcome -> Displays a friendly welcome message to the user
goodBye -> Displays a friendly good by message to the user
placePiece -> Places a piece on a board, if possible.
gameOver -> Checks if the game has ended
boardFilled -> checks if the board is filled, if it is, game is a draw
getXPiece -> returns the character of the x piece
getOPiece -> returns the character of the o piece
getXMove -> returns the numeric value of the x player
getOMove -> returns the numeric value of the o player
isFree -> determines if a row, column pair is free
getBoardSize -> returns size of board in rows
getBoard -> returns the game board
gameWon -> returns whether the game has been won or not
Implementation and Assumptions:
The board is an n x n array of ints
When a player plays a piece, that int value is stored in the array
When printing the board, number values are turned into characters
Game uses number values to check for when players have won or have a
potential win
Future Development Ideas
-Copy constructor
-Better evaluation function
References
http://neverstopbuilding.com/minimax
http://stackoverflow.com/questions/17907255/make-the-computer-never-lose-at-tic-tac-toe
----------------------------------------------------------------------------*/
public class ticTacToe {
private final String SEPERATOR = "-----";
private final int boardSize = 3;
private boolean gameWon = false;
private int[][] board;
private int xWins;
private int draws;
private int oWins;
private char xPiece = 'X';
private char oPiece = 'O';
private int xMove = 5;
private int oMove = 7;
private boolean firstMoveX = false;
private int turn = 0;
private int totalMoves;
private ticTacToe masterGame;
//----------------------------------------------------------------------------
// constructor
//----------------------------------------------------------------------------
public ticTacToe(){
xWins = 0;
draws = 0;
oWins = 0;
board = new int[boardSize][boardSize];
masterGame = this;
resetBoard();
}
public void copy(ticTacToe t){
turn = t.turn;
totalMoves = t.totalMoves;
masterGame = t.masterGame;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
board[i][j] = t.getBoardVal(i, j);
}
}
}
//----------------------------------------------------------------------------
// getXPiece
// getter function for XPiece variable
//----------------------------------------------------------------------------
public char getXPiece(){
return xPiece;
}
//----------------------------------------------------------------------------
// getOPiece
// getter function for OPiece variable
//----------------------------------------------------------------------------
public char getOPiece(){
return oPiece;
}
//----------------------------------------------------------------------------
// getXMove
// getter function for XMove variable
//----------------------------------------------------------------------------
public int getXMove(){
return xMove;
}
//----------------------------------------------------------------------------
// get OMove
// getter function for YMove variable
//----------------------------------------------------------------------------
public int getOMove(){
return oMove;
}
//----------------------------------------------------------------------------
// getBoardSize
// getter function for boardSize variable
//----------------------------------------------------------------------------
public int getBoardSize(){
return boardSize;
}
//----------------------------------------------------------------------------
// getBoard
// returns the board, used for CPUPlayer class
//----------------------------------------------------------------------------
public int[][] getBoard(){
return board;
}
//----------------------------------------------------------------------------
// isWinner
// getter function for gameWon variable
//----------------------------------------------------------------------------
public boolean isWinner(){
return gameWon;
}
//----------------------------------------------------------------------------
// welcome
// prints a welcome message to the screen
//----------------------------------------------------------------------------
public void welcome(){
System.out.println("Welcom to the game of tic-tac-toe");
System.out.println("The object is to get three ina row");
System.out.println("If both players are smart, and shart in the eye");
System.out.println("I regret to say, this will end in a tie");
System.out.println();
System.out.println("Player 1, it's your turn!\n\n");
}
//----------------------------------------------------------------------------
// goodBye
// prints a farewell message to the screen
//----------------------------------------------------------------------------
public void goodBye(){
gameStats();
System.out.println("Thanks for playing! Good bye!");
}
//----------------------------------------------------------------------------
// displayBoard
// displays the current state of the board
// numeric values are turned into X's and O's for display
//----------------------------------------------------------------------------
public void displayBoard(){
System.out.println("Total Moves: " + totalMoves);
System.out.print(" " + "| ");
for (int k = 0; k < boardSize; k++){
System.out.print(k + " | ");
}
System.out.println();
//System.out.println(EMPTY + EMPTY);
for (int w = 0; w < boardSize; w++){
System.out.print(SEPERATOR);
}
System.out.println();
for (int i = 0; i < boardSize; i++){
System.out.print(i + " |");
for (int j = 0; j < boardSize; j++){
if(board[i][j] == xMove){
System.out.print(" " + xPiece + " |");
}
else if(board[i][j] == oMove){
System.out.print(" " + oPiece + " |");
}
else{
System.out.print(" |");
}
}
System.out.println();
for (int h = 0; h < boardSize; h++){
System.out.print(SEPERATOR);
}
System.out.println();
}
int position = evaluate();
System.out.println("\nCurrent Position: " + position);
}
//----------------------------------------------------------------------------
// resetBoard
// clears the board
//----------------------------------------------------------------------------
public void resetBoard(){
gameWon = false;
if (firstMoveX){
firstMoveX = false;
turn = oMove;
}
else{
firstMoveX = true;
turn = xMove;
}
totalMoves = 0;
for (int i = 0; i < boardSize; i++){
for (int j = 0; j < boardSize; j++){
board[i][j] = 1;
}
}
}
//----------------------------------------------------------------------------
// gameStats
// prints the game stats, wins, draws, losses and win% for player 1
//----------------------------------------------------------------------------
public void gameStats(){
double winPerc = (double)(2*xWins + draws) / (double)(2* (xWins + draws + oWins));
System.out.println("\n");
System.out.println(" GAME STATISTICS");
System.out.println("--------------------------");
System.out.println("Player 1 Wins: " + xWins);
System.out.println("Games Drawn: " + draws);
System.out.println("Player 1 Losses: " + oWins);
System.out.print("Win Percentage: ");
System.out.printf("%.3f", winPerc);
System.out.println();
System.out.println("--------------------------\n");
}
//----------------------------------------------------------------------------
// placePiece
// attempts to place a piece on the board, returns false if space is occupied.
//----------------------------------------------------------------------------
public boolean placePiece(int piece, int row, int col){
if (row >= boardSize || col >= boardSize || row < 0 || col < 0){
return false;
}
if(board[row][col] == 1){
board[row][col] = piece;
totalMoves++;
if(turn == xMove){
turn = oMove;
}else{
turn = xMove;
}
return true;
}else{
System.out.print("That square is already occupied, please select ");
System.out.print("another square");
return false;
}
}
//----------------------------------------------------------------------------
// gameOver
// checks if the game is over, the game is over if there are no places left to
// play, or if a player has 3 in a row
//----------------------------------------------------------------------------
public boolean gameOver(int piece){
int product;
for(int i = 0; i < boardSize; i++){
product = rowProduct(i);
if(product == Math.pow(piece,boardSize)){
wonGame(piece);
return true;
}
product = colProduct(i);
if(product == Math.pow(piece,boardSize)){
wonGame(piece);
return true;
}
}
product = upperLeftDiagProduct();
if(product == Math.pow(piece,boardSize)){
wonGame(piece);
return true;
}
product = upperRightDiagProduct();
if(product == Math.pow(piece,boardSize)){
wonGame(piece);
return true;
}
if(boardFilled()){
draws++;
System.out.println("It's a draw");
return true;
}
return false;
}
//----------------------------------------------------------------------------
// boardFilled
// returns true if all spaces on the board are filled
//----------------------------------------------------------------------------
public boolean boardFilled(){
return totalMoves == 9;
}
//----------------------------------------------------------------------------
// getBoardVal(int,int)
// returns value of row column
//----------------------------------------------------------------------------
public int getBoardVal(int row, int col){
return board[row][col];
}
//----------------------------------------------------------------------------
// isFree
// returns true if a position on the board is free
//----------------------------------------------------------------------------
public boolean isFree(int row, int col){
if (board[row][col] == 1){
System.out.println("(" + row + "," + col + ") is free.");
return true;
}
return false;
}
//----------------------------------------------------------------------------
// rowProduct
// returns the product of a row
// if the product == the piece cubed, the game has been won
// if the product == the piece squared, the game has the potential to be won
//----------------------------------------------------------------------------
public int rowProduct(int row){
int product = 1;
for (int i = 0; i < boardSize; i++){
product *= board[row][i];
}
return product;
}
//----------------------------------------------------------------------------
// colProduct
// returns the product of a column
// if the product == the piece cubed, the game has been won
// if the product == the piece squared, the game has the potential to be won
//----------------------------------------------------------------------------
public int colProduct(int col){
int product = 1;
for (int i = 0; i < boardSize; i++){
product *= board[i][col];
}
return product;
}
//----------------------------------------------------------------------------
// upperLeftDiagProduct
// returns the product of the upper left to bottom right diagonal
// if the product == the piece cubed, the game has been won
// if the product == the piece squared, the game has the potential to be won
//----------------------------------------------------------------------------
public int upperLeftDiagProduct(){
int product = 1;
for (int i = 0; i < boardSize; i++){
product *= board[i][i];
}
return product;
}
//----------------------------------------------------------------------------
// upperRightDiagProduct
// returns the product of the upper right to bottom left diagonal
// if the product == the piece cubed, the game has been won
// if the product == the piece squared, the game has the potential to be won
//----------------------------------------------------------------------------
public int upperRightDiagProduct(){
int product = 1;
for (int i = 0; i < boardSize; i++){
product *= board[2-i][i];
}
return product;
}
//----------------------------------------------------------------------------
// wonGame
// Marks a game as won
//----------------------------------------------------------------------------
private void wonGame(int piece){
if (piece == xMove){
xWins++;
}
else{
oWins++;
}
gameWon = true;
}
//----------------------------------------------------------------------------
// evaluate
// evalute a position
// future improvement: quick wins are better
// future imporvement: assume trouble is better
//----------------------------------------------------------------------------
public int evaluate(){
int position = 0;
int oneInARow = 3;
int twoInARow = 10;
int threeInARow = 100;
int fork = 50;
for (int i = 0; i < boardSize; i++){
if(rowProduct(i) == xMove){
position += oneInARow;
}
if(rowProduct(i) == Math.pow(xMove,2)){
position += twoInARow;
}
if(rowProduct(i) == Math.pow(xMove,3)){
return threeInARow;
}
if(rowProduct(i) == oMove){
position -= oneInARow;
}
if(rowProduct(i) == Math.pow(oMove,2)){
position -= twoInARow;
}
if(rowProduct(i) == Math.pow(oMove,3)){
return 0 - threeInARow;
}
if(colProduct(i) == xMove){
position += oneInARow;
}
if(colProduct(i) == Math.pow(xMove,2)){
position += twoInARow;
}
if(colProduct(i) == Math.pow(xMove,3)){
return threeInARow;
}
if(colProduct(i) == oMove){
position -= oneInARow;
}
if(colProduct(i) == Math.pow(oMove,2)){
position -= twoInARow;
}
if(colProduct(i) == Math.pow(oMove,3)){
return 0-threeInARow;
}
}
if(upperLeftDiagProduct() == xMove){
position += oneInARow;
}
if(upperLeftDiagProduct() == Math.pow(xMove,2)){
position += twoInARow;
}
if(upperLeftDiagProduct() == Math.pow(xMove,3)){
return threeInARow;
}
if(upperLeftDiagProduct() == oMove){
position -= oneInARow;
}
if(upperLeftDiagProduct() == Math.pow(oMove,2)){
position -= twoInARow;
}
if(upperLeftDiagProduct() == Math.pow(oMove,3)){
return 0 - threeInARow;
}
if(upperRightDiagProduct() == xMove){
position += oneInARow;
}
if(upperRightDiagProduct() == Math.pow(xMove,2)){
position += twoInARow;
}
if(upperRightDiagProduct() == Math.pow(xMove,3)){
return threeInARow;
}
if(upperRightDiagProduct() == oMove){
position -= oneInARow;
}
if(upperRightDiagProduct() == Math.pow(oMove,2)){
position -= twoInARow;
}
if(upperRightDiagProduct() == Math.pow(oMove,3)){
return 0 - threeInARow;
}
return position;
}
//----------------------------------------------------------------------------
// placePiece (place a piece on the board
//----------------------------------------------------------------------------
public void placePiece(move M, int whosMove){
placePiece(whosMove, M.row, M.col);
}
//----------------------------------------------------------------------------
// removePiece(move)
// used for recursion
//----------------------------------------------------------------------------
public void removePiece(move M){
board[M.row][M.col] = 1;
totalMoves--;
}
//----------------------------------------------------------------------------
// isGameOver
// checks for cats and for wins
//----------------------------------------------------------------------------
public boolean isGameOver(){
int xWins = xMove * xMove * xMove;
int oWins = oMove * oMove * oMove;
if(boardFilled()){
return true;
}
for (int i = 0; i < 3; i++){
if(rowProduct(i) == xWins || rowProduct(i) == oWins){
return true;
}
else if(colProduct(i) == xWins || colProduct(i) == oWins){
return true;
}
}
if(upperLeftDiagProduct() == xWins || upperLeftDiagProduct() == oWins){
return true;
}
if(upperRightDiagProduct() == xWins || upperRightDiagProduct() == oWins){
return true;
}
return false;
}
//----------------------------------------------------------------------------
// getTurn
// whose turn is it
//----------------------------------------------------------------------------
public int getTurn(){
return turn;
}
//----------------------------------------------------------------------------
// isMasterGame
// is this the master game?
//----------------------------------------------------------------------------
public boolean isMasterGame(){
return masterGame == this;
}
}