-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmaze.js
executable file
·1443 lines (1260 loc) · 30.4 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
****************************************************
* maze.js
*
* Author: <[email protected]>
*
* Copyright 2011 Brandon Blodget.
*
* This script defines an API for drawing a
* Micromouse maze and controlling a mouse
* inside the generated maze.
* It requires an html5 capable web browser.
*
* License:
*
* This file is part of "MicromouseSim"
*
* "MicromouseSim" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "MicromouseSim" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "MicromouseSim". If not, see <http://www.gnu.org/licenses/>.
*
****************************************************
*/
// The Micromouse object. This is the only variable
// we export to the global namespace. The API is
// made available through this object.
var mouse;
if (!mouse) {
mouse = {};
}
// start closure
(function () {
/*
****************************************************
* Global variables to this closure
****************************************************
*/
// Note: For the maze the top left cell is
// (0,0)
var cWidth; // the width of the maze in cells
var cHeight; // the height of the maze in cells
var canvas; // the html5 canvas we draw on
var ctx; // the canvas context
var pWidth; // the width of maze in pixels
var pHeight; // the height of the maze in pixels
var pCellWidth; // width of a cell in pixels
var pCellHeight; // height of a cell in pixels
var speed; // the number of steps it takes to move one cell
var maze; // data structure the reps the maze
var memMaze; // the mouses memory of the maze.
//var memMazeValue; // mouses memory of cell values
//var memMazeWall; // mouses memory of the walls
var memMouseX; // mouse x pos in cells
var memMouseY; // mouse y pos in cells
var memMouseDir; // "N", "E", "S", or "W"
var cMouseX; // mouse x pos in cells
var cMouseY; // mouse y pos in cells
var pMouseX; // mouse x pos in pixels
var pMouseY; // mouse y pos in pixels
var tpMouseX; // target x pos in pixels
var tpMouseY; // target y pos in pixels
var mouseDir; // "N", "E", "S", or "W"
var aMouseDir; // the angle direction of mouse
var taMouseDir; // the target angle direction of mouse
var mRadius; // mouse radius.
var turnDir; // "R"ight, "L"eft, "N"one.
var driver; // user code that drives the mouse
var maze_sel; // text id of currently selected maze.
var running; // boolean. True if mouse is running
var timer_id; // id of the running timer.
var stepMode; // true if we are in stepping mode.
var ssButton; // the start stop button
var moveCount; // number of moves from start square
// constants
var turnAmount = 10; // speed at which the mouse turns
var incAmount = 5; // speed at which the mouse move
var outOfBounds = 401; // constant for out of bound values
// memMaze array constants
var NORTH = 0;
var EAST = 1;
var SOUTH = 2;
var WEST = 3;
var VISIT = 4;
var DATA = 5;
/*
****************************************************
* Public Data structures
****************************************************
*/
// public pointer to the mouses memory.
//if (typeof mouse.newMaze !== 'function') {
//mouse.memMaze = memMaze;
//}
/*
****************************************************
* Public API Functions
****************************************************
*/
// Creates a new maze and draws it to the
// canvas with the Id=maze
// ss_button: jQuery start/stop button
// maze_sel: string of selected maze.
if (typeof mouse.newMaze !== 'function') {
mouse.newMaze = function(ss_button,maze_sel) {
ssButton = ss_button;
//setRunning(false);
running = false;
// default maze size is 16x16 cells
cWidth = 16;
cHeight = 16;
canvas = document.getElementById("maze");
ctx = canvas.getContext("2d");
pWidth = canvas.width;
pHeight = canvas.height;
pCellWidth = pWidth/cWidth;
pCellHeight = pHeight/cHeight;
mouse.setSpeed(10);
// init mouse starting mostion
// bottom left square
setHomePosition();
// compute mouse radius
if (pCellWidth > pCellHeight) {
mRadius = Math.floor(pCellHeight/2) - 5;
} else {
mRadius = Math.floor(pCellWidth/2) - 5;
}
mouse.loadMaze(maze_sel);
};
}
if (typeof mouse.fwd !== 'function') {
mouse.fwd = function(cells) {
var num = cells || 1;
var i;
if (mouse.isHome()) {moveCount=0;}
moveCount += num;
for (i=0; i<num; i++) {
switch (mouseDir) {
case "N" :
if (maze[cMouseY][cMouseX].indexOf("N") !== -1) {
cMouseY = cMouseY - 1;
}
break;
case "E" :
if (maze[cMouseY][cMouseX].indexOf("E") !== -1) {
cMouseX = cMouseX + 1;
}
break;
case "S" :
if (maze[cMouseY][cMouseX].indexOf("S") !== -1) {
cMouseY = cMouseY + 1;
}
break;
case "W" :
if (maze[cMouseY][cMouseX].indexOf("W") !== -1) {
cMouseX = cMouseX - 1;
}
break;
}
}
tpMouseX = cell2px();
tpMouseY = cell2py();
memMouseX = cMouseX;
memMouseY = cMouseY;
memMouseDir = mouseDir;
memUpdate();
};
}
if (typeof mouse.back !== 'function') {
mouse.back = function(cells) {
var num = cells || 1;
var i;
if (mouse.isHome()) {moveCount=0;}
moveCount += num;
for (i=0; i<num; i++) {
switch (mouseDir) {
case "N" :
if (maze[cMouseY][cMouseX].indexOf("S") !== -1) {
cMouseY = cMouseY + 1;
}
break;
case "E" :
if (maze[cMouseY][cMouseX].indexOf("W") !== -1) {
cMouseX = cMouseX - 1;
}
break;
case "S" :
if (maze[cMouseY][cMouseX].indexOf("N") !== -1) {
cMouseY = cMouseY - 1;
}
break;
case "W" :
if (maze[cMouseY][cMouseX].indexOf("E") !== -1) {
cMouseX = cMouseX + 1;
}
break;
}
}
tpMouseX = cell2px();
tpMouseY = cell2py();
memMouseX = cMouseX;
memMouseY = cMouseY;
memMouseDir = mouseDir;
memUpdate();
};
}
if (typeof mouse.right !== 'function') {
mouse.right = function(turns) {
var num = turns || 1;
var i;
if (mouse.isHome()) {moveCount=0;}
moveCount += num;
turnDir = "R";
taMouseDir = taMouseDir + (90*num);
for (i=0; i<num; i++) {
switch (mouseDir) {
case "N" :
mouseDir = "E"; break;
case "E" :
mouseDir = "S"; break;
case "S" :
mouseDir = "W"; break;
case "W" :
mouseDir = "N"; break;
}
}
memMouseX = cMouseX;
memMouseY = cMouseY;
memMouseDir = mouseDir;
};
}
if (typeof mouse.left !== 'function') {
mouse.left = function(turns) {
var num = turns || 1;
var i;
if (mouse.isHome()) {moveCount=0;}
moveCount += num;
turnDir = "L";
taMouseDir = taMouseDir - (90*num);
for (i=0; i<num; i++) {
switch (mouseDir) {
case "N" :
mouseDir = "W"; break;
case "E" :
mouseDir = "N"; break;
case "S" :
mouseDir = "E"; break;
case "W" :
mouseDir = "S"; break;
}
}
memMouseX = cMouseX;
memMouseY = cMouseY;
memMouseDir = mouseDir;
};
}
if (typeof mouse.isPathLeft !== 'function') {
mouse.isPathLeft = function() {
var goodDir = maze[cMouseY][cMouseX];
switch (mouseDir) {
case "N" :
if (goodDir.indexOf("W") === -1) {
return false;
}
break;
case "E" :
if (goodDir.indexOf("N") === -1) {
return false;
}
break;
case "S" :
if (goodDir.indexOf("E") === -1) {
return false;
}
break;
case "W" :
if (goodDir.indexOf("S") === -1) {
return false;
}
break;
}
return true;
};
}
if (typeof mouse.isWallLeft !== 'function') {
mouse.isWallLeft = function() {
return !(mouse.isPathLeft());
};
}
if (typeof mouse.isPathRight !== 'function') {
mouse.isPathRight = function() {
var goodDir = maze[cMouseY][cMouseX];
switch (mouseDir) {
case "N" :
if (goodDir.indexOf("E") === -1) {
return false;
}
break;
case "E" :
if (goodDir.indexOf("S") === -1) {
return false;
}
break;
case "S" :
if (goodDir.indexOf("W") === -1) {
return false;
}
break;
case "W" :
if (goodDir.indexOf("N") === -1) {
return false;
}
break;
}
return true;
};
}
if (typeof mouse.isWallRight !== 'function') {
mouse.isWallRight = function() {
return !(mouse.isPathRight());
};
}
if (typeof mouse.isPathFwd !== 'function') {
mouse.isPathFwd = function() {
var goodDir = maze[cMouseY][cMouseX];
switch (mouseDir) {
case "N" :
if (goodDir.indexOf("N") === -1) {
return false;
}
break;
case "E" :
if (goodDir.indexOf("E") === -1) {
return false;
}
break;
case "S" :
if (goodDir.indexOf("S") === -1) {
return false;
}
break;
case "W" :
if (goodDir.indexOf("W") === -1) {
return false;
}
break;
}
return true;
};
}
if (typeof mouse.isWallFwd !== 'function') {
mouse.isWallFwd = function() {
return !(mouse.isPathFwd());
};
}
if (typeof mouse.isPathBack !== 'function') {
mouse.isPathBack = function() {
var goodDir = maze[cMouseY][cMouseX];
switch (mouseDir) {
case "N" :
if (goodDir.indexOf("S") === -1) {
return false;
}
break;
case "E" :
if (goodDir.indexOf("W") === -1) {
return false;
}
break;
case "S" :
if (goodDir.indexOf("N") === -1) {
return false;
}
break;
case "W" :
if (goodDir.indexOf("E") === -1) {
return false;
}
break;
}
return true;
};
}
if (typeof mouse.isWallBack !== 'function') {
mouse.isWallBack = function() {
return !(mouse.isPathBack());
};
}
if (typeof mouse.start !== 'function') {
mouse.start = function() {
stepMode = false;
if (driver && !running) {
timer_id = setInterval(update,20);
setRunning(true);
}
};
}
if (typeof mouse.stop !== 'function') {
mouse.stop = function() {
stepMode = true;
};
}
if (typeof mouse.step !== 'function') {
mouse.step = function() {
stepMode = true;
if (driver && !running) {
timer_id = setInterval(update,20);
setRunning(true);
}
};
}
// number of moves made since last at home.
if (typeof mouse.moveCount !== 'function') {
mouse.moveCount = function() {
return moveCount;
};
}
// Resets mouse memory and returns it home.
if (typeof mouse.reset !== 'function') {
mouse.reset = function() {
if (driver.load) {
driver.load();
}
mouse.home();
mouse.memClear();
};
}
// Sets the mouses speed.
// Indicates number of steps required to move
// one cell.
// 1 is fastest.
if (typeof mouse.setSpeed !== 'function') {
mouse.setSpeed = function(speedp) {
speed = speedp;
incAmount = pCellWidth/speed;
turnAmount = 90/speed;
};
}
// returns true on success else false
if (typeof mouse.loadDriver !== 'function') {
mouse.loadDriver = function(driverp) {
driver = driverp;
// make sure a maze is loaded.
if (maze_sel && maze_sel !== "loading") {
/*
if (driver.load) {
driver.load();
}
*/
//mouse.home();
//mouse.memClear(); // clear the mouses memory.
mouse.reset();
return true;
} else {
return false;
}
};
}
if (typeof mouse.loadMaze !== 'function') {
mouse.loadMaze = function(maze_selp) {
var maze_json = "mazes_json/" + maze_selp + ".json";
maze_sel = "loading";
// change menu selection
$("#maze_sel").val(maze_selp).attr('selected','selected');
$.getJSON(maze_json, function(json) {
maze_sel = maze_selp;
maze = json;
drawMaze();
drawMouse();
// clear the mouses memory
mouse.home();
mouse.memClear();
});
};
}
if (typeof mouse.x !== 'function') {
mouse.x = function() {
return cMouseX;
};
}
if (typeof mouse.y !== 'function') {
mouse.y = function() {
return cMouseY;
};
}
if (typeof mouse.heading !== 'function') {
mouse.heading = function() {
return mouseDir;
};
}
if (typeof mouse.home !== 'function') {
mouse.home = function() {
mouse.stop();
eraseMouse();
setHomePosition();
clearTimer();
drawMouse();
moveCount = 0;
};
}
if (typeof mouse.isHome !== 'function') {
mouse.isHome = function() {
if (cMouseX === 0 &&
cMouseY === 15) { // &&
// mouseDir === "N") {
return true;
} else {
return false;
}
};
}
if (typeof mouse.isGoal !== 'function') {
mouse.isGoal = function() {
if ((cMouseX === 7 || cMouseX === 8) &&
(cMouseY === 7 || cMouseY === 8)) {
return true;
} else {
return false;
}
};
}
// Mouse memory functions
//memIsWallLeft(): Returns true if a wall to the left has been remembered.
if (typeof mouse.memIsWallLeft !== 'function') {
mouse.memIsWallLeft = function() {
var i = left2Index(memMouseDir);
return !(memMaze[memMouseY][memMouseX][dir]);
};
}
//memIsPathLeft(): Returns true if a wall to the left has not been remembered.
if (typeof mouse.memIsPathLeft !== 'function') {
mouse.memIsPathLeft = function() {
var i = left2Index(memMouseDir);
return memMaze[memMouseY][memMouseX][dir];
};
}
//memIsWallRight(): Returns true if a wall to the right has been remembered.
if (typeof mouse.memIsWallRight !== 'function') {
mouse.memIsWallRight = function() {
var i = right2Index(memMouseDir);
return !(memMaze[memMouseY][memMouseX][dir]);
};
}
//memIsPathRight(): Returns true if a wall to the right has not been remembered.
if (typeof mouse.memIsPathRight !== 'function') {
mouse.memIsPathRight = function() {
var i = right2Index(memMouseDir);
return memMaze[memMouseY][memMouseX][dir];
};
}
//memIsWallFwd(): Returns true if a wall to the forward has been remembered.
if (typeof mouse.memIsWallFwd !== 'function') {
mouse.memIsWallFwd = function() {
var i = fwd2Index(memMouseDir);
return !(memMaze[memMouseY][memMouseX][dir]);
};
}
//memIsPathFwd(): Returns true if a wall to the forward has not been remembered.
if (typeof mouse.memIsPathFwd !== 'function') {
mouse.memIsPathFwd = function() {
var i = fwd2Index(memMouseDir);
return memMaze[memMouseY][memMouseX][dir];
};
}
//memIsWallBack(): Returns true if a wall to the back has been remembered.
if (typeof mouse.memIsWallBack !== 'function') {
mouse.memIsWallBack = function() {
var i = back2Index(memMouseDir);
return !(memMaze[memMouseY][memMouseX][dir]);
};
}
//memIsPathBack(): Returns true if a wall to the back has not been remembered.
if (typeof mouse.memIsPathBack !== 'function') {
mouse.memIsPathBack = function() {
var i = back2Index(memMouseDir);
return memMaze[memMouseY][memMouseX][dir];
};
}
//memSetData(data): Store data in the current cell.
if (typeof mouse.memSetData !== 'function') {
mouse.memSetData = function(data) {
memMaze[memMouseY][memMouseX][DATA] = data;
};
}
//memSetDataLeft(data): Store data in the left cell.
if (typeof mouse.memSetDataLeft !== 'function') {
mouse.memSetDataLeft = function(data) {
var off = left2Offset(memMouseDir);
memMaze[memMouseY+off.y][memMouseX+off.x][DATA] = data;
};
}
//memSetDataRight(data): Store data in the right cell.
if (typeof mouse.memSetDataRight !== 'function') {
mouse.memSetDataRight = function(data) {
var off = right2Offset(memMouseDir);
memMaze[memMouseY+off.y][memMouseX+off.x][DATA] = data;
};
}
//memSetDataFwd(data): Store data in the forward cell.
if (typeof mouse.memSetDataFwd !== 'function') {
mouse.memSetDataFwd = function(data) {
var off = fwd2Offset(memMouseDir);
memMaze[memMouseY+off.y][memMouseX+off.x][DATA] = data;
};
}
//memSetDataBack(data): Store data in the back cell.
if (typeof mouse.memSetDataBack !== 'function') {
mouse.memSetDataBack = function(data) {
var off = back2Offset(memMouseDir);
memMaze[memMouseY+off.y][memMouseX+off.x][DATA] = data;
};
}
//memGetData(): Returns the data stored in the current cell.
if (typeof mouse.memGetData !== 'function') {
mouse.memGetData = function() {
return memMaze[memMouseY][memMouseX][DATA];
};
}
//memGetDataLeft(): Returns the value for the left cell.
if (typeof mouse.memGetDataLeft !== 'function') {
mouse.memGetDataLeft = function() {
var off = left2Offset(memMouseDir);
return memMaze[memMouseY+off.y][memMouseX+off.x][DATA];
};
}
//memGetDataRight(): Returns the value for the right cell.
if (typeof mouse.memGetDataRight !== 'function') {
mouse.memGetDataRight = function() {
var off = right2Offset(memMouseDir);
return memMaze[memMouseY+off.y][memMouseX+off.x][DATA];
};
}
//memGetDataFwd(): Returns the value for the cell in front.
if (typeof mouse.memGetDataFwd !== 'function') {
mouse.memGetDataFwd = function() {
var off = fwd2Offset(memMouseDir);
return memMaze[memMouseY+off.y][memMouseX+off.x][DATA];
};
}
//memGetDataBack(): Returns the value for the cell in back.
if (typeof mouse.memGetDataBack !== 'function') {
mouse.memGetDataBack = function() {
var off = back2Offset(memMouseDir);
return memMaze[memMouseY+off.y][memMouseX+off.x][DATA];
};
}
//memGetVisited(): Returns true if the current memory has been visited.
if (typeof mouse.memGetVisited !== 'function') {
mouse.memGetVisited = function() {
return memMaze[memMouseY][memMouseX][VISIT];
};
}
//memGetVisitedLeft(): Returns true if the left cell has been visited.
if (typeof mouse.memGetVisitedLeft !== 'function') {
mouse.memGetVisitedLeft = function() {
var off = left2Offset(memMouseDir);
return memMaze[memMouseY+off.y][memMouseX+off.x][VISIT];
};
}
//memGetVisitedRight(): Returns true if the right cell has been visited.
if (typeof mouse.memGetVisitedRight !== 'function') {
mouse.memGetVisitedRight = function() {
var off = right2Offset(memMouseDir);
return memMaze[memMouseY+off.y][memMouseX+off.x][VISIT];
};
}
//memGetVisitedFwd(): Returns true if the forward cell has been visited.
if (typeof mouse.memGetVisitedFwd !== 'function') {
mouse.memGetVisitedFwd = function() {
var off = fwd2Offset(memMouseDir);
return memMaze[memMouseY+off.y][memMouseX+off.x][VISIT];
};
}
//memGetVisitedBack(): Returns true if the back cell has been visited.
if (typeof mouse.memGetVisitedBack !== 'function') {
mouse.memGetVisitedBack = function() {
var off = back2Offset(memMouseDir);
return memMaze[memMouseY+off.y][memMouseX+off.x][VISIT];
};
}
//memClear(): Clears everything from the mouse memory.
if (typeof mouse.memClear !== 'function') {
mouse.memClear = function() {
var x, y;
memMaze = [];
for (y=0;y<cHeight;y++) {
memMaze[y] = [];
for (x=0;x<cWidth;x++) {
// [NORTH,EAST,SOUTH,WEST,VISIT,DATA]
memMaze[y][x] = [];
if (y===0) {
memMaze[y][x][NORTH] = false; // wall north
} else {
memMaze[y][x][NORTH] = true;
}
if (y===cHeight-1) {
memMaze[y][x][SOUTH] = false; // wall south
} else {
memMaze[y][x][SOUTH] = true;
}
if (x===0) {
memMaze[y][x][WEST] = false; // wall west
} else {
memMaze[y][x][WEST] = true;
}
if (x===cWidth-1) {
memMaze[y][x][EAST] = false; // wall east
} else {
memMaze[y][x][EAST] = true;
}
memMaze[y][x][VISIT] = false;
memMaze[y][x][DATA] = 0;
}
}
// We should look at and record the cell we are in.
memUpdate();
};
}
//memPerfect(): Get a perfect memory of the maze.
if (typeof mouse.memPerfect !== 'function') {
mouse.memPerfect = function() {
var x, y;
memMaze = [];
for (y=0;y<cHeight;y++) {
memMaze[y] = [];
for (x=0;x<cWidth;x++) {
// [NORTH,EAST,SOUTH,WEST,VISIT,DATA]
memMaze[y][x] = [];
if (y===0) {
memMaze[y][x][NORTH] = false; // wall north
} else {
//memMaze[y][x][NORTH] = true;
memMaze[y][x][NORTH] = (maze[y][x].indexOf("N") !== -1);
}
if (y===cHeight-1) {
memMaze[y][x][SOUTH] = false; // wall south
} else {
//memMaze[y][x][SOUTH] = true;
memMaze[y][x][SOUTH] = (maze[y][x].indexOf("S") !== -1);
}
if (x===0) {
memMaze[y][x][WEST] = false; // wall west
} else {
//memMaze[y][x][WEST] = true;
memMaze[y][x][WEST] = (maze[y][x].indexOf("W") !== -1);
}
if (x===cWidth-1) {
memMaze[y][x][EAST] = false; // wall east
} else {
//memMaze[y][x][EAST] = true;
memMaze[y][x][EAST] = (maze[y][x].indexOf("E") !== -1);
}
memMaze[y][x][VISIT] = false;
memMaze[y][x][DATA] = 0;
}
}
};
}
//memSetPosAt(x,y,heading):
if (typeof mouse.memSetPosAt !== 'function') {
mouse.memSetPosAt = function(x,y,heading) {
memMouseX = x;
memMouseY = y;
memMouseDir = heading;
};
}
//memFlood(rev): Uses the walls in the mouse's memory to calculate
//how far each cell is from a destination square.
//The distances are stored in the cells value.
//when goGoal==true then goal squares are the destination
//when goGoal==false then home square is the destination
if (typeof mouse.memFlood !== 'function') {
mouse.memFlood = function(goGoalp) {
var goGoal = goGoalp;
var x, y;
var level = 0;
var currentLevel = [];
var nextLevel = [];
var cell;
var cellData;
var done = false;
var str="";
if (goGoalp === undefined) {
goGoal = true; // the default
}
// Set all data values to 255.
for (y=0;y<cHeight;y++) {
for (x=0;x<cWidth;x++) {
memMaze[y][x][DATA] = 255;
}
}
// put destination cells in currentLevel
if (goGoal) {
// goal squares
currentLevel.push(Cell(7,7),Cell(8,7),Cell(7,8),Cell(8,8));
} else {
// home square
currentLevel.push(Cell(0,15));
}
while(!done) {
while(currentLevel.length>0) {
cell = currentLevel.pop();
cellData = memMaze[cell.y][cell.x];
if (cellData[DATA] === 255) {
cellData[DATA] = level;
// add open neighbors to nextLevel
if (cellData[NORTH]) {
nextLevel.push(Cell(cell.x,cell.y-1));
}
if (cellData[EAST]) {
nextLevel.push(Cell(cell.x+1,cell.y));
}
if (cellData[SOUTH]) {
nextLevel.push(Cell(cell.x,cell.y+1));
}
if (cellData[WEST]) {
nextLevel.push(Cell(cell.x-1,cell.y));
}
}
}
if (nextLevel.length>0) {
level++;
currentLevel = nextLevel;
nextLevel = [];
} else {
done = true;
}
}
};
}
// print the content of the mouse data memory.
if (typeof mouse.memPrintData !== 'function') {
mouse.memPrintData = function() {
var x,y;
var str="";
var d;
// print debugging info
str = "Flood values:\n";
for (y=0;y<cHeight;y++) {
for (x=0;x<cWidth;x++) {
d = memMaze[y][x][DATA];
if (d < 10) {
str += " "+ memMaze[y][x][DATA] + " ";
} else if (d < 100) {
str += " "+ memMaze[y][x][DATA] + " ";
} else {
str += memMaze[y][x][DATA] + " ";
}
}
str = str + "\n";
}
console.log(str);
};
}
// print the maze in the mouse's memory.
if (typeof mouse.memPrintMaze !== 'function') {
mouse.memPrintMaze = function() {
var x,y;
var str="";
var path;
// print debugging info
str = "Flood values:\n";
for (y=0;y<cHeight;y++) {
// print north wall
for (x=0;x<cWidth;x++) {
path = memMaze[y][x][NORTH];
if (path) {