-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.jack
256 lines (224 loc) · 7.34 KB
/
Board.jack
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
class Board {
// bottom left corner where board starts drawing from
field int startRow, startCol;
field int rows, cols;
field int n;
field Array board;
constructor Board new(int x, int y) {
var int i;
var Piece b;
do Screen.clearScreen();
let startRow = x;
let startCol = y;
let rows = 9; // includes coordinate system and pieces
let cols = 9;
// initializes the pieces as singletons, so we can reuse them on the
// board as many times as we want w/o having to create new memory
// entries for them.
do Piece.initInstances();
// allocate memory for the board.
let n = 8;
let i = 0;
let board = Array.new(n);
while (i < n) {
let board[i] = Array.new(n);
let i = i + 1;
}
// put pieces on the board
do initBoard();
return this;
}
method void dispose() {
var int i, j;
var Array a;
var Piece p;
// let the pieces know that they can dispose themselves
// now that we're disposing of the board.
do Piece.disposeAll();
// dispose of the board array too
let i = 0;
while (i < n) {
let a = board[i];
do a.dispose();
let i = i + 1;
}
do board.dispose();
do Memory.deAlloc(this);
return;
}
method void newBoard() {
do initBoard();
do drawBoard();
return;
}
method Piece Board(int i, int j) {
var Piece p;
let p = board[i];
let p = p[j];
return p;
}
method void setBoard(int i, int j, Piece p) {
var Array tmp;
let tmp = board[i];
let tmp[j] = p;
return;
}
method int mirror(int i) {
return (n - i) - 1;
}
method int getLocation(int i, int j) {
return Utils.getLocation(
(startRow - i), // board is drawing itself upwards
(startCol + j) // and to the left
);
}
// Pieces are inwards more due to the fact that first we
// draw board numbers.
method int getPieceLocation(int i, int j) {
return getLocation(i + 1, j + 1);
}
method void initBoard() {
// fill board with null.
do _initNull();
// fill board with pieces.
do _initPawns(1, Utils.WHITE());
do _initPawns(mirror(1), Utils.BLACK());
do _initRestPieces(0, Utils.WHITE());
do _initRestPieces(mirror(0), Utils.BLACK());
return;
}
method void draw() {
do drawBoard();
do drawAroundBoard();
return;
}
method void drawBoard() {
var int i, j;
let i = 0;
while (i < n) {
let j = 0;
while (j < n) {
do drawBoardSquare(i, j);
let j = j + 1;
}
let i = i + 1;
}
return;
}
method void drawBoardSquare(int i, int j) {
var Piece p;
var int location, squareColor;
let location = getPieceLocation(i, j);
let squareColor = Board.squareColor(i, j);
let p = Board(i, j);
// draw empty square if no piece.
if (p = null) {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawWhite(location);
} else {
do DrawSprites.drawBlack(location);
}
} else {
do p.drawPiece(location, squareColor);
}
return;
}
method void drawAroundBoard() {
do DrawSprites.draw1(getLocation(1, 0));
do DrawSprites.draw2(getLocation(2, 0));
do DrawSprites.draw3(getLocation(3, 0));
do DrawSprites.draw4(getLocation(4, 0));
do DrawSprites.draw5(getLocation(5, 0));
do DrawSprites.draw6(getLocation(6, 0));
do DrawSprites.draw7(getLocation(7, 0));
do DrawSprites.draw8(getLocation(8, 0));
do DrawSprites.drawA(getLocation(0, 1));
do DrawSprites.drawB(getLocation(0, 2));
do DrawSprites.drawC(getLocation(0, 3));
do DrawSprites.drawD(getLocation(0, 4));
do DrawSprites.drawE(getLocation(0, 5));
do DrawSprites.drawF(getLocation(0, 6));
do DrawSprites.drawG(getLocation(0, 7));
do DrawSprites.drawH(getLocation(0, 8));
return;
}
method boolean isLegalMove(int x1, int y1, int x2, int y2,
int playerToMove) {
// TODO: need to check that the piece can do that
// and that the current board state does allow that,
// that no own/adversary pieces are in destination's way.
// TODO: also check that the play is moving his own pieces! :)
// and also that there's a piece at the source, that
// you're actually moving something.
// TODO: special rule for promotion.
// TODO: is someone in check? can you actually move? does it block the
// existing check?
return true;
}
method void move(int r1, int c1, int r2, int c2) {
var Piece p;
let p = Board(r1, c1);
do setBoard(r1, c1, null);
do setBoard(r2, c2, p);
do drawBoardSquare(r1, c1);
do drawBoardSquare(r2, c2);
return;
}
method void _initNull() {
var int i, j;
let i = 0;
while (i < n) {
let j = 0;
while (j < n) {
do setBoard(i, j, null);
let j = j + 1;
}
let i = i + 1;
}
return;
}
method void _initPawns(int i, int color) {
var int j;
let j = 0;
while (j < n) {
do setBoard(i, j, Piece.getInstance(Utils.PAWN(), color));
let j = j + 1;
}
return;
}
method void _initRestPieces(int i, int color) {
do setBoard(i, 0, Piece.getInstance(Utils.ROOK(), color));
do setBoard(i, 1, Piece.getInstance(Utils.KNIGHT(), color));
do setBoard(i, 2, Piece.getInstance(Utils.BISHOP(), color));
do setBoard(i, 3, Piece.getInstance(Utils.QUEEN(), color));
do setBoard(i, 4, Piece.getInstance(Utils.KING(), color));
do setBoard(i, 5, Piece.getInstance(Utils.BISHOP(), color));
do setBoard(i, 6, Piece.getInstance(Utils.KNIGHT(), color));
do setBoard(i, 7, Piece.getInstance(Utils.ROOK(), color));
return;
}
function int squareColor(int x, int y) {
if (Utils.mod(x, 2) = Utils.mod(y, 2)) {
return Utils.BLACK();
} else {
return Utils.WHITE();
}
}
// returns coordinate of a square: e.g. A2, B6 etc.
function String coordsToString(int row, int col) {
var String files, ranks, coordsString, msg;
// Sanity checking bounds of crt square.
let msg = "coordsToString: row or col out of bounds";
if (row < 0) { do Utils.error(msg); }
if (row > (Utils.CHESSN() - 1)) { do Utils.error(msg); }
if (col < 0) { do Utils.error(msg); }
if (col > (Utils.CHESSN() - 1)) { do Utils.error(msg); }
// Form text to show on display.
let files = "ABCDEFGH";
let ranks = "12345678";
let coordsString = String.new(2);
do coordsString.appendChar(files.charAt(col));
do coordsString.appendChar(ranks.charAt(row));
return coordsString;
}
}