-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chessboard.java
320 lines (263 loc) · 12.2 KB
/
Chessboard.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Ragha
*/
/**
* Class Chessboard represents a 2 dimensional chessboard
* to traverse and find a path through
*/
public class Chessboard {
private ChessboardStatus[][] chessboard;
/**
* Creates an square Chessboard of given size
*/
public Chessboard(int size) {
this(size, size);
}
/**
* Creates a 2 dimensional Chessboard of given height and size, with all
* positions initially open
*/
public Chessboard(int width, int height) {
chessboard = new ChessboardStatus[height][width];
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
chessboard[row][col] = ChessboardStatus.OPEN;
}
}
}
/**
* Initializes a Chessboard from the given textual representation
*/
public Chessboard(String sChessboard) {
this.chessboard = stringToChessboard(sChessboard);
}
/**
* Returns the ChessboardStatus value corresponding to the given cell,
* specified by its row and column
*/
public ChessboardStatus getPosStatus(int row, int col) {
return chessboard[row][col];
}
/**
* Returns the ChessboardStatus value corresponding to the given position
*/
public ChessboardStatus getPosStatus(ChessboardPosition pos) {
return chessboard[pos.getCoords()[0]][pos.getCoords()[1]];
}
/**
* Sets the cell corresponding to the specified row and column to the given
* status value
*/
public void setPosStatus(int row, int col, ChessboardStatus newStatus) {
chessboard[row][col] = newStatus;
}
/**
* Sets the cell corresponding to the specified position to the given status
* value
*/
public void setPosStatus(ChessboardPosition pos, ChessboardStatus newStatus) {
chessboard[pos.getCoords()[0]][pos.getCoords()[1]] = newStatus;
}
/**
* String representation of the given Chessboard
*/
public String toString() {
String s = null;
if (chessboard != null && chessboard[0] != null) {
s = "";
for (int r = 0; r < chessboard.length; r++) {
for (int c = 0; c < chessboard[0].length; c++) {
s = s.concat(String.valueOf(chessboard[r][c].text()));
}
s = s.concat(System.lineSeparator());
}
}
return s;
}
/**
* Converts string to array chessboard and assign to each of its cells
* the corresponding status value based on the given string
*/
private ChessboardStatus[][] stringToChessboard(String sChessboard) {
ChessboardStatus[][] chessboard = null;
if (sChessboard != null) {
String[] sChessboardRows = sChessboard.split("\n");
char[] cChessboardCols;
chessboard = new ChessboardStatus[sChessboardRows.length][];
for (int row = 0; row < sChessboardRows.length; row++) {
if (sChessboardRows[row] == null) {
chessboard[row] = null;
} else {
chessboard[row] = new ChessboardStatus[sChessboardRows[row].length()];
cChessboardCols = sChessboardRows[row].toCharArray();
for (int col = 0; col < cChessboardCols.length; col++) {
if (cChessboardCols[col] == ChessboardStatus.GOAL.text()) {
chessboard[row][col] = ChessboardStatus.GOAL;
} else if (cChessboardCols[col] == ChessboardStatus.OPEN.text()) {
chessboard[row][col] = ChessboardStatus.OPEN;
} else if (cChessboardCols[col] == ChessboardStatus.OBSTACLE.text()) {
chessboard[row][col] = ChessboardStatus.OBSTACLE;
} else if (cChessboardCols[col] == ChessboardStatus.VISITED.text()) {
chessboard[row][col] = ChessboardStatus.VISITED;
}
}
}
}
}
return chessboard;
}
/**
* Returns the ChessboardPosition of the next position, if its is a valid position.
* Returns null if the destination position is outside the limits of the array.
*/
public ChessboardPosition getNeighbour(ChessboardPosition pos, Move mov) {
ChessboardPosition newPos = null;
int[] newCoords = getNeighbourCoords(pos.getCoords()[0], pos.getCoords()[1], mov);
if (newCoords != null) {
// Create new ChessboardPosition object with the new coordinates and the received position pos as previous one (from)
newPos = new ChessboardPosition(newCoords, pos);
}
return newPos;
}
/**
* Returns the coordinates of the next position, if its is a valid position.
* Returns null if the destination position is outside the limits of the array.
*/
public int[] getNeighbourCoords(int row, int col, Move mov) {
int[] currentCoords = new int[]{row, col};
int[] newCoords = new int[]{
currentCoords[0] + mov.vShift(),
currentCoords[1] + mov.hShift()
};
if (newCoords[0] < 0 || newCoords[0] >= chessboard.length
|| newCoords[1] < 0 || newCoords[1] >= chessboard[0].length) {
// invalid position
newCoords = null;
}
return newCoords;
}
/**
* Updates the state of the chessboard positions following a given DLLPath.
* If the position to visit is OPEN or VISITED change it to VISITED.
* If the position to visit is GOAL, does not change it so that the goal keeps displaying in the chessboard.
* If the position to visit is OBSTACLE or the position is outside the limits of the
* chessboard, it is an invalid move, stop and finish.
*
*/
public void followPath(DLLPath path) {
if (path != null) {
int[] coords = path.removeFirst();
while (coords != null) {
//to see what happen in this method, we call to String for currntly obj
// System.out.println(this);
switch (this.getPosStatus(coords[0], coords[1])) {
case OPEN:
case VISITED:
this.setPosStatus(coords[0], coords[1], ChessboardStatus.VISITED);
coords = path.removeFirst();
break;
case OBSTACLE:
// invalid move: stop here
return;
case GOAL:
// does not change status to keep showing GOAL
coords = path.removeFirst();
break;
}
}
}
}
public static void main(String[] args){
Chessboard chessboard = new Chessboard(4);
System.out.print(chessboard);
ChessboardPosition pos1 = new ChessboardPosition(0,1);
ChessboardPosition pos2 = new ChessboardPosition(2,2);
chessboard.setPosStatus(pos1, ChessboardStatus.OBSTACLE);
chessboard.setPosStatus(pos2, ChessboardStatus.GOAL);
chessboard.setPosStatus(0, 2, ChessboardStatus.OBSTACLE);
chessboard.setPosStatus(0, 3, ChessboardStatus.OBSTACLE);
chessboard.setPosStatus(1, 3, ChessboardStatus.OBSTACLE);
chessboard.setPosStatus(2, 0, ChessboardStatus.OBSTACLE);
chessboard.setPosStatus(2, 3, ChessboardStatus.OBSTACLE);
chessboard.setPosStatus(3, 0, ChessboardStatus.OBSTACLE);
chessboard.setPosStatus(3, 1, ChessboardStatus.OBSTACLE);
chessboard.setPosStatus(3, 2, ChessboardStatus.OBSTACLE);
chessboard.setPosStatus(3, 3, ChessboardStatus.OBSTACLE);
System.out.print(chessboard);
System.out.println("Status in position 0,2: "+chessboard.getPosStatus(0, 2));
System.out.println("Status in position2 : "+chessboard.getPosStatus(pos2));
if(chessboard.getPosStatus(0, 2)==ChessboardStatus.OBSTACLE)
System.out.println("TRRRRREEEE");
//getNebout
System.out.println("Neighbour up of pos (1,2) is: "+ (chessboard.getNeighbour(new ChessboardPosition(1,2) , Move.UP)).toString());
System.out.println("Neighbour down of pos (1,2) is: "+ (chessboard.getNeighbour(new ChessboardPosition(1,2) , Move.DOWN)).toString());
System.out.println("Neighbour right of pos (1,2) is: "+ (chessboard.getNeighbour(new ChessboardPosition(1,2) , Move.RIGHT)).toString());
System.out.println("Neighbour up left pos (1,2) is: "+ (chessboard.getNeighbour(new ChessboardPosition(1,2) , Move.LEFT)).toString());
//string to chessboard
Chessboard chessboard2 = new Chessboard(ChessboardSamples.sChessboard2);
System.out.print(chessboard2.toString());
int[] Coords = new int[]{2, 4};
for( int v : Coords)
System.out.print(v+" ");
//follow path for samples2
System.out.println("___________________\n");
/* //test if status of position are open or visited or goal
System.out.println("Chessboard before followPath: ");
System.out.println(chessboard2);
DLLPath p1 = new DLLPath();
p1.addLast(0, 0);p1.addLast(1, 0);p1.addLast(1, 1);p1.addLast(1, 2);p1.addLast(1, 3);p1.addLast(2, 3);p1.addLast(3, 3);
chessboard2.followPath(p1);
System.out.println("Chessboard after followPath: ");
System.out.println(chessboard2);*/
//test if status of postion are obtacle or invalid postion
//obtacle
/* System.out.println("Chessboard before followPath: ");
System.out.println(chessboard2);
DLLPath p2 = new DLLPath();
p2.addLast(0, 0);p2.addLast(1, 0);p2.addLast(1, 1);p2.addLast(1, 2);p2.addLast(1, 3);
//obtical
p2.addLast(1, 4);p2.addLast(2, 4);
p2.addLast(2, 3);p2.addLast(3, 3);
chessboard2.followPath(p2);
System.out.println("Chessboard after followPath: ");
System.out.println(chessboard2);*/
/* //invalid position
System.out.println("Chessboard before followPath: ");
System.out.println(chessboard2);
DLLPath p2 = new DLLPath();
//invalid postion
p2.addLast(-1,0);
p2.addLast(0, 0);p2.addLast(1, 0);p2.addLast(1, 1);p2.addLast(1, 2);p2.addLast(1, 3); p2.addLast(2, 3);p2.addLast(3, 3);
chessboard2.followPath(p2);
System.out.println("Chessboard after followPath: ");
System.out.println(chessboard2);*/
Chessboard chessboardSample2 =new Chessboard(ChessboardSamples.sChessboard2);
DLLPath path= buildPath(chessboardSample2,PathSamples.movsPath2a);
System.out.println(path.toString());
chessboardSample2.followPath(path);
System.out.println(chessboardSample2.toString());
}
public static DLLPath buildPath(Chessboard chessboard, Move[] movs) {
DLLPath path = new DLLPath();
int[] currentCoords = new int[]{0, 0}; // (row, col)
int[] nextCoords;
path.addLast(currentCoords[0], currentCoords[1]);
for (int i = 0; i < movs.length; i++) {
nextCoords = chessboard.getNeighbourCoords(currentCoords[0], currentCoords[1], movs[i]);
if (nextCoords != null) {
path.addLast(nextCoords[0], nextCoords[1]);
} else {
// invalid movement: stop here
break;
}
currentCoords = nextCoords;
}
return path;
}
}