-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
613 lines (567 loc) · 30.6 KB
/
Copy pathindex.html
File metadata and controls
613 lines (567 loc) · 30.6 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
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
<!DOCTYPE html>
<!-- data-mode is "python" or "blocks"; maze.js switches it. -->
<html lang="en" data-mode="python">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#081f3d">
<meta name="description"
content="Write Python in the browser to guide a robot through a maze.">
<title>Python Maze Solver</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="challenge.css">
</head>
<body>
<header class="site-header">
<a class="brand" href="./" aria-label="Python Maze Solver home">
<img class="uob-logo" src="assets/uob-logo.png"
alt="University of Bradford">
<span class="brand-divider" aria-hidden="true"></span>
<strong>Python Maze Solver</strong>
</a>
<button id="tutorialsBtn" class="header-button" type="button">
Tutorials
</button>
</header>
<main id="app">
<div class="lab-grid">
<!--
The maze panel: the canvas is where the maze and the player are
drawn by JavaScript, with the run and maze controls beneath it.
-->
<section class="panel maze-panel" aria-labelledby="mazeHeading">
<div class="panel-heading">
<div>
<p class="step-label">The maze</p>
<h2 id="mazeHeading">Guide the triangle to the goal</h2>
</div>
</div>
<div class="maze-stage">
<canvas id="mazeCanvas" width="480" height="480"></canvas>
<!-- Explains a run that Python had to stop, e.g. an endless loop. -->
<div id="mazeNotice" class="maze-notice" role="status" hidden>
<strong id="mazeNoticeTitle"></strong>
<span id="mazeNoticeText"></span>
</div>
</div>
<div id="buttons">
<!--
The label starts as "Loading Python…" because Pyodide takes
a few seconds to arrive and the tutorial asks for a click
almost immediately. maze.js restores the real label when
the interpreter is ready.
-->
<button id="runBtn" class="button primary" disabled>
Loading Python…
</button>
<button id="stopBtn" class="button secondary" disabled>
Stop
</button>
<label id="speedLabel">
Speed
<input type="range" id="speed" min="1" max="200" value="100">
</label>
</div>
<div id="mazeControls">
<label for="mazeSelect">Maze:</label>
<select id="mazeSelect" disabled>
<optgroup label="Difficulty">
<option value="tutorial">Tutorial maze</option>
<option value="easy">Easy: one winding route</option>
<option value="medium">Medium: junctions and dead ends
</option>
<option value="hard">Hard: loops and wall islands</option>
<option value="expert">Expert: loops and open areas</option>
</optgroup>
<optgroup label="Extra challenges">
<option value="plaza">Plaza: pillars, no wall to follow
</option>
<option value="marathon">Marathon: a very large maze
</option>
</optgroup>
</select>
<button id="generateMazeBtn" class="button secondary" type="button"
disabled>
Generate new maze
</button>
</div>
<p id="mazeStatus" role="status" aria-live="polite">
Starting Python. The first load takes a few seconds.
</p>
</section>
<!--
The code panel: a text area where the user writes Python, and a
read-only text area showing printed output and errors.
-->
<section class="panel code-panel" aria-labelledby="codeHeading">
<div class="panel-heading">
<div>
<p class="step-label">Your program</p>
<h2 id="codeHeading">
<span class="for-python">Python code</span>
<span class="for-blocks">Code blocks</span>
</h2>
</div>
<div id="modeSwitch" class="mode-switch" role="group"
aria-label="Write the program with">
<button type="button" data-mode="blocks"
aria-pressed="false">Code blocks</button>
<button type="button" data-mode="python"
aria-pressed="true">Python</button>
</div>
</div>
<div class="code-editor for-python">
<pre id="lineNumbers" aria-hidden="true"></pre>
<textarea id="code" spellcheck="false" wrap="off"></textarea>
</div>
<!--
blocks.js replaces this message with the blocks workspace, or
with an explanation if Blockly could not be downloaded.
-->
<div id="blocksEditor" class="blocks-editor for-blocks">
<p class="blocks-unavailable">Loading code blocks…</p>
</div>
<div class="middle-actions">
<button id="sampleBtn" class="button secondary for-python"
type="button">
Load sample
</button>
<button id="convertBtn" class="button secondary for-blocks"
type="button">
Convert to Python
</button>
</div>
<p class="panel-title">Output</p>
<textarea id="output" readonly></textarea>
</section>
<!-- Instructions and reference material, all about the Python. -->
<section class="panel help-panel for-python">
<div class="tabs" id="help-tabs">
<div class="tabs-nav" id="referenceTabs">
<button type="button" data-tab="maze" class="active">Maze API
& Help
</button>
<button type="button" data-tab="guide">Harder mazes</button>
</div>
<div class="tabs-content" tabindex="0"
aria-label="Help and reference content">
<section class="tab-pane active" data-tab="maze">
<div class="tab-section">
<p><strong>Goal and controls</strong></p>
<p>
Write a Python program that guides the blue triangle from
its starting square to the green goal square without
walking into walls. The triangle starts facing right, and
every run begins again from the start.
</p>
<ul>
<li><strong>Run program</strong> runs the code in the Python
editor.
</li>
<li><strong>Stop</strong> halts the triangle where it is.
Every run starts again from the beginning.
</li>
<li>The <strong>speed</strong> slider changes animation
speed.</li>
<li><strong>Load sample</strong> loads a right-hand-wall
solver into the code box.
</li>
<li>The <strong>Maze</strong> menu changes difficulty, and
<strong>Generate new maze</strong> builds a fresh
layout of the one you are on, so a program that
only works on a single maze is easy to spot.
</li>
<li><strong>Challenge mode</strong> opens at the bottom of
the page after your first goal, and tests your program
on 150 new mazes.
</li>
</ul>
</div>
<div class="tab-section">
<p><strong>Action functions</strong></p>
<p>These functions change the triangle:</p>
<ul>
<li><code>move()</code>: move one square in the direction
the triangle is facing. It raises a
<code>RuntimeError</code> if a wall is ahead.
</li>
<li><code>turn_left()</code>: turn 90 degrees left without
moving.
</li>
<li><code>turn_right()</code>: turn 90 degrees right
without moving.
</li>
</ul>
<p><strong>Checking functions</strong></p>
<p>These look at the maze without moving the triangle, and
each one answers <code>True</code> or <code>False</code>.
Directions are relative to the way it is currently
facing.</p>
<ul>
<li><code>path_ahead()</code>: is the square directly
ahead open?
</li>
<li><code>path_left()</code>: is the square to the
triangle's left open?
</li>
<li><code>path_right()</code>: is the square to the
triangle's right open?
</li>
<li><code>path_behind()</code>: is the square directly
behind open?
</li>
<li><code>at_goal()</code>: is the triangle on the green
goal square?
</li>
</ul>
<img class="help-figure" src="assets/help-directions.svg"
alt="Facing right, ahead is the square to the right, left is above, right is below and behind is to the left. Facing down, ahead is below, left is to the right, right is to the left and behind is above.">
<p><strong>Where am I?</strong></p>
<p>One function reports something the triangle cannot see:</p>
<ul>
<li><code>position()</code>: the square the triangle is
on, as a <code>(row, column)</code> pair counting from
zero at the top left. Unlike the checks above it does
not answer <code>True</code> or <code>False</code>.
</li>
</ul>
<p>Storing every <code>position()</code> you have visited is
how a program can tell that it has been somewhere before,
which is exactly what wall following cannot do.</p>
</div>
<div class="tab-section">
<p><strong>Useful Python patterns</strong></p>
<p>Use <code>if</code> to choose one action. A colon starts each
block, and its contents are indented by four spaces:</p>
<pre><code>if path_ahead():
move()
elif path_right():
turn_right()
else:
turn_left()</code></pre>
<p>Use <code>while</code> to repeat choices until the goal is
reached. The <code>...</code> below is a placeholder, so
replace it with your own indented decision:</p>
<pre><code>while not at_goal():
# Check the paths and choose an action here
...</code></pre>
<p>You can also name a reusable sequence by defining your own
function:</p>
<pre><code>def go_forward_until_wall():
while path_ahead():
move()
go_forward_until_wall()</code></pre>
<p>For how each difficulty is solved, open the
<strong>Harder mazes</strong> tab.</p>
</div>
<div class="tab-section">
<p><strong>Output and common errors</strong></p>
<p><code>print()</code> messages and Python errors appear in
the Output panel. Read the final line first; it usually
identifies the problem.</p>
<ul>
<li><strong><code>SyntaxError</code>:</strong> check exact
spelling, matching parentheses and a colon after
<code>if</code>, <code>elif</code>, <code>else</code>,
<code>while</code> and <code>def</code> lines.
</li>
<li><strong><code>IndentationError</code>:</strong> use the
same indentation throughout a block, normally four
spaces. After a line ending in a colon, Enter indents
the next line for you. Tab indents every selected line
and Shift+Tab unindents them.
</li>
<li><strong><code>RuntimeError: Wall ahead</code>:</strong>
call a path question before <code>move()</code>.
</li>
<li><strong><code>StepLimitError</code>:</strong> a loop is
repeating without enough progress. Make sure a branch
moves or turns and that the loop can eventually stop. On
Hard and Expert mazes it can also mean a working
strategy is going round and round a loop in the maze.
</li>
</ul>
<p>When debugging, start with one or two commands, run them,
then add one decision at a time.</p>
</div>
</section>
<!--
What comes after the tutorials: why each difficulty needs a
better program. The pictures are drawn from the real mazes
and solvers by tools/make_help_images.py. Challenge mode
links to these sections by id.
-->
<section class="tab-pane" data-tab="guide">
<div class="tab-section">
<p><strong>Each difficulty adds one idea</strong></p>
<p>A program that solves one difficulty can fail the next,
because each adds something new to the maze. This tab
goes through them in order. After your first goal,
challenge mode tests a program on 25 mazes of each.</p>
</div>
<div class="tab-section has-image" id="guide-easy">
<div>
<p><strong>Easy: one winding corridor</strong></p>
<p>An Easy maze turns but never branches, so the program
only has to keep going forward and turn at corners:</p>
<pre><code>while not at_goal():
if path_ahead():
move()
elif path_right():
turn_right()
move()
else:
turn_left()</code></pre>
<p><code>elif</code> means “otherwise, if”. Python checks
the branches in order and runs only the first one that
fits.</p>
</div>
<div class="tab-section-aside">
<img src="assets/help-easy.svg"
alt="An Easy maze: one corridor from the start to the goal, with no branches.">
</div>
</div>
<div class="tab-section has-image" id="guide-medium">
<div>
<p><strong>Medium: junctions and dead ends</strong></p>
<p>At a junction, “keep going forward” guesses, and it can
walk into a dead end and stay there. Swap the first two
branches so the right path comes first:</p>
<pre><code>while not at_goal():
if path_right():
turn_right()
move()
elif path_ahead():
move()
else:
turn_left()</code></pre>
<p>The same three rules in a different order make a
different strategy. The triangle keeps its right hand on
the wall, tries every branch it meets and walks back out
of dead ends. On Easy and Medium mazes it always reaches
the goal, because there is exactly one route between any
two squares.</p>
</div>
<div class="tab-section-aside">
<img src="assets/help-medium.svg"
alt="A Medium maze: branching corridors and dead ends, but no loops.">
</div>
</div>
<div class="tab-section has-image" id="guide-loops">
<div>
<p><strong>Hard and Expert: loops</strong></p>
<p>Hard and Expert mazes contain loops, so some walls are
islands that never touch the goal. A wall follower that
starts beside one goes round it forever. Every lap looks
the same, so a rule that only checks the squares next to
the triangle cannot tell it has been there before.</p>
<p>The picture is Hard maze 15 from challenge mode. The
right-hand rule follows the red route, passes one wall
away from the goal, and arrives back where it
started.</p>
</div>
<div class="tab-section-aside">
<img src="assets/help-loop.svg"
alt="Hard maze 15 with the wall follower's route in red. The route loops back to the start and never reaches the goal in the top right corner.">
</div>
</div>
<div class="tab-section has-image" id="guide-plaza">
<div>
<p><strong>Plaza: no wall to follow</strong></p>
<p>A Plaza is an open hall of separate pillars, with the
start and goal away from the outside wall. Keeping a hand
on a pillar only leads round that pillar, so no wall
follower solves a single one.</p>
</div>
<div class="tab-section-aside">
<img src="assets/help-plaza.svg"
alt="A Plaza maze: open floor dotted with small separate pillars.">
</div>
</div>
<div class="tab-section has-image" id="guide-memory">
<div>
<p><strong>Give the triangle a memory</strong></p>
<p>What a wall follower lacks is memory.
<code>position()</code> gives the square the triangle is
on, so a program can keep every square it has visited in
a <code>set</code> and tell new ground from ground it has
covered:</p>
<pre><code>visited = set()
while not at_goal():
visited.add(position())
...</code></pre>
<p>Then prefer a direction whose square is not in
<code>visited</code>, and keep a list of the moves made so
they can be retraced out of a dead end.
<code>position()</code> does not say which way the
triangle faces, so keep that in a variable as well and
change it at every turn.</p>
<p>The picture shows a program like this on the same maze.
It explored the shaded squares, backed out of the dead
ends and found the goal. This approach solves every
difficulty, Plaza and Marathon included.</p>
</div>
<div class="tab-section-aside">
<img src="assets/help-memory.svg"
alt="Hard maze 15 again. The squares a memory solver visited are shaded, and its route in blue reaches the goal.">
</div>
</div>
<div class="tab-section">
<p><strong>Further challenges</strong></p>
<ul>
<li><strong>Prefer left:</strong> swap the
<code>path_right()</code> branch for a
<code>path_left()</code> one, and compare.
</li>
<li><strong>Walk less:</strong> challenge mode compares each
solved maze with its shortest route, shown as par.
<code>1.0×</code> par means no wasted moves.
</li>
<li><strong>Name repeated behaviour:</strong> define a
function such as <code>def turn_around():</code> and call
it where needed.
</li>
</ul>
</div>
</section>
</div>
</div>
</section>
</div>
<!-- Challenge mode builds its own contents into this mount point. -->
<div id="challenge-mode"></div>
<!-- Shown while a tutorial is minimised, to bring it back. -->
<button id="resumeTutorialBtn" type="button" hidden>Resume tutorial</button>
</main>
<!--
Each option shows what working that way looks like. The blocks picture and
the first code picture are the same program, so the choice reads as two
ways of writing one thing rather than an easy option and a hard one.
preview-blocks.svg was exported from the real blocks, so it matches them.
-->
<dialog id="tutorialSelector" class="tutorial-selector"
aria-label="Choose a tutorial">
<div class="tutorial-selector-panel">
<div class="tutorial-options">
<button type="button" class="tutorial-option"
data-tutorial-mode="blocks">
<span class="tutorial-option-preview" aria-hidden="true">
<img src="assets/preview-blocks.svg" alt="">
</span>
<span class="tutorial-option-title">Code blocks</span>
<span class="tutorial-option-description">
Snap blocks together. No typing and no experience needed.
</span>
</button>
<button type="button" class="tutorial-option"
data-tutorial-mode="programming">
<span class="tutorial-option-preview tutorial-option-code"
aria-hidden="true">
<span class="code-comment"># Turn right, then move twice</span>
<span>turn_right()</span>
<span>move()</span>
<span>move()</span>
</span>
<span class="tutorial-option-title">Python, step by step</span>
<span class="tutorial-option-description">
Type real Python code, with each idea explained as you go.
</span>
</button>
<button type="button" class="tutorial-option"
data-tutorial-mode="python">
<span class="tutorial-option-preview tutorial-option-code"
aria-hidden="true">
<span>while not at_goal():</span>
<span> if path_ahead():</span>
<span> move()</span>
<span> else:</span>
<span> turn_left()</span>
</span>
<span class="tutorial-option-title">Python for coders</span>
<span class="tutorial-option-description">
You have programmed before. A short tour of the commands and
how Python differs.
</span>
</button>
</div>
<!-- tutorial.js relabels this "Cancel" once a tutorial has been chosen. -->
<button id="closeTutorialSelector" class="tutorial-selector-close"
type="button">Skip the tutorial</button>
</div>
</dialog>
<!-- Offered once, after the first time the goal is reached. -->
<dialog id="goalDialog" class="goal-dialog" aria-labelledby="goalDialogTitle"
aria-describedby="goalDialogText">
<h2 id="goalDialogTitle"></h2>
<p id="goalDialogText"></p>
<div class="goal-dialog-actions">
<button id="goalDialogLater" class="button secondary" type="button">
Not now
</button>
<!-- A second suggestion, only offered in code blocks mode. -->
<button id="goalDialogExtra" class="button secondary" type="button"
hidden></button>
<button id="goalDialogAction" class="button primary" type="button">
</button>
</div>
</dialog>
<div id="tutorialBackdrop" class="tutorial-backdrop" hidden
aria-hidden="true"></div>
<section id="tutorialCoachmark" class="tutorial-coachmark" hidden
tabindex="-1" aria-labelledby="tutorialStepTitle"
aria-describedby="tutorialStepBody">
<!-- The top of the card is a handle for dragging it out of the way. -->
<div id="tutorialDragHandle" class="tutorial-card-header"
title="Drag to move this card">
<p id="tutorialStepCount" class="tutorial-step-count"></p>
<h2 id="tutorialStepTitle"></h2>
<span class="tutorial-drag-grip" aria-hidden="true"></span>
</div>
<div id="tutorialStepBody" class="tutorial-step-body"></div>
<div id="tutorialHints" class="tutorial-hints" aria-live="polite"></div>
<p id="tutorialFeedback" class="tutorial-feedback" aria-live="polite"
hidden></p>
<div class="tutorial-code-action">
<button id="tutorialHintBtn" type="button" hidden>Show a hint</button>
<button id="tutorialInsertCodeBtn" type="button" hidden>
Replace editor with example
</button>
</div>
<div class="tutorial-navigation">
<div class="tutorial-navigation-group">
<button id="tutorialCloseBtn" type="button">Close</button>
<button id="tutorialMinimizeBtn" type="button"
aria-label="Minimize tutorial">Minimize</button>
</div>
<div class="tutorial-navigation-group">
<button id="tutorialBackBtn" type="button">Back</button>
<button id="tutorialNextBtn" type="button">Continue</button>
</div>
</div>
</section>
<!--
Pyodide is a Python interpreter compiled to WebAssembly.
This script loads Pyodide and provides a global loadPyodide() function.
-->
<script src="https://cdn.jsdelivr.net/pyodide/v0.26.2/full/pyodide.js"></script>
<!--
JavaScript code for the maze app. Handles running the user's Python code
in Pyodide, updating the maze canvas, and swapping between instruction tabs.
-->
<script type="module" src="js/maze.js"></script>
<script src="js/tutorial.js"></script>
<!--
Challenge mode runs the editor's program against many generated mazes.
It builds its own markup and talks to the game through globalThis.mazeGame.
-->
<script defer src="js/challenge.js"></script>
<!--
Blocks mode. Blockly is Google's open-source blocks editor, the one Scratch's
editor grew from; blocks.js defines the maze's own blocks on top of it.
-->
<script defer src="https://cdn.jsdelivr.net/npm/blockly@13.3.0/blockly_compressed.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/blockly@13.3.0/msg/en.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/blockly@13.3.0/python_compressed.js"></script>
<script defer src="js/blocks.js"></script>
</body>
</html>