After adding "error messages" when the robot hits a wall and has
problems with beepers, and adding robot positioning in the GUI world
builder, I felt ready to tackle some of Roger Frank's lessons. You
won't be suprised to hear that, in the process, I found a few bugs.=20
However, here's an example that now works properly, taken from Roger
Frank's lesson 2.
=3D=3D Initial world file =3D=3D
robot =3D (1, 5, 4)
walls =3D []
beepers =3D {}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Note that my robot is *always* facing South by default.
(It's drawn so that it looks better that way :-)
I think that using Python syntax for strings (e.g. 'N')
would be too much at this stage.
Ok, here's the "normal solution"
=3D=3Dlesson2.py=3D=3D
turnleft()
move()
move()
put_beeper()
move()
put_beeper()
move()
put_beeper()
move()
put_beeper()
move()
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
This is the final world configuration:
robot =3D (7, 5, 0)
walls =3D []
beepers =3D {
(5, 5): 1,=20
(4, 5): 1,=20
(6, 5): 1,=20
(3, 5): 1
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Notice that the coordinates for the beepers do not appear to be in an
ordered way; this is typical of a Python dictionary.
Also note that there is no "import powermode" command.
Now, for variety, here's a more advanced solution:
=3D=3Dlesson2a.py=3D=3D
def put_and_move():
put_beeper()
move()
=20
turnleft()
move()
move()
repeat(put_and_move, 4)
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
I have introduced a "repeat function" whose syntax
is as follows:
repeat(command, number_of_times)
and is defined internally as follows:
def repeat(f, n):
for i in range(n):
f()
This is to replace the "do" command in GvR,
but hiding from the student the complication of for loops.
I think that "repeat" is more appropriate than "do", but I'm willing
to listen to thoughts to the contrary :-)
Andr=E9
|