-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.jack
More file actions
112 lines (100 loc) · 2.66 KB
/
Copy pathTile.jack
File metadata and controls
112 lines (100 loc) · 2.66 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
class Tile {
// state is 16 bit int
// save all needed information in those 16 bit
// to reduce heap usage
// increases calculations needed
// -unused-- door state
// 0000 0000 0000 0000
// door
// 3210
// 0 := bottomDoor
// 1:= rightDoor
// state
// 3210
// 0,1,2 are mutually exclusive
// 3 should only be set, when 2 isnt
// (wall cant be a target)
// if 0 is set := free
// if 1 is set := fog
// if 2 is set := wall
// if 3 is set := target
// free := 0
// fog := 1
// wall := 2
field int binaryState;
/*
field int state;
field boolean doorBottom, doorRight;
*/
constructor Tile new(int pstate) {
do setState(pstate);
do closeDoor(1);
do closeDoor(2);
return this;
}
method void dispose() {
do Memory.deAlloc(this);
return;
}
method void makeTarget() {
let binaryState = binaryState | 8;
return;
}
method boolean isTarget() {
return ((binaryState & 8) = 8);
}
method void setState(int newState) {
if(newState = 0) {
let binaryState = binaryState | 1 & (~2) & (~4);
}
if(newState = 1) {
let binaryState = binaryState | 2 & (~1) & (~4);
}
if(newState = 2) {
let binaryState = binaryState | 4 & (~2) & (~1);
}
return;
}
method int getState() {
if((binaryState & 1) = 1) {
return 0;
}
if((binaryState & 2) = 2) {
return 1;
}
if((binaryState & 4) = 4) {
return 2;
}
return -1;
}
method void openDoor(int decider) {
if(decider = 1) {
// bottomDoor bit corresponds to int value 16
let binaryState = binaryState | 16;
}
if(decider = 2) {
// rightDoor bit corresponds to int value 32
let binaryState = binaryState | 32;
}
return;
}
method void closeDoor(int decider) {
if(decider = 1) {
// bottomDoor bit corresponds to int value 16
// keep all bits except the bit for 16 (5th one)
let binaryState = binaryState & (~16);
}
if(decider = 2) {
// bottomDoor bit corresponds to int value 32
// keep all bits except the bit for 32 (6th one)
let binaryState = binaryState & (~32);
}
return;
}
method boolean isOpenBottom() {
return ((binaryState & 16) = 16);
}
method boolean isOpenRight() {
return ((binaryState & 32) = 32);
}
}