-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessGame.jack
88 lines (75 loc) · 2.24 KB
/
ChessGame.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
class ChessGame {
field boolean exit;
field Board board;
field GamePlay gamePlay;
static ChessGame instance;
/* Singleton methods, don't call constructor directly. */
function void newInstance() {
let instance = ChessGame.new();
return;
}
function ChessGame getInstance() {
return instance;
}
constructor ChessGame new() {
// set bottom left corner in the 32 x 16 grid.
// knowing that the board will take 9 x 9 space.
let board = Board.new(13, (32-9) / 2);
// starts at same level as board and has a size of 8 pieces,
// same as the chessboard.
let gamePlay = GamePlay.new(
22 * Utils.WORD(),
5 * Utils.WORD(),
Utils.CHESSN() * Utils.WORD(),
board
);
let exit = false;
return this;
}
method void dispose() {
do board.dispose();
do gamePlay.dispose();
do Memory.deAlloc(this);
return;
}
/* ChessGame loop */
method void run() {
var char key, crtKey;
do shortcutsDraw();
do board.draw();
do gamePlay.draw();
while (~exit) {
// wait for key to be pressed.
while ((key = 0) & (~exit)) {
let key = Keyboard.keyPressed();
do Sys.wait(50);
}
let crtKey = key;
// Waits for the key to be released.
while ((~(key = 0)) & (~exit)) {
let key = Keyboard.keyPressed();
do Sys.wait(50);
}
// forward exaxct key to gameplay.
do gamePlay.pressedKey(crtKey);
}
return;
}
method void shortcutsDraw() {
var int n, i, rowStart;
var Array msgs;
let rowStart = 1;
let n = 4;
let msgs = Array.new(n);
let msgs[0] = "Shortcuts:";
let msgs[1] = "arrow keys: navigate between pieces to move";
let msgs[2] = "space: select square to move a piece to/from";
let msgs[3] = "escape: start a new game";
let i = 0;
while (i < n) {
do Utils.printStringCentered(msgs[i], rowStart + i);
let i = i + 1;
}
return;
}
}