Skip to content

Commit 449aa64

Browse files
authored
Merge pull request #1 from chptr-one/refactoring
The changes look good, the enum especially. Thanks for the contribution.
2 parents c294213 + e5d576d commit 449aa64

File tree

8 files changed

+158
-145
lines changed

8 files changed

+158
-145
lines changed

TicTacToe/src/Game/Tile.java

-26
This file was deleted.

TicTacToe/src/AI/MiniMax.java renamed to TicTacToe/src/ai/MiniMax.java

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
package AI;
1+
package ai;
22

3-
import Game.Board;
3+
import game.Board;
4+
5+
import static game.Mark.*;
46

57
/**
68
* The MiniMax algorithm in its most basic form.
9+
*
710
* @author DavidHurst
811
*/
912
public class MiniMax {
@@ -38,10 +41,10 @@ public static int miniMax(Board board, int depth, boolean isMax) {
3841
for (int row = 0; row < board.getWidth(); row++) {
3942
for (int col = 0; col < board.getWidth(); col++) {
4043
if (!board.isTileMarked(row, col)) {
41-
board.setMarkAt(row, col, 'X');
44+
board.setMarkAt(row, col, X);
4245
highestVal = Math.max(highestVal, miniMax(board,
4346
depth - 1, false));
44-
board.setMarkAt(row, col, ' ');
47+
board.setMarkAt(row, col, BLANK);
4548
}
4649
}
4750
}
@@ -52,10 +55,10 @@ public static int miniMax(Board board, int depth, boolean isMax) {
5255
for (int row = 0; row < board.getWidth(); row++) {
5356
for (int col = 0; col < board.getWidth(); col++) {
5457
if (!board.isTileMarked(row, col)) {
55-
board.setMarkAt(row, col, 'O');
58+
board.setMarkAt(row, col, O);
5659
lowestVal = Math.min(lowestVal, miniMax(board,
5760
depth - 1, true));
58-
board.setMarkAt(row, col, ' ');
61+
board.setMarkAt(row, col, BLANK);
5962
}
6063
}
6164
}
@@ -75,9 +78,9 @@ public static int[] getBestMove(Board board) {
7578
for (int row = 0; row < board.getWidth(); row++) {
7679
for (int col = 0; col < board.getWidth(); col++) {
7780
if (!board.isTileMarked(row, col)) {
78-
board.setMarkAt(row, col, 'X');
81+
board.setMarkAt(row, col, X);
7982
int moveValue = miniMax(board, MAX_DEPTH, false);
80-
board.setMarkAt(row, col, ' ');
83+
board.setMarkAt(row, col, BLANK);
8184
if (moveValue > bestValue) {
8285
bestMove[0] = row;
8386
bestMove[1] = col;
@@ -99,13 +102,13 @@ public static int[] getBestMove(Board board) {
99102
private static int evaluateBoard(Board board) {
100103
int rowSum = 0;
101104
int bWidth = board.getWidth();
102-
int Xwin = 'X' * bWidth;
103-
int Owin = 'O' * bWidth;
105+
int Xwin = X.getMark() * bWidth;
106+
int Owin = O.getMark() * bWidth;
104107

105108
// Check rows for winner.
106109
for (int row = 0; row < bWidth; row++) {
107110
for (int col = 0; col < bWidth; col++) {
108-
rowSum += board.getMarkAt(row, col);
111+
rowSum += board.getMarkAt(row, col).getMark();
109112
}
110113
if (rowSum == Xwin) {
111114
return 10;
@@ -119,7 +122,7 @@ private static int evaluateBoard(Board board) {
119122
rowSum = 0;
120123
for (int col = 0; col < bWidth; col++) {
121124
for (int row = 0; row < bWidth; row++) {
122-
rowSum += board.getMarkAt(row, col);
125+
rowSum += board.getMarkAt(row, col).getMark();
123126
}
124127
if (rowSum == Xwin) {
125128
return 10;
@@ -133,7 +136,7 @@ private static int evaluateBoard(Board board) {
133136
// Top-left to bottom-right diagonal.
134137
rowSum = 0;
135138
for (int i = 0; i < bWidth; i++) {
136-
rowSum += board.getMarkAt(i, i);
139+
rowSum += board.getMarkAt(i, i).getMark();
137140
}
138141
if (rowSum == Xwin) {
139142
return 10;
@@ -145,7 +148,7 @@ private static int evaluateBoard(Board board) {
145148
rowSum = 0;
146149
int indexMax = bWidth - 1;
147150
for (int i = 0; i <= indexMax; i++) {
148-
rowSum += board.getMarkAt(i, indexMax - i);
151+
rowSum += board.getMarkAt(i, indexMax - i).getMark();
149152
}
150153
if (rowSum == Xwin) {
151154
return 10;

TicTacToe/src/AI/MiniMaxAlphaBeta.java renamed to TicTacToe/src/ai/MiniMaxAlphaBeta.java

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
package AI;
1+
package ai;
22

3-
import Game.Board;
3+
import game.Board;
4+
5+
import static game.Mark.*;
46

57
/**
68
* The MiniMax algorithm optimised with Alpha-Beta pruning.
9+
*
710
* @author DavidHurst
811
*/
912
public class MiniMaxAlphaBeta {
@@ -44,10 +47,10 @@ public static int miniMax(Board board, int depth, int alpha, int beta,
4447
for (int row = 0; row < board.getWidth(); row++) {
4548
for (int col = 0; col < board.getWidth(); col++) {
4649
if (!board.isTileMarked(row, col)) {
47-
board.setMarkAt(row, col, 'X');
50+
board.setMarkAt(row, col, X);
4851
highestVal = Math.max(highestVal, miniMax(board,
4952
depth - 1, alpha, beta, false));
50-
board.setMarkAt(row, col, ' ');
53+
board.setMarkAt(row, col, BLANK);
5154
alpha = Math.max(alpha, highestVal);
5255
if (alpha >= beta) {
5356
return highestVal;
@@ -62,10 +65,10 @@ public static int miniMax(Board board, int depth, int alpha, int beta,
6265
for (int row = 0; row < board.getWidth(); row++) {
6366
for (int col = 0; col < board.getWidth(); col++) {
6467
if (!board.isTileMarked(row, col)) {
65-
board.setMarkAt(row, col, 'O');
68+
board.setMarkAt(row, col, O);
6669
lowestVal = Math.min(lowestVal, miniMax(board,
6770
depth - 1, alpha, beta, true));
68-
board.setMarkAt(row, col, ' ');
71+
board.setMarkAt(row, col, BLANK);
6972
beta = Math.min(beta, lowestVal);
7073
if (beta <= alpha) {
7174
return lowestVal;
@@ -89,10 +92,10 @@ public static int[] getBestMove(Board board) {
8992
for (int row = 0; row < board.getWidth(); row++) {
9093
for (int col = 0; col < board.getWidth(); col++) {
9194
if (!board.isTileMarked(row, col)) {
92-
board.setMarkAt(row, col, 'X');
95+
board.setMarkAt(row, col, X);
9396
int moveValue = miniMax(board, MAX_DEPTH, Integer.MIN_VALUE,
9497
Integer.MAX_VALUE, false);
95-
board.setMarkAt(row, col, ' ');
98+
board.setMarkAt(row, col, BLANK);
9699
if (moveValue > bestValue) {
97100
bestMove[0] = row;
98101
bestMove[1] = col;
@@ -114,13 +117,13 @@ public static int[] getBestMove(Board board) {
114117
private static int evaluateBoard(Board board) {
115118
int rowSum = 0;
116119
int bWidth = board.getWidth();
117-
int Xwin = 'X' * bWidth;
118-
int Owin = 'O' * bWidth;
120+
int Xwin = X.getMark() * bWidth;
121+
int Owin = O.getMark() * bWidth;
119122

120123
// Check rows for winner.
121124
for (int row = 0; row < bWidth; row++) {
122125
for (int col = 0; col < bWidth; col++) {
123-
rowSum += board.getMarkAt(row, col);
126+
rowSum += board.getMarkAt(row, col).getMark();
124127
}
125128
if (rowSum == Xwin) {
126129
return 10;
@@ -134,7 +137,7 @@ private static int evaluateBoard(Board board) {
134137
rowSum = 0;
135138
for (int col = 0; col < bWidth; col++) {
136139
for (int row = 0; row < bWidth; row++) {
137-
rowSum += board.getMarkAt(row, col);
140+
rowSum += board.getMarkAt(row, col).getMark();
138141
}
139142
if (rowSum == Xwin) {
140143
return 10;
@@ -148,7 +151,7 @@ private static int evaluateBoard(Board board) {
148151
// Top-left to bottom-right diagonal.
149152
rowSum = 0;
150153
for (int i = 0; i < bWidth; i++) {
151-
rowSum += board.getMarkAt(i, i);
154+
rowSum += board.getMarkAt(i, i).getMark();
152155
}
153156
if (rowSum == Xwin) {
154157
return 10;
@@ -160,7 +163,7 @@ private static int evaluateBoard(Board board) {
160163
rowSum = 0;
161164
int indexMax = bWidth - 1;
162165
for (int i = 0; i <= indexMax; i++) {
163-
rowSum += board.getMarkAt(i, indexMax - i);
166+
rowSum += board.getMarkAt(i, indexMax - i).getMark();
164167
}
165168
if (rowSum == Xwin) {
166169
return 10;

TicTacToe/src/AI/MiniMaxCombined.java renamed to TicTacToe/src/ai/MiniMaxCombined.java

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
package AI;
1+
package ai;
22

3-
import Game.Board;
3+
import game.Board;
4+
5+
import static game.Mark.*;
46

57
/**
68
* The MiniMax algorithm which is optimised with Alpha-Beta pruning and improved
79
* to have a better heuristic for it's evaluation function.
10+
*
811
* @author DavidHurst
912
*/
1013
public class MiniMaxCombined {
@@ -45,10 +48,10 @@ public static int miniMax(Board board, int depth, int alpha, int beta,
4548
for (int row = 0; row < board.getWidth(); row++) {
4649
for (int col = 0; col < board.getWidth(); col++) {
4750
if (!board.isTileMarked(row, col)) {
48-
board.setMarkAt(row, col, 'X');
51+
board.setMarkAt(row, col, X);
4952
highestVal = Math.max(highestVal, miniMax(board,
5053
depth - 1, alpha, beta, false));
51-
board.setMarkAt(row, col, ' ');
54+
board.setMarkAt(row, col, BLANK);
5255
alpha = Math.max(alpha, highestVal);
5356
if (alpha >= beta) {
5457
return highestVal;
@@ -63,10 +66,10 @@ public static int miniMax(Board board, int depth, int alpha, int beta,
6366
for (int row = 0; row < board.getWidth(); row++) {
6467
for (int col = 0; col < board.getWidth(); col++) {
6568
if (!board.isTileMarked(row, col)) {
66-
board.setMarkAt(row, col, 'O');
69+
board.setMarkAt(row, col, O);
6770
lowestVal = Math.min(lowestVal, miniMax(board,
6871
depth - 1, alpha, beta, true));
69-
board.setMarkAt(row, col, ' ');
72+
board.setMarkAt(row, col, BLANK);
7073
beta = Math.min(beta, lowestVal);
7174
if (beta <= alpha) {
7275
return lowestVal;
@@ -90,10 +93,10 @@ public static int[] getBestMove(Board board) {
9093
for (int row = 0; row < board.getWidth(); row++) {
9194
for (int col = 0; col < board.getWidth(); col++) {
9295
if (!board.isTileMarked(row, col)) {
93-
board.setMarkAt(row, col, 'X');
96+
board.setMarkAt(row, col, X);
9497
int moveValue = miniMax(board, MAX_DEPTH, Integer.MIN_VALUE,
9598
Integer.MAX_VALUE, false);
96-
board.setMarkAt(row, col, ' ');
99+
board.setMarkAt(row, col, BLANK);
97100
if (moveValue > bestValue) {
98101
bestMove[0] = row;
99102
bestMove[1] = col;
@@ -118,13 +121,13 @@ public static int[] getBestMove(Board board) {
118121
private static int evaluateBoard(Board board, int depth) {
119122
int rowSum = 0;
120123
int bWidth = board.getWidth();
121-
int Xwin = 'X' * bWidth;
122-
int Owin = 'O' * bWidth;
124+
int Xwin = X.getMark() * bWidth;
125+
int Owin = O.getMark() * bWidth;
123126

124127
// Check rows for winner.
125128
for (int row = 0; row < bWidth; row++) {
126129
for (int col = 0; col < bWidth; col++) {
127-
rowSum += board.getMarkAt(row, col);
130+
rowSum += board.getMarkAt(row, col).getMark();
128131
}
129132
if (rowSum == Xwin) {
130133
return 10 + depth;
@@ -138,7 +141,7 @@ private static int evaluateBoard(Board board, int depth) {
138141
rowSum = 0;
139142
for (int col = 0; col < bWidth; col++) {
140143
for (int row = 0; row < bWidth; row++) {
141-
rowSum += board.getMarkAt(row, col);
144+
rowSum += board.getMarkAt(row, col).getMark();
142145
}
143146
if (rowSum == Xwin) {
144147
return 10 + depth;
@@ -152,7 +155,7 @@ private static int evaluateBoard(Board board, int depth) {
152155
// Top-left to bottom-right diagonal.
153156
rowSum = 0;
154157
for (int i = 0; i < bWidth; i++) {
155-
rowSum += board.getMarkAt(i, i);
158+
rowSum += board.getMarkAt(i, i).getMark();
156159
}
157160
if (rowSum == Xwin) {
158161
return 10 + depth;
@@ -164,7 +167,7 @@ private static int evaluateBoard(Board board, int depth) {
164167
rowSum = 0;
165168
int indexMax = bWidth - 1;
166169
for (int i = 0; i <= indexMax; i++) {
167-
rowSum += board.getMarkAt(i, indexMax - i);
170+
rowSum += board.getMarkAt(i, indexMax - i).getMark();
168171
}
169172
if (rowSum == Xwin) {
170173
return 10 + depth;

0 commit comments

Comments
 (0)