-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAchi.java
More file actions
304 lines (289 loc) · 7.9 KB
/
Achi.java
File metadata and controls
304 lines (289 loc) · 7.9 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
/**
* class that implements support methods needed by algorithm computerPlay
*/
package assignment2;
public class Achi {
// Attribute Declarations
private char[][] gameBoard;
private int board_size;
private int max_levels;
/**
* Constructor creates a 2D array of specified size
* @param board_size - amount of columns/rows of array
* @param max_levels - level of difficulty
*/
public Achi (int board_size, int max_levels){
this.max_levels = max_levels;
this.board_size = board_size;
this.gameBoard = new char[board_size][board_size];
int i, j = 0;
// create an empty gameboard
for (i=0; i<board_size; i++){
for (j=0; j<board_size; j++){
gameBoard[i][j]=' ';
}
}
}
/**
* class that creates a dictionary
* @return - dictionary of selected size
*/
public Dictionary createDictionary(){
return new Dictionary();
}
/**
* Represents the contents of gameBoard as a string then checks if the string is in configurations dictionary
* @param configurations - dictionary that contains all configuration
* @return score of configuration if the configuration is in the dictionary, -1 if score isn't in dictionary
*/
public int repeatedConfig(Dictionary configurations){
String boardConfig = "";
// turn the board configurations into a string
for (int i=0; i<board_size; i++){
for (int j=0; j<board_size; j++){
boardConfig = boardConfig + gameBoard[i][j];
}
}
// check if the configuration is in a dictionary
return configurations.find(boardConfig);
}
/**
* Represents contents of gameBoard as a string then inserts string and score into configurations dictionary
* @param configurations - dictionary that contains all configuration
* @param score - score of that configuration
* @throws DictionaryException
*/
public void insertConfig(Dictionary configurations, int score){
String boardConfig = "";
// represents contents of gameBoard as a string
for (int i =0; i<board_size; i++){
for (int j=0; j<board_size; j++){
boardConfig = boardConfig + gameBoard[i][j];
}
}
// put the data into a configdata object
ConfigData config = new ConfigData(boardConfig, score);
// insert into the dictionary, catch any exceptions generated
try {
configurations.insert(config);
} catch (DictionaryException e) {
e.printStackTrace();
}
}
/**
* stores symbol in gameBoard[row][column]
* @param row - row that you want to insert symbol
* @param col - column that you want to insert symbol
* @param symbol
*/
public void storePlay(int row, int col, char symbol){
gameBoard[row][col]=symbol;
}
/**
* check if the tile at a specified position on the board is empty
* @param row
* @param col
* @return true if the tile is empty, false otherwise
*/
public boolean tileIsEmpty(int row, int col){
if (gameBoard[row][col]==' '){
return true;
}
else{
return false;
}
}
/**
* check if the tile at a specified position on the board is the computer ('O')
* @param row
* @param col
* @return true if the tile is the computer, false otherwise
*/
public boolean tileIsComputer(int row, int col){
if (gameBoard[row][col]== 'O'){
return true;
}
else{
return false;
}
}
/**
* check if the tile at a specified position on the board is human ('X')
* @param row
* @param col
* @return
*/
public boolean tileIsHuman(int row, int col){
if (gameBoard[row][col]=='X'){
return true;
}
else{
return false;
}
}
/**
* check if there is a win on the board (adjacent tiles of type symbol in same row, column, or diagonal)
* @param symbol - symbol that you are checking if there is a win
* @return true if there is a win for the symbol, false if not
*/
public boolean wins(char symbol){
boolean horizontalWin = true; // variable for horizontal win
boolean verticalWin = true; // variable for vertical win
boolean diagonalWin1 = true; // variable for diagonal win
boolean diagonalWin2 = true;
int h = 0;
int v = 0;
int d1 = 0;
int d2 = 0;
for (int i=0; i<board_size; i++){ // iterate through rows of array
// check horizontal win
while (h<board_size){ // iterate through a column
if (gameBoard[i][h]!=symbol){ // check each element against the symbol
horizontalWin = false; // found that an element doesn't equal the symbol
}
h++; // increment count
}
if (horizontalWin == true){ // if all element in row match symbol, return true
return true;
}
else{ // reset variable to check next row
h=0;
horizontalWin = true;
}
}
// check for vertical wins
for (int q=0; q<board_size; q++){ // iterate through column of array
while (v<board_size){ // iterate through a row
if (gameBoard[v][q]!=symbol){ // check each element in column against the symbol
verticalWin=false; // found that an element on row doesn't equal symbol
}
v++; // increment count
}
if (verticalWin == true){ // if all elements in column match symbol, return true
return true;
}
else{ // reset variable to check next column
v=0;
verticalWin = true;
}
}
//check for diagonal wins top left to bottom right
for (d1=0; d1<board_size; d1++){ // iterate through rows
if (gameBoard[d1][d1]!=symbol){ // check each spot with the same row and column coordinate
diagonalWin1 = false;
}
}
if (diagonalWin1==true){
return true;
}
//check for diagonal top right to bottom left
for (d2=0; d2<board_size; d2++){
if (gameBoard[d2][board_size-1-d2]!=symbol){
diagonalWin2=false;
break;
}
}
if (diagonalWin2==true){
return true;
}
return false;
}
/**
* class that determines if the board is a draw
* @param symbol - symbol of the next move
* @return true if the board is a draw, false otherwise
*/
public boolean isDraw(char symbol){
int spaceCount = 0;
int spaceRow=0;
int spaceColumn=0;
boolean surSymbol = true;
// find the space in the board and remember the row and column number
for (int r=0; r<board_size; r++){
for (int c=0; c<board_size; c++){
if (gameBoard[r][c]==' '){
spaceRow = r;
spaceColumn = c;
spaceCount++;
}
}
}
// Check if there is more than one space
if (spaceCount > 1){
surSymbol = false;
}
else{
// check spot below
if (spaceRow+1<board_size){
if (gameBoard[spaceRow+1][spaceColumn]==symbol){
surSymbol = false;
}
}
// check spot to the right
if (spaceColumn+1<board_size){
if (gameBoard[spaceRow][spaceColumn+1]==symbol){
surSymbol = false;
}
}
// check spot above
if (spaceRow-1>0){
if (gameBoard[spaceRow -1][spaceColumn]==symbol){
surSymbol = false;
}
}
// check spot to the left
if (spaceColumn-1>0){
if (gameBoard[spaceRow][spaceColumn-1]==symbol){
surSymbol = false;
}
}
// check upper left corner
if ((spaceRow-1>0) && (spaceColumn-1)>0){
if (gameBoard[spaceRow-1][spaceColumn-1]==symbol){
surSymbol = false;
}
}
// check lower right corner
if ((spaceRow+1<board_size) && (spaceColumn+1)<board_size){
if (gameBoard[spaceRow+1][spaceColumn+1]==symbol){
surSymbol = false;
}
}
// check upper right corner
if ((spaceRow-1>0) && (spaceColumn+1)<board_size){
if (gameBoard[spaceRow-1][spaceColumn+1]==symbol){
surSymbol = false;
}
}
// check lower right corner
if ((spaceRow+1<board_size) && (spaceColumn-1)>0){
if (gameBoard[spaceRow+1][spaceColumn-1]==symbol){
surSymbol = false;
}
}
}
return surSymbol;
}
/**
* method that evaluates the winner on the board
* @param symbol
* @return
*/
public int evalBoard(char symbol){
int points=1;
// computer wins
if (wins('O')==true){
points =3;
}
//human wins
else if (wins('X')==true){
points=0;
}
//game is a draw
else if (isDraw(symbol)==true){
points=2;
}
// game is still undecided
return points;
}
}