-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiece.jack
More file actions
275 lines (258 loc) · 8.07 KB
/
Piece.jack
File metadata and controls
275 lines (258 loc) · 8.07 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
class Piece {
field int type, color;
// singletons
static Piece blackPawn, whitePawn;
static Piece blackBishop, whiteBishop;
static Piece blackKnight, whiteKnight;
static Piece blackRook, whiteRook;
static Piece blackQueen, whiteQueen;
static Piece blackKing, whiteKing;
// Call initInstances and the getInstance directly, don't
// need to interact with the constructors directly yourself!
function void initInstances() {
let blackPawn = Piece.newPawn(Utils.BLACK());
let blackBishop = Piece.newBishop(Utils.BLACK());
let blackKnight = Piece.newKnight(Utils.BLACK());
let blackRook = Piece.newRook(Utils.BLACK());
let blackQueen = Piece.newQueen(Utils.BLACK());
let blackKing = Piece.newKing(Utils.BLACK());
let whitePawn = Piece.newPawn(Utils.WHITE());
let whiteBishop = Piece.newBishop(Utils.WHITE());
let whiteKnight = Piece.newKnight(Utils.WHITE());
let whiteRook = Piece.newRook(Utils.WHITE());
let whiteQueen = Piece.newQueen(Utils.WHITE());
let whiteKing = Piece.newKing(Utils.WHITE());
return;
}
function void disposeAll() {
do blackPawn.dispose(); do whitePawn.dispose();
do blackRook.dispose(); do whiteRook.dispose();
do blackBishop.dispose(); do whiteBishop.dispose();
do blackKnight.dispose(); do whiteKnight.dispose();
do blackQueen.dispose(); do whiteQueen.dispose();
do blackKing.dispose(); do whiteKing.dispose();
return;
}
// Can use this after you've called initInstances only.
function Piece getInstance(int type, int color) {
if (type = Utils.PAWN()) {
if (color = Utils.WHITE()) {
return whitePawn;
}
return blackPawn;
}
if (type = Utils.BISHOP()) {
if (color = Utils.WHITE()) {
return whiteBishop;
}
return blackBishop;
}
if (type = Utils.KNIGHT()) {
if (color = Utils.WHITE()) {
return whiteKnight;
}
return blackKnight;
}
if (type = Utils.ROOK()) {
if (color = Utils.WHITE()) {
return whiteRook;
}
return blackRook;
}
if (type = Utils.QUEEN()) {
if (color = Utils.WHITE()) {
return whiteQueen;
}
return blackQueen;
}
if (type = Utils.KING()) {
if (color = Utils.WHITE()) {
return whiteKing;
}
return blackKing;
}
do Utils.error("Unknown piece type given.");
return null;
}
constructor Piece newPawn(int c) {
let type = Utils.PAWN();
let color = c;
return this;
}
constructor Piece newBishop(int c) {
let type = Utils.BISHOP();
let color = c;
return this;
}
constructor Piece newKnight(int c) {
let type = Utils.KNIGHT();
let color = c;
return this;
}
constructor Piece newRook(int c) {
let type = Utils.ROOK();
let color = c;
return this;
}
constructor Piece newQueen(int c) {
let type = Utils.QUEEN();
let color = c;
return this;
}
constructor Piece newKing(int c) {
let type = Utils.KING();
let color = c;
return this;
}
method void dispose() {
do Memory.deAlloc(this);
return;
}
method boolean isBlack() {
return color = Utils.BLACK();
}
method boolean isWhite() {
return color = Utils.WHITE();
}
/* Calls corresponding drawing color based on its own location. */
method void drawPiece(int location, int squareColor) {
if (type = Utils.PAWN()) {
if (isWhite()) {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawPawnWW(location);
} else {
do DrawSprites.drawPawnWB(location);
}
} else {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawPawnBW(location);
} else {
do DrawSprites.drawPawnBB(location);
}
}
return;
}
if (type = Utils.KNIGHT()) {
if (isWhite()) {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawKnightWW(location);
} else {
do DrawSprites.drawKnightWB(location);
}
} else {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawKnightBW(location);
} else {
do DrawSprites.drawKnightBB(location);
}
}
return;
}
if (type = Utils.BISHOP()) {
if (isWhite()) {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawBishopWW(location);
} else {
do DrawSprites.drawBishopWB(location);
}
} else {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawBishopBW(location);
} else {
do DrawSprites.drawBishopBB(location);
}
}
return;
}
if (type = Utils.ROOK()) {
if (isWhite()) {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawRookWW(location);
} else {
do DrawSprites.drawRookWB(location);
}
} else {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawRookBW(location);
} else {
do DrawSprites.drawRookBB(location);
}
}
return;
}
if (type = Utils.QUEEN()) {
if (isWhite()) {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawQueenWW(location);
} else {
do DrawSprites.drawQueenWB(location);
}
} else {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawQueenBW(location);
} else {
do DrawSprites.drawQueenBB(location);
}
}
return;
}
if (type = Utils.KING()) {
if (isWhite()) {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawKingWW(location);
} else {
do DrawSprites.drawKingWB(location);
}
} else {
if (squareColor = Utils.WHITE()) {
do DrawSprites.drawKingBW(location);
} else {
do DrawSprites.drawKingBB(location);
}
}
return;
}
do Output.printString("Don't know how to draw piece");
do print();
do Sys.error(-1);
return;
}
method void print() {
var String color;
if (isBlack()) {
let color = "black";
} else {
if (isWhite()) {
let color = "white";
} else {
let color = "unknown color?";
}
}
do Output.printString(color);
do Output.printString(" ");
do Output.printString(getTypeStr());
do Output.println();
return;
}
method String getTypeStr() {
if (type = Utils.PAWN()) {
return "Pawn";
}
if (type = Utils.KNIGHT()) {
return "Knight";
}
if (type = Utils.BISHOP()) {
return "Bishop";
}
if (type = Utils.ROOK()) {
return "Rook";
}
if (type = Utils.QUEEN()) {
return "Queen";
}
if (type = Utils.KING()) {
return "King";
}
return "Unknown?";
}
}