-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
638 lines (587 loc) · 31.5 KB
/
Copy pathindex.html
File metadata and controls
638 lines (587 loc) · 31.5 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
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
<!DOCTYPE html>
<html lang="en">
<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>
<span class="mode-pill">Open day activity</span>
</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>
</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="resetBtn" class="button secondary" disabled>
Reset position
</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">Python code</h2>
</div>
</div>
<div class="code-editor">
<pre id="lineNumbers" aria-hidden="true"></pre>
<textarea id="code" spellcheck="false" wrap="off"></textarea>
</div>
<div class="middle-actions">
<button id="sampleBtn" class="button secondary" type="button">
Load sample
</button>
</div>
<p class="panel-title">Output</p>
<textarea id="output" readonly></textarea>
</section>
<!-- Instructions and reference material. -->
<section class="panel help-panel">
<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">Beginner Guide</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>Reset position</strong> stops the current
animation and returns the triangle to the start.
</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>
</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>
<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 a complete worked progression, open the
<strong>Beginner Guide</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. Pressing Tab in the editor inserts four spaces.
</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>
<section class="tab-pane" data-tab="guide">
<div class="tab-section">
<p><strong>Start here: Python in one minute</strong></p>
<p>
A program is a list of precise instructions. Python reads
the list from top to bottom. In this activity, a named
instruction such as <code>move()</code> is a
<strong>function call</strong>; the parentheses tell Python
to run it.
</p>
<p>
Python is case-sensitive, so <code>move()</code> and
<code>Move()</code> are different. Lines beginning with
<code>#</code> are comments for people. Use
<code>print("message")</code> to show text in Output.
</p>
<pre><code># This is a comment
move()
print("My program finished")</code></pre>
<p>Select <strong>Run program</strong> to try your code. Every
run resets the triangle first. Errors are useful feedback:
read the final line in Output, correct the code, and run it
again.</p>
</div>
<div class="tab-section">
<p><strong>Step 1: Your first commands</strong></p>
<p>Start with one command and press <strong>Run
program</strong>:</p>
<pre><code>move()</code></pre>
<p>Next, replace the editor contents with a sequence. Each
command runs once, in order:</p>
<pre><code>turn_right()
move()
move()</code></pre>
<p>
Predict the result before running it. Then swap
<code>turn_right()</code> for <code>turn_left()</code> and
compare what happens.
</p>
</div>
<div class="tab-section">
<p><strong>Step 2: Ask questions</strong></p>
<p>
A path function asks about the maze without moving the
triangle. Its answer is either <code>True</code> or
<code>False</code>, called a Boolean value. Directions are
relative to where the triangle is facing.
</p>
<pre><code>print(path_ahead())
print(path_left())
print(path_right())</code></pre>
<p>Run this code, then add a turn before the questions and
compare the answers.</p>
</div>
<div class="tab-section">
<p><strong>Step 3: Make a decision with if</strong></p>
<p>An <code>if</code> statement runs a block only when its
question is <code>True</code>. <code>else</code> supplies a
fallback:</p>
<pre><code>if path_ahead():
move()
else:
turn_right()</code></pre>
<p>
The colon starts a block. The four spaces before each
action are indentation; they show which action belongs to
which choice. Python treats indentation as part of the
program, so keep it consistent.
</p>
</div>
<div class="tab-section">
<p><strong>Step 4: Repeat actions with while</strong></p>
<p>
A <code>while</code> loop repeats its indented block while
its question remains <code>True</code>. This loop moves
safely until a wall is ahead:
</p>
<pre><code>while path_ahead():
move()</code></pre>
<p>
The path changes after every move, so Python checks the
question again. If a loop never changes anything relevant
to its question, it can run forever; this activity stops it
and reports a <code>StepLimitError</code>.
</p>
</div>
<div class="tab-section">
<p><strong>Step 5: Choose one maze action</strong></p>
<p>
The Easy maze is one winding route: it turns, but it never
branches and has no dead ends. So a program only has to keep
following the corridor. For one round, the rules are:
</p>
<ol>
<li>If the path ahead is open, move.</li>
<li>Otherwise, if the right path is open, turn right and
move.
</li>
<li>Otherwise, turn left and check again next round.</li>
</ol>
<p>In short: keep going forward, and only turn at a corner.
<code>elif</code> means “otherwise, if”. Python checks the
branches in order and runs only the first matching one.</p>
</div>
<div class="tab-section">
<p><strong>Step 5a: Test one round</strong></p>
<p>Run these rules once and observe which branch Python
chooses:</p>
<pre><code>if path_ahead():
move()
elif path_right():
turn_right()
move()
else:
turn_left()</code></pre>
<p>Because each run starts in the same place, you can change
the order of the questions and compare the result.</p>
</div>
<div class="tab-section">
<p><strong>Step 6: Repeat until the goal</strong></p>
<p>
<code>at_goal()</code> becomes <code>True</code> on the green
square. <code>not</code> reverses a Boolean value, so
<code>while not at_goal():</code> means “repeat while we have
not reached the goal”. Put the one-round decision inside it:
</p>
<pre><code>while not at_goal():
if path_ahead():
move()
elif path_right():
turn_right()
move()
else:
turn_left()
print("Reached the goal!")</code></pre>
<p>This solves any Easy maze, however winding, because there is
only ever one way to continue. Select <strong>Generate new
maze</strong> and run the same program to check that.</p>
</div>
<div class="tab-section">
<p><strong>Step 7: Medium mazes and one small change</strong></p>
<p>
Medium mazes add junctions and dead ends, so “keep going
forward” now guesses wrongly at a junction and gets stuck in
dead ends. Swap the first two branches so the right path is
preferred:
</p>
<pre><code>while not at_goal():
if path_right():
turn_right()
move()
elif path_ahead():
move()
else:
turn_left()</code></pre>
<p>
That is the same three rules in a different order, and it is
now a completely different strategy: the triangle keeps its
right hand on the wall, follows every branch it meets and
walks back out of dead ends by itself. The order of the
branches, not the branches themselves, decides the behaviour.
</p>
<p>
This is guaranteed to reach the goal on Easy and Medium
mazes, because they contain no loops: there is exactly one
route between any two squares, so following one wall
eventually visits the goal.
</p>
</div>
<div class="tab-section">
<p><strong>Step 8: Where wall following breaks</strong></p>
<p>
Hard and Expert mazes contain loops, and their walls are no
longer one connected shape. If the triangle starts following
a wall island that does not touch the goal, it can circle it
forever: the squares it sees look identical on each lap, so
no rule that only asks about neighbouring squares can tell
that it has been there before.
</p>
<p>
The activity stops such a program with a
<code>StepLimitError</code>, but that is only a safety
net, not how a good solver knows it is lost. To get past
this you need <em>memory</em>: some record of where the
triangle has already been, so it can rule those routes out
and back up to the last untried junction. Try the wall
follower on Hard first; it often still works, which is
exactly why “it worked once” is not the same as “it is
correct”.
</p>
</div>
<div class="tab-section">
<p><strong>Further challenges</strong></p>
<p>Change one thing at a time, predict the result, then test it:</p>
<ul>
<li><strong>Prefer left instead of right:</strong> swap the
<code>path_left()</code> and <code>path_right()</code>
blocks.
</li>
<li><strong>Measure your solver:</strong> create a variable
such as <code>moves = 0</code>, increase it after every
<code>move()</code>, and print the final value. Compare
the count on Easy against Hard.
</li>
<li><strong>Name repeated behaviour:</strong> create a
function such as <code>def turn_around():</code> and
call it where needed.</li>
<li><strong>Test assumptions:</strong> generate several Hard
and Expert mazes and run the same solver on each. When
it fails, use <code>print()</code> to work out what it
keeps repeating.</li>
</ul>
</div>
<div class="tab-section">
<p><strong>Going further: give the triangle a memory</strong></p>
<p>
<code>position()</code> tells you which square you are on.
Keep every square you have visited in a
<code>set</code>, and your program can tell the difference
between new ground and a route it has already walked.
</p>
<pre><code>visited = set()
while not at_goal():
visited.add(position())
print(len(visited), "squares seen")
...</code></pre>
<p>
That is the missing ingredient. Prefer a direction whose
square is not in <code>visited</code>, remember the way
you came so you can back out of a dead end, and you have a
solver that copes with loops, pillars and open ground.
It is more work than wall following, and it is the only
approach here that survives every difficulty.
</p>
</div>
</section>
</div>
</div>
</section>
</div>
<!-- Challenge mode builds its own contents into this mount point. -->
<div id="challenge-mode"></div>
<div class="tutorial-toolbar" id="tutorialToolbar" hidden>
<p id="tutorialStatus" aria-live="polite"></p>
<div class="tutorial-toolbar-actions">
<button id="resumeTutorialBtn" type="button" hidden>
Resume tutorial
</button>
<button id="restartTutorialBtn" type="button">Restart tutorial</button>
<button id="changeTutorialBtn" type="button">Change tutorial</button>
</div>
</div>
</main>
<dialog id="tutorialSelector" class="tutorial-selector"
aria-labelledby="tutorialSelectorTitle"
aria-describedby="tutorialSelectorDescription">
<div class="tutorial-selector-panel">
<h2 id="tutorialSelectorTitle">Select a tutorial</h2>
<p id="tutorialSelectorDescription">
Choose the option that best matches your current experience. All
options use the same Python editor and maze. You can change the
tutorial later.
</p>
<div class="tutorial-options">
<button type="button" class="tutorial-option"
data-tutorial-mode="programming">
<span class="tutorial-option-title">New to coding</span>
<span class="tutorial-option-description">
Start here with little or no computing experience. Learn what a
program is, then practise commands, decisions and loops.
</span>
</button>
<button type="button" class="tutorial-option"
data-tutorial-mode="python">
<span class="tutorial-option-title">New to Python</span>
<span class="tutorial-option-description">
For participants who know some programming but not Python. A quick
tour of calls, output, Booleans, indentation, decisions and loops.
</span>
</button>
<button type="button" class="tutorial-option"
data-tutorial-mode="instructions">
<span class="tutorial-option-title">Quick start</span>
<span class="tutorial-option-description">
For participants already familiar with Python. Tours the maze,
editor, controls, output and help before independent work.
</span>
</button>
</div>
<button id="closeTutorialSelector" class="tutorial-selector-close"
type="button" hidden>Keep current tutorial</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">
<p id="tutorialStepCount" class="tutorial-step-count"></p>
<h2 id="tutorialStepTitle"></h2>
<div id="tutorialStepBody" class="tutorial-step-body"></div>
<p id="tutorialFeedback" class="tutorial-feedback" aria-live="polite"
hidden></p>
<div class="tutorial-code-action">
<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>
</body>
</html>