-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack.jack
More file actions
58 lines (49 loc) · 1.14 KB
/
Stack.jack
File metadata and controls
58 lines (49 loc) · 1.14 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
/**
* Models the stack as a 2D array
*/
class Stack {
field Array stack;
constructor Stack new() {
var int r;
let stack = Array.new(Constants.LEVELS());
let r = 0;
while (r < Constants.LEVELS()) {
let stack[r] = Array.new(Constants.COLS());
let r = r + 1;
}
return this;
}
/** Adds a new row to the stack, keeping only blocks that are stackable */
method int add(Array row, int level) {
var int blocksStacked, i;
var Array top, newTop;
let top = stack[level - 1];
let newTop = stack[level];
let i = 0;
let blocksStacked = 0;
if (level = 0) {
let newTop = stack[0];
while (i < Constants.COLS()) {
let newTop[i] = row[i];
let i = i + 1;
}
let blocksStacked = 3;
} else {
while (i < Constants.COLS()) {
let newTop[i] = top[i] & row[i];
if (newTop[i]) {
let blocksStacked = blocksStacked + 1;
}
let i = i + 1;
}
}
return blocksStacked;
}
method Array getRow(int level) {
return stack[level];
}
method void dispose() {
do Memory.deAlloc(this);
return;
}
}