-
Notifications
You must be signed in to change notification settings - Fork 1
/
Board.java
315 lines (240 loc) · 6.94 KB
/
Board.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
/**
* This class repersent a simple Pentago game board
*
* @author Mohammad Mahdi Malmsi
* @version 0.1.0
*/
public class Board
{
/* Feilds */
// the dimensions of the game board
private final int BOARD_X = 6;
private final int BOARD_Y = 6;
// the dimensions of the game visual board
private final int VISUAL_BOARD_X = (6 * 7) + 5;
private final int VISUAL_BOARD_Y = (6 * 4) + 3;
// the main board of the game
// 1 refers to player 1 (W: white)
// -1 refers to player 2 (B: black)
// 0 refers to empty block
private int[][] mainBoard;
// the visual board of the game
private char[][] visualBoard;
/* Constructor */
/**
* Creat a new board
* (no args Constructor)
*/
public Board()
{
// set up main board
mainBoard = new int[BOARD_Y][BOARD_X];
for (int j = 0; j < BOARD_Y; j++)
for (int i = 0; i < BOARD_X; i++)
mainBoard[j][j] = 0;
// set up visual board
visualBoard = new char[VISUAL_BOARD_Y][VISUAL_BOARD_X];
makeVisualBoard();
}
/* Methods */
// * getter methods *
/**
* @return the visual board
*/
public char[][] getVisualBoard()
{
return visualBoard;
}
/**
* @return the y of the visual board
*/
public int getVisualBoardY()
{
return VISUAL_BOARD_Y;
}
/**
* @return the x of the visual board
*/
public int getVisualBoardX()
{
return VISUAL_BOARD_X;
}
/**
* @return the y of the main board
*/
public int getMainBoardY()
{
return BOARD_Y;
}
/**
* @return the x of the main board
*/
public int getMainBoardX()
{
return BOARD_X;
}
/**
* @return the number of the empty blocks of the board
*/
public int getNumberOfEmptyBlocks()
{
int numberOfEmptyBlocks = 0;
for (int j = 0; j < BOARD_Y; j++)
for (int i = 0; i < BOARD_X; i++)
if (mainBoard[j][i] == 0)
numberOfEmptyBlocks++;
return numberOfEmptyBlocks;
}
/**
* Get the kind of a block of board
*
* @param y : the y of the block
* @param x : the x of the block
*
* @return the kind of the choosen block. < 1: player 1, white>,
* <-1: player 2, black>,
* < 0: empty>
*/
public int getMainBoardBlockKind(int y, int x)
{
return mainBoard[y][x];
}
/**
* Change the main board blocks kind.
*
* @param y : the y of the block
* @param x : the x of the block
* @param kind : the new kind of the block to set. < 1: player 1, white>,
* <-1: player 2, black>,
* < 0: empty>
*/
public void setBoard(int y, int x, int kind)
{
// change main board
mainBoard[y][x] = kind;
char kindChar = '\0';
switch (kind)
{
case 1:
kindChar = 'W';
break;
case -1:
kindChar = 'B';
break;
case 0:
default:
kindChar = ' ';
break;
}
// change visual board
setVisualBoard(y, x, kindChar);
}
/**
* This method rotate the board squers
*
* @param squerNumber : number of the squer that you want to rotate it
* @param way : {@code true}: clockwise, {@code false}: counterClockwise
*/
public void rotate(int squerNumber, boolean way)
{
int x = 0, y = 0;
switch (squerNumber)
{
case 2:
x = 3;
break;
case 3:
y = 3;
break;
case 4:
y = x = 3;
break;
}
int hold = mainBoard[y][x];
if (way)
{
setBoard(y, x, mainBoard[y+2][x]);
setBoard(y+2, x, mainBoard[y+2][x+2]);
setBoard(y+2, x+2, mainBoard[y][x+2]);
setBoard(y, x+2, hold);
hold = mainBoard[y+1][x];
setBoard(y+1, x, mainBoard[y+2][x+1]);
setBoard(y+2, x+1, mainBoard[y+1][x+2]);
setBoard(y+1, x+2, mainBoard[y][x+1]);
setBoard(y, x+1, hold);
}
else
{
setBoard(y, x, mainBoard[y][x+2]);
setBoard(y, x+2, mainBoard[y+2][x+2]);
setBoard(y+2, x+2, mainBoard[y+2][x]);
setBoard(y+2, x, hold);
hold = mainBoard[y+1][x];
setBoard(y+1, x, mainBoard[y][x+1]);
setBoard(y, x+1, mainBoard[y+1][x+2]);
setBoard(y+1, x+2, mainBoard[y+2][x+1]);
setBoard(y+2, x+1, hold);
}
}
/**
* Reset the board for new game
*/
public void reset()
{
for (int j = 0; j < BOARD_Y; j++)
for (int i = 0; i < BOARD_X; i++)
setBoard(j, i, 0);
}
// change the visual board kind
private void setVisualBoard(int y, int x, char kind)
{
if (y > 2)
y = (4 * y) + 3;
else
y = (4 * y) + 1;
if (x > 2)
x = (7 * x) + 5;
else
x = (7 * x) + 1;
for (int j = 0; j < 3; j++)
for (int i = 0; i < 6; i++)
visualBoard[y+j][x+i] = kind;
}
// this method fill the visual board for first time
private void makeVisualBoard()
{
int y = 0, x = 0;
for (int j = 0; j < VISUAL_BOARD_Y; j++)
for (int i = 0; i < VISUAL_BOARD_X; i++)
{
if (j > 13)
y = 2;
else
y = 0;
if (i > 24)
x = 4;
else
x = 0;
if (j == 12 && i == 21)
visualBoard[j][i] = '1';
else if (j == 12 && i == 25)
visualBoard[j][i] = '2';
else if (j== 14 && i == 21)
visualBoard[j][i] = '3';
else if (j == 14 && i == 25)
visualBoard[j][i] = '4';
else if (i == 22 || i == 23 || i == 24)
visualBoard[j][i] = ' ';
else if (j == 13)
visualBoard[j][i] = ' ';
else if ((j-y)%4 == 0 && (i-x)%7 == 0)
visualBoard[j][i] = '•';
else if ((j-y)%4 == 0 && (i-x)%7 != 0)
visualBoard[j][i] = '-';
else if ((j-y)%4 != 0 && (i-x)%7 == 0)
visualBoard[j][i] = '|';
else
visualBoard[j][i] = ' ';
}
}
}