home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Crawly Crypt Collection 1
/
crawlyvol1.bin
/
program
/
compiler
/
elan1v5
/
karel
/
charles.e
next >
Wrap
Text File
|
1989-03-10
|
3KB
|
152 lines
PROC turn right:
dx INCR dy;
dy DECR dx;
dx INCR dy;
draw karel
ENDPROC turn right;
PROC turn left:
dy INCR dx;
dx DECR dy;
dy INCR dx;
draw karel
ENDPROC turn left;
PROC take beeper:
IF beeper
THEN corner [xpos] [ypos] DECR ball
ELSE
go to screen bottom;
put (" There is no beeper at this corner.");
assert (karel error)
FI
ENDPROC take beeper;
PROC stop:
go to screen bottom;
put (" Karel has completed his task.");
sleep (1);
disappear karel;
go to screen bottom
ENDPROC stop;
PROC set cursor:
cursor (xstep * xpos, ystep * (height + 1 - ypos))
ENDPROC set cursor;
PROC reset cursor:
cursor (1, ystep * (height + 1 - ypos))
ENDPROC reset cursor;
PROC place karel on corner:
corner [xpos] [ypos] INCR karel;
draw karel
ENDPROC place karel on corner;
PROC move:
disappear karel;
try to move;
place karel on corner.
try to move:
IF facing wall
THEN
go to screen bottom;
IF leaves town
THEN put (" Karel disappeared from sight.")
ELSE put (" Karel hit the wall.")
FI
ELSE
xpos INCR dx;
ypos INCR dy
FI.
ENDPROC move;
BOOL PROC leaves town:
xpos + dx < 1 OR xpos + dx > width OR (ypos + dy < 1 OR ypos + dy > height)
ENDPROC leaves town;
PROC go to screen bottom:
cursor (1, ystep * (height + 1))
ENDPROC go to screen bottom;
BOOL PROC facing west:
dx = - 1
ENDPROC facing west;
BOOL PROC facing wall:
IF leaves town
THEN true
ELSE corner [xpos + dx] [ypos + dy] = stone
FI
ENDPROC facing wall;
BOOL PROC facing south:
dy = - 1
ENDPROC facing south;
BOOL PROC facing north:
dy = 1
ENDPROC facing north;
BOOL PROC facing east:
dx = 1
ENDPROC facing east;
PROC drop beeper:
IF beeper
THEN
go to screen bottom;
put (" There is already a beeper at this corner.");
assert (karel error)
ELSE corner [xpos] [ypos] INCR ball
FI
ENDPROC drop beeper;
PROC draw karel:
set cursor;
IF facing north
THEN put ("^")
ELIF facing east
THEN put (">")
ELIF facing south
THEN put ("v")
ELSE put ("<")
FI;
reset cursor
ENDPROC draw karel;
PROC disappear karel:
corner [xpos] [ypos] DECR karel;
set cursor;
IF beeper
THEN put ("o")
ELSE put (" ")
FI;
reset cursor
ENDPROC disappear karel;
BOOL PROC beeper:
corner [xpos] [ypos] MOD karel = ball
ENDPROC beeper;
start karel:
LET width = 29, height = 21;
LET xstep = 2, ystep = 1;
LET empty = 0, ball = 1, stone = 2, karel = 3;
INT VAR xpos, ypos;
INT VAR dx :: 0, dy :: 1;
ROW width ROW height INT VAR corner;
FOR ypos FROM 1 UPTO height
REP corner [1] [ypos] := empty
ENDREP;
FOR xpos FROM 2 UPTO width
REP corner [xpos] := corner [1]
ENDREP;
xpos := 1;
ypos := 1;
BOOL CONST karel error :: FALSE;
place karel on corner.
⇦