-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGamePlay.jack
176 lines (159 loc) · 5.57 KB
/
GamePlay.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
/* Information about players and who's turn is to move. */
class GamePlay {
// rectangle to draw itself into, with top left corner.
field int x, y, size;
field int crtPlayerToMove;
// if you selected a square and then another, it tells
// the gameplay we want to move from selected square to
// the current square.
field int selRow, selCol; // current selected square
field int crtRow, crtCol; // current square we're on
field String helpText;
// reference to it, so it can tell it when a move has happened.
field Board board;
constructor GamePlay new(int _x, int _y, int _size, Board b) {
let x = _x;
let y = _y;
let size = _size;
// just an acquaintance, doesn't deallocate it itself.
let board = b;
do newGame();
return this;
}
method void dispose() {
do Memory.deAlloc(this);
return;
}
method void newGame() {
let crtPlayerToMove = Utils.WHITE();
do _drawPlayerToMoveIndicator();
let selRow = -1;
let selCol = -1;
// defaults to A1 in left bottom corner
do _writeCurrentSquare(0, 0);
do _writeInHelpTextArea("");
return;
}
// react to user pressing keys by changing its view
// and by letting the board that a move happened, to update
// its state as well.
method void pressedKey(int key) {
var String s1, s2;
if (key = Utils.LEFTARROWKEY()) {
let crtCol = Math.max(0, crtCol - 1);
}
if (key = Utils.UPARROWKEY()) {
let crtRow = Math.min(Utils.CHESSN() - 1, crtRow + 1);
}
if (key = Utils.RIGHTARROWKEY()) {
let crtCol = Math.min(Utils.CHESSN() - 1, crtCol + 1);
}
if (key = Utils.DOWNARROWKEY()) {
let crtRow = Math.max(0, crtRow - 1);
}
if (key = Utils.ESCKEY()) {
do newGame();
do board.newBoard();
}
if (key = Utils.SPACEKEY()) {
// if no current selection, we record it and show on the screen
if (selRow = -1) {
let selRow = crtRow;
let selCol = crtCol;
let s1 = Board.coordsToString(selRow, selCol);
let s2 = Utils.concat("selected ", s1);
do _writeInHelpTextArea(s2);
do s1.dispose();
do s2.dispose();
} else {
// move there
if (board.isLegalMove(selRow, selCol,
crtRow, crtCol,
crtPlayerToMove)) {
// perform the move, then update who's turn to move is.
do board.move(selRow, selCol, crtRow, crtCol);
let crtPlayerToMove = Utils.mod(crtPlayerToMove + 1, 2);
do _drawPlayerToMoveIndicator();
// reset selected square now
let selRow = -1;
let selCol = -1;
do _writeInHelpTextArea("");
// else, reset selection, let user know it was an illegal move.
} else {
let selRow = -1;
let selCol = -1;
do _writeInHelpTextArea("illegal move");
}
}
}
do _writeCurrentSquare(crtRow, crtCol);
return;
}
method void draw() {
do Utils.drawRectangleBorder(x, y, x + size, y + size);
do _drawPlayerToMoveIndicator();
do _writeInPlayerArea(Utils.WHITE(), 2, "Player 1");
do _writeInPlayerArea(Utils.BLACK(), 2, "Player 2");
do Utils.printStringAtPx(
"Crt square: ",
x + Utils.FONTWIDTH(),
(y + (size / 2)) - Utils.FONTHEIGHT()
);
return;
}
method void _drawPlayerToMoveIndicator() {
// erase crt player indicator from both.
do _writeInPlayerArea(Utils.WHITE(), 0, " ");
do _writeInPlayerArea(Utils.BLACK(), 0, " ");
// redraw the active player.
do _writeInPlayerArea(crtPlayerToMove, 0, "<");
return;
}
method void _writeCurrentSquare(int row, int col) {
var String coordsString;
let coordsString = Board.coordsToString(row, col);
// updates the current values
let crtRow = row;
let crtCol = col;
// write crt square on screen for users to see.
do Utils.printStringAtPx(
coordsString,
(x + (size / 2)) + (Utils.FONTWIDTH() * 5),
(y + (size / 2)) - Utils.FONTHEIGHT()
);
do coordsString.dispose();
return;
}
method void _writeInHelpTextArea(String help) {
var String empty;
// write an empty line over it first, to erase any previous message.
let empty = " ";
do Utils.printStringAtPx(
empty,
x + Utils.FONTWIDTH(),
y + (size / 2)
);
// writes help text for user.
do Utils.printStringAtPx(
help,
x + Utils.FONTWIDTH(),
y + (size / 2)
);
let helpText = help;
return;
}
method void _writeInPlayerArea(int color, int offset, String text) {
var int row;
if (color = Utils.WHITE()) {
let row = (y + size) - (Utils.FONTWIDTH() * 2);
} else {
let row = y + Utils.FONTWIDTH();
}
do Utils.printStringAtPx(
text,
x + (Utils.FONTWIDTH() * (offset + 1)),
row
);
return;
}
}