-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.java
More file actions
359 lines (339 loc) · 9.72 KB
/
Copy pathKnight.java
File metadata and controls
359 lines (339 loc) · 9.72 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
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
import java.util.Arrays;
import java.util.Random;
public class Knight {
private Square currentSquare;
private Square startingSquare;
private boolean[][] board;
static Random randy = new Random();
/**
* Constructs a Knight. This Knight's board should be created with size rows
* x cols. Sets the value of the Square represented by s to true. Sets all
* other board values to false. Sets currentSquare and startingSquare to s.
*
* @param s
* the starting Square for this Knight
* @param rows
* the number of rows in this Knight's board
* @param cols
* the number of columns in this Knight's board
*/
public Knight(Square s, int rows, int cols) {
// 1
board = new boolean[rows][cols];
currentSquare = s;
startingSquare = s;
clearBoard();
board[s.getRow()][s.getCol()] = true;
}
/**
* Returns this Knight's current Square.
*
* @return this Knight's current Square.
*/
public Square getCurrentSquare() {
// 2
return currentSquare;
}
/**
* Returns this Knight's starting Square.
*
* @return this Knight's starting Square.
*/
public Square getStartingSquare() {
return startingSquare;
}
/**
* Returns this Knight's board.
*
* @return this Knight's board.
*/
public boolean[][] getBoard() {
return board;
}
/**
* Returns a 1D array of Square representing a Knights Tour for this Knight.
*
* @return a 1D array of Square representing a Knights Tour for this Knight
*/
public Square[] solve() {
// 13
int moves=board.length*board[0].length;
Square[] tour = new Square[moves];
tour[0] = startingSquare;
// System.out.println(Arrays.toString(tour));
// System.out.println(moves);
while (tour[moves-1]==null) {
setCurrentSquare(this.getStartingSquare());
for (int k=0; k<moves-1; k++) {
// int k=0;
setCurrentSquare(this.getCurrentSquare());
Square next = getBestSquare(getBestSquares(getPossibleSquares()));
setCurrentSquare(next);
board[next.getRow()][next.getCol()] = true;
tour[k+1]=next;
// k++;
}
clearBoard();
}
return tour;
// int moves = 0;
//// ArrayList<Square> fin = new ArrayList<Square>();
// setStartingSquare(this.currentSquare);
// while (boardisFalse() == true) {
// setCurrentSquare(this.getCurrentSquare());
// Square next = getBestSquare(getBestSquares(getPossibleSquares()));
// setCurrentSquare(next);
// board[next.getRow()][next.getCol()] = true;
// moves++;
//// fin.add(next);
// }
// Square[] pre = new Square[moves];
//// Square[] pre = new Square[board.length*board[0].length];
//// for(int i = 0; i< pre.length;i++) {
//// System.out.println(pre[i]);
//// }
// int k = 0;
//// clearBoard();
//// while (pre[pre.length-1]==null) {
// setCurrentSquare(this.getStartingSquare());
// while (boardisFalse() == true) {
// setCurrentSquare(this.getCurrentSquare());
// Square next = getBestSquare(getBestSquares(getPossibleSquares()));
// setCurrentSquare(next);
// board[next.getRow()][next.getCol()] = true;
// pre[k] = next;
// k++;
//// }
//// clearBoard();
// }
//// Square[]tour=pre;
// return pre;
//// return toArray(fin);
//// for (int i=0;i<fin.size();i++) {
//// tour[i] = fin.get(i);
//// }
//// return tour;
}
public boolean boardisFalse() {
int count = 0;
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] == false) {
count++;
}
}
}
if (count > 0) {
return true;
} else {
return false;
}
}
// regular set method, give instance variable the parameter's value, should
// also put value true
public void setCurrentSquare(Square currentSquare) {
// 5
this.currentSquare = currentSquare;
board[currentSquare.getRow()][currentSquare.getCol()] = true;
}
// ask about
public void setStartingSquare(Square startingSquare) {
// 6
this.startingSquare = startingSquare;
clearBoard();
currentSquare = startingSquare;
board[startingSquare.getRow()][startingSquare.getCol()] = true;
}
/**
* Determines if starting Square is reachable in one move from current
* Square.
*
* @return true if starting Square is reachable in one move from current
* Square, false otherwise
*/
public boolean startIsReachableFromCurrent() {
// 14 (not necessary unless you are doing a closed Knight's Tour)
if (startingSquare.getRow()==currentSquare.getRow()+2 && startingSquare.getCol()==currentSquare.getCol()+1) {
return true;
}
else {
return false;
}
}
/**
* Returns a 1D array of Square in which each Square has the smallest
* positive score.
*
* @param possible
* the array of Square
* @return a 1D array of Square in which each Square has the smallest score
* in possible
*/
public Square[] getBestSquares(Square[] possible) {
// 11
int min = getScoreOfSquare(possible[0].getRow(), possible[0].getCol());
int count = 0;
int m = 0;
for (int k = 0; k < possible.length; k++) {
if (min > getScoreOfSquare(possible[k].getRow(), possible[k].getCol())) {
min = getScoreOfSquare(possible[k].getRow(), possible[k].getCol());
}
}
for (int n = 0; n < possible.length; n++) {
if (min == getScoreOfSquare(possible[n].getRow(), possible[n].getCol())) {
count++;
}
}
Square[] toReturn = new Square[count];
for (int n = 0; n < possible.length; n++) {
if (min == getScoreOfSquare(possible[n].getRow(), possible[n].getCol())) {
toReturn[m] = possible[n];
m++;
}
}
return toReturn;
}
/**
* Randomly chooses a Square from squaresWithBestScore
*
* @param squaresWithBestScore
* an array of Square
* @return a randomly-chosen Square from squaresWithBestScore
*/
public Square getBestSquare(Square[] squaresWithBestScore) {
// 12
if (squaresWithBestScore.length == 1) {
return squaresWithBestScore[0];
} else {
Random randy = new Random();
int r = randy.nextInt(squaresWithBestScore.length - 1);
return squaresWithBestScore[r];
}
}
/**
* Sets all values in this Knight's board to false.
*/
public void clearBoard() {
// 7
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
board[r][c] = false;
}
}
}
/**
* Returns a 1D array of all unvisited Squares that are within one knight
* move of this Knight's current Square.
*
* @return a 1D array of all unvisited Squares that are within one knight
* move of this Knight's current Square
*/
public Square[] getPossibleSquares() {
// 10
int row = currentSquare.getRow();
int col = currentSquare.getCol();
Square[] toReturn = new Square[getScoreOfSquare(currentSquare.getRow(), currentSquare.getCol())];
int k = 0;
// k<toReturn.length;
if (isValid(row + 2, col + 1) && board[row + 2][col + 1] == false) {
Square a = new Square(row + 2, col + 1);
toReturn[k] = a;
k++;
}
if (isValid(row + 2, col - 1) && board[row + 2][col - 1] == false) {
Square a = new Square(row + 2, col - 1);
toReturn[k] = a;
k++;
}
if (isValid(row - 2, col + 1) && board[row - 2][col + 1] == false) {
Square a = new Square(row - 2, col + 1);
toReturn[k] = a;
k++;
}
if (isValid(row - 2, col - 1) && board[row - 2][col - 1] == false) {
Square a = new Square(row - 2, col - 1);
toReturn[k] = a;
k++;
}
if (isValid(row - 1, col + 2) && board[row - 1][col + 2] == false) {
Square a = new Square(row - 1, col + 2);
toReturn[k] = a;
k++;
}
if (isValid(row + 1, col + 2) && board[row + 1][col + 2] == false) {
Square a = new Square(row + 1, col + 2);
toReturn[k] = a;
k++;
}
if (isValid(row - 1, col - 2) && board[row - 1][col - 2] == false) {
Square a = new Square(row - 1, col - 2);
toReturn[k] = a;
k++;
}
if (isValid(row + 1, col - 2) && board[row + 1][col - 2] == false) {
Square a = new Square(row + 1, col - 2);
toReturn[k] = a;
k++;
}
return toReturn;
}
/**
* Returns the number of unvisited Squares that can be reached (with a
* knight move) from the Square at row, col.
*
* @param row
* the row
* @param col
* the column
* @return the number of unvisited Squares that can be reached (with a
* knight move) from the Square at row, col
*/
public int getScoreOfSquare(int row, int col) {
// 9
int count = 0;
if (isValid(row + 2, col + 1) && board[row + 2][col + 1] == false) {
count++;
}
if (isValid(row + 2, col - 1) && board[row + 2][col - 1] == false) {
count++;
}
if (isValid(row - 2, col + 1) && board[row - 2][col + 1] == false) {
count++;
}
if (isValid(row - 2, col - 1) && board[row - 2][col - 1] == false) {
count++;
}
if (isValid(row - 1, col + 2) && board[row - 1][col + 2] == false) {
count++;
}
if (isValid(row + 1, col + 2) && board[row + 1][col + 2] == false) {
count++;
}
if (isValid(row - 1, col - 2) && board[row - 1][col - 2] == false) {
count++;
}
if (isValid(row + 1, col - 2) && board[row + 1][col - 2] == false) {
count++;
}
return count;
}
/**
* Returns true if the square at row r, column c is in this Knight's board;
* returns false otherwise.
*
* @param r
* the row
* @param c
* the column
* @return true if the square at row r, column c is in this Knight's board;
* false otherwise
*/
public boolean isValid(int r, int c) {
// 8
if (r >= 0 && r < board.length && c >= 0 && c < board[0].length) {
return true;
} else {
return false;
}
}
}