-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.jack
More file actions
175 lines (159 loc) · 3.48 KB
/
Copy pathPlayer.jack
File metadata and controls
175 lines (159 loc) · 3.48 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
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
class Player {
field int x,y;
field int direction;
field Maze maze;
constructor Player new(int px, int py, Maze pmaze) {
let x = px;
let y = py;
let direction = 1;
let maze = pmaze;
return this;
}
method int getDirection() {
return direction;
}
method int getX() {
return x;
}
method int getY() {
return y;
}
method void turnTo(int pdirection) {
let direction = pdirection;
do look();
return;
}
method void dispose() {
do Memory.deAlloc(this);
return;
}
method void turnClockwise() {
if(direction = 3) {
let direction = 0;
} else {
let direction = direction + 1;
}
do look();
return;
}
method void turnCounterClockwise() {
if(direction = 0) {
let direction = 3;
} else {
let direction = direction - 1;
}
do look();
return;
}
method void move() {
if(~maze.canMove()) {
return;
}
if(direction = 0) {
do moveNorth();
}
if(direction = 1) {
do moveEast();
}
if(direction = 2) {
do moveSouth();
}
if(direction = 3) {
do moveWest();
}
return;
}
method void moveNorth() {
var int newY;
let newY = y - 1;
if(maze.isFree(x, newY)) {
let y = newY;
do maze.setTile(x, newY, 0);
}
return;
}
method void moveSouth() {
var int newY;
let newY = y + 1;
if(maze.isFree(x, newY)) {
let y = newY;
do maze.setTile(x, newY, 0);
}
return;
}
method void moveWest() {
var int newX;
let newX = x - 1;
if(maze.isFree(newX, y)) {
let x = newX;
do maze.setTile(newX, y, 0);
}
return;
}
method void moveEast() {
var int newX;
let newX = x + 1;
if(maze.isFree(newX, y)) {
let x = newX;
do maze.setTile(newX, y, 0);
}
return;
}
// looks into current direction until it sees
// a wall and discovers all tiles between.
method void look() {
if(direction = 0) {
do lookNorth();
}
if(direction = 1) {
do lookEast();
}
if(direction = 2) {
do lookSouth();
}
if(direction = 3) {
do lookWest();
}
return;
}
method void lookNorth() {
var int oldY;
let oldY = y;
while(maze.canMove()) {
do moveNorth();
}
let y = oldY;
do maze.update();
return;
}
method void lookEast() {
var int oldX;
let oldX = x;
while(maze.canMove()) {
do moveEast();
}
let x = oldX;
do maze.update();
return;
}
method void lookSouth() {
var int oldY;
let oldY = y;
while(maze.canMove()) {
do moveSouth();
}
let y = oldY;
do maze.update();
return;
}
method void lookWest() {
var int oldX;
let oldX = x;
while(maze.canMove()) {
do moveWest();
}
let x = oldX;
do maze.update();
return;
}
}