-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathkarel_run.py
executable file
·157 lines (127 loc) · 4.16 KB
/
karel_run.py
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
#!/usr/bin/env python3
"""Karel implementation in Python (with curses).
Import this file to command Karel using simple functions.
The curses screen starts upon import.
Note that all locally defined values are prefixed with '_'
to deter anyone who may be tempted to misuse them.
"""
from curses import endwin
import sys
import errno
from karel import *
def karel_help():
print("Karel expected usage:")
print("python3 YOUR_PROGRAM.py YOUR_WORLD.karelmap")
print("\nFor interactive mode:")
print("python3 karel_run.py YOUR_WORLD.karelmap")
###########################################################
# KAREL START-UP #
###########################################################
# Board is loaded and starts the curses window
try:
if len(sys.argv) == 1:
karel_help()
exit(errno.EPERM)
_karel, _karel_map = construct_map(sys.argv[1])
_w = Window(_karel, _karel_map)
except IOError as _e:
print("Could not open karel map file:\n" + str(_e))
exit(errno.ENOENT)
except RuntimeError as _e:
print("Failed parsing map:\n" + str(_e))
exit(errno.EINVAL)
except RobotError as _e:
endwin()
print("There was a problem with curses:\n" + str(_e))
exit(errno.EINVAL)
def refresh(func, moved=False):
"""Execute a Board method and redraw. """
try:
res = func()
_w.redraw(moved)
return res
except RobotError as _rob_e:
_w.draw_exception(_rob_e)
###########################################################
# KAREL FUNCTIONS #
###########################################################
# Movement
def move():
"""Karel tries to move in the direction he is facing. """
refresh(_w.move, True)
def turn_left():
"""Karel turns left. """
refresh(_w.karel.turn_left)
def turn_right():
"""Karel turns right. """
refresh(_w.karel.turn_right)
# Beepers
def pick_beeper():
"""Karel tries to pick up a beeper. """
refresh(_w.pick_beeper)
def put_beeper():
"""Karel puts down a beeper (if he has any). """
refresh(_w.put_beeper)
def beeper_is_present():
"""True iff Karel stands on a beeper. """
return _w.beeper_is_present()
# Walls
def front_is_blocked():
"""True iff Karel can't move forward. """
return _w.front_is_blocked()
def front_is_treasure():
"""True iff Karel stands in front of a Treasure. """
return _w.front_is_treasure()
# Direction
def facing_north():
"""True iff Karel is facing north (^). """
return _w.karel.facing_north()
def facing_south():
"""True iff Karel is facing south (v). """
return _w.karel.facing_south()
def facing_east():
"""True iff Karel is facing east (>). """
return _w.karel.facing_east()
def facing_west():
"""True iff Karel is facing west (<). """
return _w.karel.facing_west()
# Execution
def set_speed(spd):
"""Set how fast Karel moves (0 to 100). """
_w.speed = _w.valid_speed(spd)
def set_karel_beepers(b=0):
"""Set Karel's beepers, with None working as inf. """
_w.karel.beepers = b if b is None else max(0, round(b))
def pause():
"""Pause execution, press any key to continue. """
_w.pause()
###########################################################
# INTERACTIVE CLIENT FOR TESTING #
###########################################################
def interactive():
"""Command the created Board using your keyboard. """
set_speed(100) # do not wait
curses.noecho()
curses.cbreak()
_w.screen.keypad(1)
while True:
# end if user presses 'c'
ch = _w.screen.getch()
if ch != curses.ERR:
if ch == ord('q'):
exit()
elif ch == curses.KEY_LEFT:
turn_left()
elif ch == curses.KEY_RIGHT:
turn_right()
elif ch == curses.KEY_UP:
move()
elif ch == ord('u'):
put_beeper()
elif ch == ord('i'):
pick_beeper()
else:
_w.draw_exception("Use arrows to move, pIck, pUt and Quit.")
time.sleep(0.1)
if __name__ == '__main__':
interactive()