-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoriginal.html.txt
1147 lines (966 loc) · 49.2 KB
/
original.html.txt
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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Picobot</title>
<script src="app.js"></script>
<link rel="stylesheet" href="app.css" type="text/css" />
</head>
<body body="" bgcolor="#e1e1e1">
<script type="text/javascript" src="./picobotmaps.js"></script>
<!-- create the new drawing canvas -->
<table border="0" style="text-align:center;margin: 0 auto;">
<tbody>
<tr>
<td>
<!-- Here, we build the board! Using sexy javascript! -->
<script language="JavaScript" type="text/javascript">
/////////////////////////////////////////////////////////////////////////////////////////////
// Some horrible browsers do not support the javascript map function, so we add it back in!//
/////////////////////////////////////////////////////////////////////////////////////////////
if (!Array.prototype.map) {
Array.prototype.map = function (fun /*, thisp*/) {
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
var rows = 25
var cols = 25
var len = 20
document.write('<div id="myCanvas" style="position: relative; height: ' + rows * len + 'px; width: ' + cols * len + 'px; top: 0px; left: 0px;">');
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
document.write('<div id="r' + i + 'c' + j + '" style="position:absolute; top:' + len * i + 'px; left:' + len * j + 'px; width:' + len + 'px; height:' + len + 'px; background-color:white;" onmousedown="toggle_wall(' + i + ',' + j + ');"><\/div>');
}
}
document.write('<\/div>');
</script>
<!-- end of myCanvas div -->
<form name="Controls">
<table style="width: 100%; text-align: center;" border="0" cellpadding="2">
<tbody>
<tr>
<td><input name="Go" value="Go" onclick="go();" type="button"></td>
<td><input name="Stop" value="Stop" onclick="stop();" type="button"></td>
<td><input name="Step" value="Step" onclick="update();" type="button"></td>
<td><input name="Reset" value="Reset" onclick="stop();fullReset();" type="button"></td>
<td>
<input name="Map-" value="<--" onclick="stop();changeMap(-1);document.calc.mapi.value = getCMI();" type="button"> MAP
<input name="Map+" value="-->" onclick="stop();changeMap(1);document.calc.mapi.value = getCMI();" type="button">
</td>
</tr>
</tbody>
</table>
<table style="width: 100%; text-align: center; background-color: rgb(238, 238, 170);">
<tbody>
<tr>
<td><textarea readonly="readonly" name="state" id="state" rows="1" cols="3" size="10" style="overflow: hidden; text-align: center;"></textarea></td>
<td><textarea readonly="readonly" name="surroundings" id="surroundings" rows="1" cols="5" style="overflow: hidden; text-align: center;"></textarea></td>
<td><textarea readonly="readonly" name="progress" id="progress" rows="1" cols="10" style="overflow: hidden; text-align: center;"></textarea></td>
</tr>
<tr>
<td width="33%"> State </td>
<td width="33%"> Surroundings </td>
<td width="33%"> Cells to go </td>
</tr>
</tbody>
</table>
<table style="width: 100%; text-align: center; background-color: rgb(238, 238, 170);">
<tbody>
<tr>
<td><textarea name="currentrule" readonly="readonly" id="currentrule" rows="1" cols="30" style="overflow: hidden; text-align: center;"> </textarea></td>
<td><textarea name="nextrule" readonly="readonly" id="nextrule" rows="1" cols="30" style="overflow: hidden; text-align: center;"> </textarea></td>
</tr>
<tr>
<td> Previous Rule </td>
<td> Next Rule </td>
</tr>
</tbody>
</table>
<table style="width: 100%; text-align: center; background-color: rgb(238, 238, 170);">
<tbody>
<tr>
<td><input name="West" value="West" onclick="stop();teleportRobot('W');" type="button"></td>
<td><input name="East" value="East" onclick="stop();teleportRobot('E');" type="button"></td>
<td> - Teleport Robot - </td>
<td><input name="North" value="North" onclick="stop();teleportRobot('N');" type="button"></td>
<td><input name="South" value="South" onclick="stop();teleportRobot('S');" type="button"></td>
</tr>
</tbody>
</table>
</form>
</td>
<td>
<div id="picodiv"></div>
</td>
</tr>
<tr>
<td></td>
<td>
<div id="oldpicodiv" style="">
<!-- right-hand side -->
<form name="calc" method="post" action="./picouploader.py">
<table border="0">
<tbody>
<tr>
<td>
<center><h1>Picobot</h1></center>
</td>
</tr>
<tr>
<td>
<center><b>Rules</b></center>
<textarea name="rules" id="rules" rows="20" cols="60" style="font-family: courier;">
#
# Hashtag lines are optional comments
#
# state 0 with nothing N: go one step N
0 x*** -> N 0
# state 0 with something to the N: go W + into st 1
# ** This will crash if picobot has a wall to the W! **
0 N*** -> W 1
# state 1 with nothing to the S: go one step S
1 ***x -> S 1
# state 1 with something to the S: stay put + into state 0
1 ***S -> X 0
</textarea>
</td>
</tr>
<tr>
<td>
<center>
<input name="add" id="enterrulesbutton" value="Enter rules for Picobot" onclick="stop();document.calc.mesg.value = computeRules(document.calc.rules.value);" type="button">
<input name="mapi" value="0" type="hidden">
</center>
</td>
</tr>
<tr>
<td>
<center>
Be sure to hit "Enter rules" after making changes.
</center>
</td>
</tr>
<tr>
<td>
<br>
<center>
<b>Messages</b>
</center>
<textarea readonly="readonly" id="mesg" name="mesg" rows="10" cols="60" style="font-family: courier;"> </textarea>
</td>
</tr>
<tr></tr>
</tbody>
</table>
</form>
</div>
</td>
</tr>
</tbody>
<tfoot>
<tr><td colspan="2" style="text-align:center">This is a slightly adapted version of the <a href="http://www.cs.hmc.edu/picobot">original Picobot</a>, created by <a href="http://www.cs.hmc.edu/~dodds">Zach Dodds</a>.</td></tr>
</tfoot>
</table>
<!-- Here begins the picobot engine -->
<script language="javascript" type="text/javascript">
try {
function drawOneSquare(r, c, hexcolor) {
divName = 'r' + r + 'c' + c;
document.getElementById(divName).style.backgroundColor = hexcolor;
}
var DEBUG = 0; // set to 0 to turn off debugging
var TIMING_DELAY = 20;
var MAXROWS = 25;
var MAXCOLS = 25;
var MAXCOOKIES = 625
var cookies_remaining = MAXCOOKIES
var COLORS = ["white", "blue", "lime", "red", "#808080", "aaaaff", "ff11dd"]
var COOKIE_COLOR = 6
var DONE_COLOR = 5
var VISITED_COLOR = 4
var ERROR_COLOR = 3
var ROBOT_COLOR = 2
var WALL_COLOR = 1
var CLEAR_COLOR = 0
function updateCellColor(r, c, colorIndex) {
currentCells[r][c] = colorIndex;
drawOneSquare(r, c, COLORS[colorIndex]);
}
// global variable for current cells
var currentCells = Array(MAXROWS);
// draw the map above...
function drawMap(map) {
currentCells = Array(MAXROWS);
// map will be a 25x25 2d array
for (var r = 0; r < MAXROWS; ++r) {
currentCells[r] = Array(MAXCOLS); // make the row
for (var c = 0; c < MAXCOLS; ++c) {
if (r < map.length && c < map[0].length) {
currentCells[r][c] = map[r][c];
drawOneSquare(r, c, COLORS[map[r][c]]);
}
else {
currentCells[r][c] = CLEAR_COLOR;
drawOneSquare(r, c, COLORS[CLEAR_COLOR])
}
}
}
}
// Toggles the existance of a wall in a particular location
function toggle_wall(row, col) {
if (currentCells[row][col] != ROBOT_COLOR);
{
if (currentCells[row][col] != WALL_COLOR)
updateCellColor(row, col, WALL_COLOR);
else if (currentCells[row][col] == WALL_COLOR)
updateCellColor(row, col, CLEAR_COLOR);
}
clearVisited();
}
// counts any particular type of square in a map
function countSquares(m, colorIndex) {
var total = 0;
for (var r = 0; r < MAXROWS; ++r) {
for (var c = 0; c < MAXCOLS; ++c) {
if (m[r][c] == colorIndex) {
++total;
}
}
}
return total;
}
// clears the map of all visitted cells
function clearVisited() {
for (var r = 0; r < MAXROWS; ++r) {
for (var c = 0; c < MAXCOLS; ++c) {
if (currentCells[r][c] == VISITED_COLOR) {
currentCells[r][c] = CLEAR_COLOR;
drawOneSquare(r, c, COLORS[CLEAR_COLOR]);
}
}
}
cookies = [];
cookies_remaining = MAXCOOKIES;
updateProgress;
}
// blanks the map and resets the robot
function fullReset() {
for (var r = 0; r < MAXROWS; ++r) {
for (var c = 0; c < MAXCOLS; ++c) {
if (currentCells[r][c] == VISITED_COLOR || currentCells[r][c] == ROBOT_COLOR
|| currentCells[r][c] == ERROR_COLOR || currentCells[r][c] == COOKIE_COLOR) {
currentCells[r][c] = CLEAR_COLOR;
drawOneSquare(r, c, COLORS[CLEAR_COLOR]);
}
}
}
resetBot();
}
// global variable for current rules
// RULES[state] is a mapping from one of the 16 'NEWS'-'xxxx' strings
// to a direction to move and a new state to enter (or other activity)
var RULES = Array(); // supposedly will expand to fit!
var MAXRULES = 100;
// initialize the rules to a good size
for (rulenum = 0; rulenum < MAXRULES; ++rulenum) {
RULES[rulenum] = Array();
}
// global robot
var robot = { row: 0, col: 0, state: 0 };
// global cookies
var cookies = []
XXXX = 0
NXXX = 1
XEXX = 2
NEXX = 3
XXXS = 4
NXXS = 5
XEXS = 6
NEXS = 7
XXWX = 8
NXWX = 9
XEWX = 10
NEWX = 11
XXWS = 12
NXWS = 13
XEWS = 14
NEWS = 15
COOKIE = 16
var SURR_TEXT = ["xxxx", "Nxxx", "xExx", "NExx", "xxxS", "NxxS",
"xExS", "NExS", "xxWx", "NxWx", "xEWx", "NEWx",
"xxWS", "NxWS", "xEWS", "NEWS",
"xxxx", "Nxxx", "xExx", "NExx", "xxxS", "NxxS",
"xExS", "NExS", "xxWx", "NxWx", "xEWx", "NEWx",
"xxWS", "NxWS", "xEWS", "NEWS"]
MOVE_X = 0 // don't move at all - better change state!
MOVE_N = 1
MOVE_E = 2
MOVE_W = 3
MOVE_S = 4
OTHER = 0 // other actions
// we'll make our own rules for now
RULE_DIR = 0;
RULE_NEWSTATE = 1;
RULE_OTHERACTION = 2;
RULE_LINENUM = 3;
RULE_TEXT = 4;
RULE_COOKIE = 5; // index of the string the user typed
function cookieLocation(row, col) {
return col + MAXCOLS * row;
}
function drawCookies() {
var the_cookie = 0
var r = 0
var c = 0
for (var i = 0; i < cookies.length; i++) {
the_cookie = cookies[i]
c = the_cookie % MAXCOLS;
r = (the_cookie - c) / MAXCOLS;
updateCellColor(r, c, COOKIE_COLOR);
}
}
function randomInRange(N) {
return Math.floor(N * Math.random())
}
function resetBot() {
var nrow;
var ncol;
found = false
while (!found) {
nrow = randomInRange(MAXROWS)
ncol = randomInRange(MAXCOLS)
if (currentCells[nrow][ncol] != WALL_COLOR)
found = true
}
robot = { row: nrow, col: ncol, state: 0 };
drawOneSquare(robot.row, robot.col, COLORS[ROBOT_COLOR]);
currentCells[robot.row][robot.col] = ROBOT_COLOR;
// re-update the squares to go...
document.getElementById('state').value = "0";
document.getElementById('surroundings').value = SURR_TEXT[getSurroundings()];
cookies = [];
cookies_remaining = MAXCOOKIES;
updateProgress();
}
function teleportRobot(direction) {
// will move the robot if it's legal to...
// will not mark the space moved from as grey, but clear
//
// direction must be 'W', 'E', 'N', or 'S' to move
// get robot's current position
var r = robot.row;
var c = robot.col;
var msgText = "";
if (direction == 'W') {
// check if it's safe to move...
if (isWallToW(r, c)) {
msgText = "There is a wall to the west.\nNot moving the robot.";
}
else {
// clear previous location
updateCellColor(robot.row, robot.col, CLEAR_COLOR);
// move it west
robot.col = robot.col - 1;
}
}
else if (direction == 'E') {
// check if it's safe to move...
if (isWallToE(r, c)) {
msgText = "There is a wall to the east.\nNot moving the robot.";
}
else {
// clear previous location
updateCellColor(robot.row, robot.col, CLEAR_COLOR)
// move it west
robot.col = robot.col + 1;
}
}
else if (direction == 'N') {
// check if it's safe to move...
if (isWallToN(r, c)) {
msgText = "There is a wall to the north.\nNot moving the robot.";
}
else {
// clear previous location
updateCellColor(robot.row, robot.col, CLEAR_COLOR);
// move it west
robot.row = robot.row - 1;
}
}
else if (direction == 'S') {
// check if it's safe to move...
if (isWallToS(r, c)) {
msgText = "There is a wall to the south.\nNot moving the robot.";
}
else {
// clear previous location
updateCellColor(robot.row, robot.col, CLEAR_COLOR);
// move it west
robot.row = robot.row + 1;
}
}
// clear the cheated map
clearVisited();
// and draw cookies!
drawCookies();
// don't forget to draw the robot on top...
updateCellColor(robot.row, robot.col, ROBOT_COLOR);
// change the status tag
document.getElementById('surroundings').value = SURR_TEXT[getSurroundings()];
// print any messages
document.getElementById('mesg').value = msgText;
}
function moveRobot(rule) {
var dir = rule[RULE_DIR];
//alert('dir is' + dir)
var newstate = rule[RULE_NEWSTATE];
var newrow = robot.row;
var newcol = robot.col;
var r = newrow;
var c = newcol;
// Deal with cookies before we move!
var cookieAction = rule[RULE_COOKIE];
if (cookieAction == 1) {
if (cookies_remaining > 0) {
cookies_remaining += -1;
cookies.push(cookieLocation(robot.row, robot.col));
}
else
return "No more cookies. Stopping."
}
if (cookieAction == 2) {
cookieIndexInList = cookies.indexOf(cookieLocation(r, c))
if (cookieIndexInList == -1)
return "There is no cookie here! You cannot pull cookies out of thin air!"
else {
cookies_remaining += 1;
cookies.splice(cookieIndexInList, 1)
}
}
if (dir == MOVE_X); // do not move at all
else if (dir == MOVE_N) {
if (!isWallToN(r, c)) newrow -= 1;
else return "Can not move to the north!";
}
else if (dir == MOVE_E) {
if (!isWallToE(r, c)) newcol += 1;
else return "Can not move to the east!";
}
else if (dir == MOVE_W) {
if (!isWallToW(r, c)) newcol -= 1;
else return "Can not move to the west!";
}
else if (dir == MOVE_S) {
if (!isWallToS(r, c)) newrow += 1;
else return "Can not move to the south!";
}
else return 'Direction of ' + dir + ' not recognized.';
// check that it's not a wall or out of bounds...
// we return a nonempty message if we are and
// an empty string if everything is OK
if (newcol < 0) return 'Trying to move to col -1! Stopping';
if (newcol >= MAXCOLS) return 'Trying to move to col " + MAXCOLS + "! Stopping';
if (newrow < 0) return 'Trying to move to row -1! Stopping';
if (newrow >= MAXROWS) return 'Trying to move to row " + MAXROWS + "! Stopping';
// now update the data structure and the display
updateCellColor(robot.row, robot.col, VISITED_COLOR);
robot.row = newrow;
robot.col = newcol;
drawCookies();
updateCellColor(robot.row, robot.col, ROBOT_COLOR);
// now update the state of the robot
robot.state = newstate;
if (DEBUG) return 'Robot is ' + robot;
else return ''; // everything is OK
}
function isWallToN(r, c) {
var CC = currentCells;
//alert('r and c are ' + r + ' ' + c )
if (r == 0 || CC[r - 1][c] == WALL_COLOR) return 1;
return 0
}
function isWallToE(r, c) {
var CC = currentCells;
if (c == MAXCOLS - 1 || CC[r][c + 1] == WALL_COLOR) return 1;
return 0
}
function isWallToW(r, c) {
var CC = currentCells;
if (c == 0 || CC[r][c - 1] == WALL_COLOR) return 1;
return 0
}
function isWallToS(r, c) {
var CC = currentCells;
if (r == MAXROWS - 1 || CC[r + 1][c] == WALL_COLOR) return 1;
return 0
}
function getSurroundings() {
// look at the currentCells array and robot to
// see what's around us!
// should make sure that we're in bounds!
// check to the N
var r = robot.row;
var c = robot.col;
if (cookies.indexOf(cookieLocation(r, c)) != -1)
if (isWallToN(r, c)) // YES N
if (isWallToE(r, c)) // YES E
if (isWallToW(r, c)) // YES W
if (isWallToS(r, c)) // YES S
return NEWS + COOKIE;
else // NO S
return NEWX + COOKIE;
else // NO W
if (isWallToS(r, c)) // YES S
return NEXS + COOKIE;
else // NO S
return NEXX + COOKIE;
else // NO E
if (isWallToW(r, c)) // YES W
if (isWallToS(r, c)) // YES S
return NXWS + COOKIE;
else // NO S
return NXWX + COOKIE;
else // NO W
if (isWallToS(r, c)) // YES S
return NXXS + COOKIE;
else // NO S
return NXXX + COOKIE;
else // NO N
if (isWallToE(r, c)) // YES E
if (isWallToW(r, c)) // YES W
if (isWallToS(r, c)) // YES S
return XEWS + COOKIE;
else // NO S
return XEWX + COOKIE;
else // NO W
if (isWallToS(r, c)) // YES S
return XEXS + COOKIE;
else // NO S
return XEXX + COOKIE;
else // NO E
if (isWallToW(r, c)) // YES W
if (isWallToS(r, c)) // YES S
return XXWS + COOKIE;
else // NO S
return XXWX + COOKIE;
else // NO W
if (isWallToS(r, c)) // YES S
return XXXS + COOKIE;
else // NO S
return XXXX + COOKIE;
else
if (isWallToN(r, c)) // YES N
if (isWallToE(r, c)) // YES E
if (isWallToW(r, c)) // YES W
if (isWallToS(r, c)) // YES S
return NEWS;
else // NO S
return NEWX;
else // NO W
if (isWallToS(r, c)) // YES S
return NEXS;
else // NO S
return NEXX;
else // NO E
if (isWallToW(r, c)) // YES W
if (isWallToS(r, c)) // YES S
return NXWS;
else // NO S
return NXWX;
else // NO W
if (isWallToS(r, c)) // YES S
return NXXS;
else // NO S
return NXXX;
else // NO N
if (isWallToE(r, c)) // YES E
if (isWallToW(r, c)) // YES W
if (isWallToS(r, c)) // YES S
return XEWS;
else // NO S
return XEWX;
else // NO W
if (isWallToS(r, c)) // YES S
return XEXS;
else // NO S
return XEXX;
else // NO E
if (isWallToW(r, c)) // YES W
if (isWallToS(r, c)) // YES S
return XXWS;
else // NO S
return XXWX;
else // NO W
if (isWallToS(r, c)) // YES S
return XXXS;
else // NO S
return XXXX;
}
function update() {
// what is the rule
// it's the current robot's state and surr
var currentstate = robot.state;
var surr = getSurroundings();
rule = RULES[currentstate][surr]
var ruleText = "";
var mesgText = "";
if (!(rule)) {
ruleText = "none";
mesgText = mesgText + "\n" + "No rule for state " +
currentstate + " and surr " + SURR_TEXT[surr] +
"\nStopping."
}
else {
// get the parts of the rule needed to move the robot
mesgText = mesgText + moveRobot(rule) + "\n";
ruleText = rule[RULE_TEXT];
}
// set the current (== previous) rule
document.getElementById('currentrule').value = ruleText;
// do it again to get the _next_ rule
currentstate = robot.state;
surr = getSurroundings();
var newrule = RULES[currentstate][surr];
var newruletext = "";
if (!(newrule)) {
newruleText = "none";
}
else {
// get the parts of the rule needed to move the robot
//reply = moveRobot(newrule);
newruleText = '' + newrule[RULE_TEXT];
}
// set the next rule
document.getElementById('mesg').value = mesgText;
document.getElementById('nextrule').value = newruleText;
document.getElementById('state').value = currentstate;
document.getElementById('surroundings').value = SURR_TEXT[surr];
updateProgress();
}
var timerID = null
function go() {
clearInterval(timerID);
timerID = setInterval("update()", TIMING_DELAY);
}
function stop() {
clearInterval(timerID);
}
function removeEmptyStrings(arr) {
// returns an array without empty strings
var newarr = Array()
var n = 0 // location for next element of newarr
for (var i = 0; i < arr.length; ++i) {
if (arr[i] != '') newarr[n++] = arr[i]
}
return newarr
}
function addToList(sL, n) {
// append all of the ints in sL (with n added) to sL
var L = sL.length;
for (var i = 0; i < L; ++i) {
sL[L + i] = sL[i] + n;
}
return sL
}
function computeSurrFromString(surrStr) {
// must be length 4
if (surrStr.length != 4) return -1; // ERROR
// check first char: must be N, x, X, or *
var nc = surrStr.charAt(0);
if (nc != 'N' && nc != 'X' && nc != '*') return -1; //ERROR
var ec = surrStr.charAt(1);
if (ec != 'E' && ec != 'X' && ec != '*') return -1; //ERROR
var wc = surrStr.charAt(2);
if (wc != 'W' && wc != 'X' && wc != '*') return -1; //ERROR
var sc = surrStr.charAt(3);
if (sc != 'S' && sc != 'X' && sc != '*') return -1; //ERROR
// everything is OK, we prepare the list of integers
// to return - this is the list of surroundings matched
// by the input surrStr
surroundingsList = Array();
var i = 0; // next element in surroundingsList
// base value
var base = (nc == 'N') + 2 * (ec == 'E') + 4 * (sc == 'S') + 8 * (wc == 'W');
// add them into the list
surroundingsList[i++] = base;
if (nc == '*') surroundingsList = addToList(surroundingsList, 1)
if (ec == '*') surroundingsList = addToList(surroundingsList, 2)
if (wc == '*') surroundingsList = addToList(surroundingsList, 8)
if (sc == '*') surroundingsList = addToList(surroundingsList, 4)
return surroundingsList
}
function computeCookieSeeFromString(cookieSeeStr) {
if (cookieSeeStr == '*' || cookieSeeStr == '*M') return 0;
if (cookieSeeStr == 'XM') return 1;
if (cookieSeeStr == 'M' || cookieSeeStr == 'WM') return 2;
else return -1;
}
function computeCookieDoFromString(cookieSeeStr) {
if (cookieSeeStr == '*') return 0;
if (cookieSeeStr == 'DM') return 1;
if (cookieSeeStr == 'PM') return 2;
else return -1;
}
function clearRules() {
var maxstates = RULES.length;
for (var statenum = 0; statenum < maxstates; ++statenum) {
RULES[statenum] = Array(); // gone!
// does java script have garbage collection? I hope so!
// Yes, it does!
}
}
function checkAndSetNewRule(state, surrList, move, cookieAction, newstate, linenum, ruleStr) {
var reply = '';
var message = '';
// for each element of surr
var L = surrList.length;
for (var i = 0; i < L; ++i) {
var surr = surrList[i];
if (!(RULES[state][surr])) {
if (DEBUG) reply += "No repeat rule. state:" + state + " surr:" + surr + "\n";
// it's OK, so set the rule (no action yet)
RULES[state][surr] = [move, newstate, OTHER, linenum, ruleStr, cookieAction];
}
else {
reply += "Repeat Rule! The state: " + state + " surr: " + SURR_TEXT[surr] +
"\n was already handled on line #" + RULES[state][surr][3] +
"\n which reads as follows:" +
"\n " + RULES[state][surr][4] + "\n";
return reply;
}
}
return reply;
}
function computeRules(s) {
// convert tabs to spaces
// given that this is a browser, it's not clear
// how you would even enter a tab, but anyway...
s = s.replace(' ', ' ');
// we should clear out the rules here...
clearRules();
// here are all of the typed rules, line-by-line
var allRules = s.split('\n');
var numberOfLines = allRules.length;
var returnmessage = '';
// loop through them all...
for (var linenum = 0; linenum < numberOfLines; ++linenum) {
// the current rule == the current line's string
var ruleStr = String(allRules[linenum]);
returnmessage = "Oops...\n\nOn line number " + linenum + ", which is this:\n " + ruleStr + "\n\n";
// on IE we need to remove the '\r' at the end - this is weird!
var L = ruleStr.length;
if (ruleStr.indexOf('\r') != -1) {
ruleStr = ruleStr.substring(0, ruleStr.indexOf('\r')); // get rid of the \r
}
// throw out anything at or after the first # (commenting...)
var locationOfComment = ruleStr.indexOf('#');
if (locationOfComment != -1) // there _was_ a comment
{
ruleStr = ruleStr.substring(0, locationOfComment);
}
// make the whole thing uppercase
ruleStr = ruleStr.toUpperCase();
// tokenize on spaces into the list s
var s = ruleStr.split(' ');
s = removeEmptyStrings(s);
// blank lines are OK, but partial lines are not
if (s.length == 0) continue; // ignore lines with no tokens
if (s.length < 5) {
returnmessage += "There are too few tokens with only " + s.length +
" seen.\n\n 5 are needed, in the form\n " +
" State Surr -> Move NewState";
return returnmessage;
}
else if (s.length > 5) {
returnmessage += "There are too many tokens with " + s.length +
" seen.\n\n 5 are needed, in the form\n " +
" State Surr -> Move NewState";
return returnmessage;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// check stateStr
var stateStr = s[0]
var state = parseInt(stateStr)
if (!(state >= 0 && state < MAXRULES)) state = -1;
if (state == -1) {
returnmessage += "The first token, representing the input state, was not an integer" +
" between 0 and " + (MAXRULES - 1) + " (inclusive).";
return returnmessage;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// check surrStr
var surrStr = s[1]
var surr = computeSurrFromString(surrStr)
if (surr == -1) {
returnmessage += "The second token, representing the \"NEWS\" surroundings,\n was not in" +
" a recognized format. It should be\n from xxxx to NEWS, with *s allowed.";
return returnmessage;
}
// now surr is a list of the appropriate surroundings...
// we should check against our rule base to see if s/t is
// defined twice...
// but we wait to do this until everything has parsed all right...
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// Check whether to do anything if we are on a cookie
// If we were on an arrow, then we don't care about cookies so insert a *
var cookieSeeStr = s[2];
if (cookieSeeStr == "->") {
s.splice(2, 0, '*');
cookieSeeStr = s[2];
}
if (cookieSeeStr == "-") {
returnmessage += "It seems you have a dash - instead of an arrow ->\n" +
" Perhaps there's a space between the - and the >\n" +
" Be sure your rule is in the form\n" +
" State Surr -> Move NewState";
return returnmessage;
}
var cookieSee = computeCookieSeeFromString(cookieSeeStr);
if (cookieSee == -1) {
returnmessage += "This middle token should be an arrow: ->\n\nInstead, it was " + s[2] +
"\n\n" + " Be sure your rule is in the form\n" +
" State Surr -> Move NewState";
return returnmessage;
}
else if (cookieSee == 0) {
surr = addToList(surr, 16)
}
else if (cookieSee == 2) {
surr = surr.map(function (item) { return item + 16; });
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// Not all that important...
var arrowStr = s[3];
if (arrowStr != "->") {