-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.js
372 lines (309 loc) · 9.78 KB
/
Maze.js
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
let cols, rows, destination;
//w is the length of the lines
let w = 30;
// array to store in my cell objects
let grid= [];
//current cell and also the player as they will both inherit the first grid object.
//no separate player class needed
let current,player,end;
//visited cells are stored in a stack
let stack = [];
//the win history of the player is stored into a queue
let win_history = []
let move_count = 0;
let reset = false;
let Bool = false;
let start_game = false;
function setup(){
var canvas = createCanvas(600,400);
canvas.parent('sketch-holder');
set_game()
}
function draw(){
background(51);
//displays the grid on canvas
//this for loop does not do anything with the generation of the maze
// generation of maze code
//will only run if the generated property in cell is false as it hasn't been fully generated
//code will not be ran when stack length == 0
//this is to modulate my code and so that other parts of the game code will nt be interfered with this part.
if(current.generated == false){
for (let square of grid){
square.show();
}
//visited becomes true when cell has visited
current.visited = true;
//current cell is highlighted
current.highlight();
//next cell is obtained by going through the checkNeighbours function
let next = current.checkneighbours();
if(next){
//next cell is marked true when visited
next.visited = true;
//put current cell in stack array
stack.push(current);
//remove walls at current and next cell
removeWall(current,next);
//my new current is the next cell
current = next;
frameRate(60);
} else if(stack.length > 0) {
//while there are cells in stack,current will bw top of stack
current = stack.pop();
}
}
//maze generation is completed when there are no more items in stack
if(stack.length == 0 && reset == false){
//limit frame rate for slower animation when the player moves
frameRate(10)
current.generated = true;
start_game = true
}
// once maze has been fully generated, it will highlight player and end at starting position
if(start_game == true){
player.highlight();
end.highlight();
keydown()
//grid lines are drawn over the objects so that it is more clear which walls can be entered to the endpoint
for (let square of grid){
square.show();
}
}
if(reset == true){
//still display maze
for( let maze of grid){
maze.show();
}
//display reset button
let reset_button = createButton('Reset game')
reset_button.position(width, 200)
//go back into set game function
reset_button.mousePressed(set_game);
}
}
function set_game(){
if(reset == true ){
grid = []
stack = []
win_history.push(move_count)
move_count = 0;
Bool = false;
start_game = false;
reset = false
removeElements()
}
cols = floor(width/w);
rows = floor(height/w);
// player = new Player();
//nested loop that will make 400 cell objects-20x20 grid
for(let j = 0 ; j < rows; j++){
for (let i = 0 ; i < cols; i++){
let cell = new Cell(i,j);
//putting the cell object into the array using the push fucntion
grid.push(cell);
}
}
//the current cell starts at first grid cell object
current = grid[0];
start_point = grid[0];
player = new Player();
player.i = 0;
player.j = 0;
let end_num = grid.length -1
end = grid[end_num];
}
function removeWall (cur,nex){
// left or right for the difference between the current cell and next cell
let x = cur.i - nex.i;
if(x == 1){
cur.walls[2] = false;
nex.walls[3] = false;
} else if(x == -1){
cur.walls[3] = false;
nex.walls[2] = false;
}
// above or below for the difference between the current cell and next cell
let y = cur.j - nex.j;
if(y == 1){
cur.walls[0] = false;
nex.walls[1] = false;
} else if(y == -1){
cur.walls[1] = false;
nex.walls[0] = false;
}
}
//this function finds the index of the grid array
function index (i,j){
//validation for edge cases will return a null index
if (i < 0 || j < 0 || i > cols-1 || j > rows -1){
return -1;
}
return i + j * cols;
}
function keydown(){
let move_display;
if(reset == true){
console.log('completed');
} else{
if (keyIsDown(LEFT_ARROW) || keyIsDown(65)){
if(player.i >= 0){
let val_move = player.wall_boundary(2)
if(val_move == true){
player.i -=1;
move_count+=1;
}
}
}
if (keyIsDown(UP_ARROW)|| keyIsDown(87)){
if(player.j >=0){
let val_move = player.wall_boundary(3);
if(val_move == true){
player.j -=1;
move_count+=1;
}
}
}
if (keyIsDown(DOWN_ARROW) || keyIsDown(83)){
if(player.j < rows -1){
let val_move = player.wall_boundary(4)
if(val_move == true){
player.j +=1;
move_count+=1;
}
}
}
if(keyIsDown(RIGHT_ARROW) || keyIsDown(68)){
if(player.i < cols -1 ){
let val_move = player.wall_boundary(1)
if( val_move == true){
player.i +=1;
move_count+=1;
}
}
}
removeElements();
if(win_history.length > 0){
let spacing = 3
let history_str = 'Amount of moves won by:'
let print_header = createElement('h2',history_str);
print_header.position(width,10);
for(let i = 0; i < win_history.length; i++){
let print_win = createElement('p4',win_history[i]);
print_win.position(width,10*(i+5)+spacing)
spacing+=3;
}
}
let moveLog_str = 'Move Log: '
let moveLog_prnt = createElement('h2',moveLog_str)
moveLog_prnt.position(width ,100)
move_display = createElement('h2', move_count);
move_display.position(width,120);
if(player.i == end.i && player.j == end.j){
// let win_line = "You're winner"
// let congratualtion = createElement('h2', win_line);
// congratualtion.position(width+20,20);
start_game = false;
reset = true;
}
}
}
function keyPressed(){
if(start_game == true){
//key pressed is a function that is entered once a key is pressed
let move_display;
if(reset == true){
//once the game is completed
//moves will not be logged anymore
console.log('completed')
} else{
if (keyCode == RIGHT_ARROW || keyCode == 68){
//boundary constraint will be the first validation
//contrains the player for the right side
if(player.i < cols -1 ){
// if player is with in the valid region then launch wall boundary
//val_move is the returned boolean from wall boundary
//1 is the key code for right
let val_move = player.wall_boundary(1)
//if val_move is true / valid then increment move counter by 1 and move player position
if( val_move == true){
player.i +=1;
move_count+=1;
}
}
}
if (keyCode == LEFT_ARROW || keyCode == 65){
//boundary constraint will be the first validation
//contrains the player for the left side
if(player.i >= 0){
// if player is with in the valid region then launch wall boundary
//val_move is the returned boolean from wall boundary
//2 is the key code for left
let val_move = player.wall_boundary(2)
if(val_move == true){
//player will move in the negative of right
player.i -=1;
move_count+=1;
}
}
}
if (keyCode == UP_ARROW|| keyCode == 87){
//boundary constraint will be the first validation
//contrains the player for the top
if(player.j >=0){
// if player is with in the valid region then launch wall boundary
//val_move is the returned boolean from wall boundary
//3 is the key code for up
let val_move = player.wall_boundary(3);
if(val_move == true){
//player j will be changed
player.j -=1;
move_count+=1;
}
}
}
if (keyCode == DOWN_ARROW || keyCode == 83){
//boundary constraint will be the first validation
//contrains the player for down
if(player.j < rows -1){
// if player is with in the valid region then launch wall boundary
//val_move is the returned boolean from wall boundary
//4 is the key code for down
let val_move = player.wall_boundary(4)
if(val_move == true){
//player j will change to the negative of up
player.j +=1;
move_count+=1;
}
}
}
removeElements();
//if there is an element inside win history array then display array
if(win_history.length > 0){
//this is an incremental spacing variable to space out each result
let spacing = 3
let history_str = 'Amount of moves won by:'
let print_header = createElement('h2',history_str);
print_header.position(width,10);
//loop through the win history array
for(let i = 0; i < win_history.length; i++){
//at each element, make a p4 element
let print_win = createElement('p4',win_history[i]);
//the positon of the p4 element would need to change downwards
print_win.position(width,10*(i+5)+spacing)
spacing+=3;
}
}
let moveLog_str = 'Move Log: '
let moveLog_prnt = createElement('h2',moveLog_str)
moveLog_prnt.position(width,100)
move_display = createElement('h2', move_count);
move_display.position(width,120);
if(player.i == end.i && player.j == end.j){
reset = true;
start_game = false;
}
}
}
}
// set move log
// removes all html elements and re writes it all out again