-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayfield.jack
233 lines (220 loc) · 4.97 KB
/
Playfield.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
/** Implements a playfield */
class Playfield {
field int width;
field int height;
field Array grid;
/** Constructs a new Playfield. */
constructor Playfield new() {
var int i;
let width = 10;
let height = 18;
let grid = Array.new(height);
let i = 0;
while (i < height) {
let grid[i] = Array.new(width);
let i = i + 1;
}
return this;
}
/** Disposes this playfield. */
method void dispose() {
do Memory.deAlloc(this);
return;
}
/** Clears the playfield. */
method void clear() {
var int i;
var int j;
var Array row;
var Square s;
let i = 0;
while (i < height) {
let row = grid[i];
let j = 0;
while (j < width) {
if (~(row[j] = null)) {
let s = row[j];
do s.erase();
let row[j] = null;
}
let j = j + 1;
}
let i = i + 1;
}
return;
}
/** Gets the playfield width. */
method int getWidth() {
return width;
}
/** Gets the playfield height. */
method int getHeight() {
return height;
}
/** Gets the grid. */
method Array getGrid() {
return grid;
}
/** Sets a tetromino on the playfield. */
method void setTetromino(Tetromino t) {
var int i;
var int xpos;
var int ypos;
var Array row;
var Array squares;
var Square square;
let i = 0;
let squares = t.getSquares();
while (i < 4) {
let square = squares[i];
let ypos = square.getY();
let row = grid[ypos];
let row[square.getX()] = square;
let i = i + 1;
}
return;
}
/** Checks if there is space for a tetromino in the given direction */
method boolean isSpace(Tetromino t, int direction) {
var Array row;
var Array squares;
var Square square;
var int xpos;
var int ypos;
var int xdest;
var int ydest;
var boolean isSpace;
var int i;
let squares = t.getSquares();
let isSpace = true;
let i = 0;
if (direction = 2) {
while (isSpace & (i < 4)) {
let square = squares[i];
let xpos = square.getX();
let ydest = square.getY() + 1;
if (ydest < height) {
let row = grid[ydest];
if (~(row[xpos] = null)) {
let isSpace = false;
}
} else {
let isSpace = false;
}
let i = i + 1;
}
}
if (direction = 3) {
while (isSpace & (i < 4)) {
let square = squares[i];
let ypos = square.getY();
let xdest = square.getX() - 1;
if (ypos > 0) {
if (xdest > -1) {
let row = grid[ypos];
if (~(row[xdest] = null)) {
let isSpace = false;
}
} else {
let isSpace = false;
}
}
let i = i + 1;
}
}
if (direction = 4) {
while (isSpace & (i < 4)) {
let square = squares[i];
let ypos = square.getY();
let xdest = square.getX() + 1;
if (ypos > 0) {
if (xdest < width) {
let row = grid[ypos];
if (~(row[xdest] = null)) {
let isSpace = false;
}
} else {
let isSpace = false;
}
}
let i = i + 1;
}
}
return isSpace;
}
/** Clears the bottom row after scoring. */
method void clearRow(int rowIndex) {
var int j;
var Array row;
var Square s;
let j = 0;
let row = grid[rowIndex];
while (j < width) {
let s = row[j];
do s.erase();
do s.dispose();
let s = null;
let row[j] = null;
let j = j + 1;
}
return;
}
/** Shifts a row down */
method void shiftRow(int rowIndex) {
var int j;
var Array row;
var Array prevRow;
var Square s;
let row = grid[rowIndex];
let prevRow = grid[rowIndex - 1];
let j = 0;
while (j < width) {
if (~(prevRow[j] = null)) {
let s = prevRow[j];
do s.erase();
do s.setY(s.getY() + 1);
do s.draw();
let prevRow[j] = null;
let row[j] = s;
}
let j = j + 1;
}
return;
}
/** Checks if there is space for a new tetromino */
method boolean isFull() {
var Array row;
var boolean full;
let full = false;
let row = grid[1];
if (row[4] | row[5]) {
let full = true;
}
return full;
}
/** Checks if the tetromino can be rotated. */
method boolean canRotate(Tetromino tetromino) {
var int type;
var int orientation;
var boolean canRotate;
var Array squares;
var Square s;
let type = tetromino.getType();
let orientation = tetromino.getOrientation();
let squares = tetromino.getSquares();
let canRotate = true;
if (type = 0) {
let canRotate = false;
}
if (type = 1) {
if (orientation = 1) {
let s = squares[0];
if (s.getX() > 6) {
let canRotate = false;
}
}
}
// TODO: check if tetromino can actually be rotated
return canRotate;
}
}