forked from alts/karel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
165 lines (134 loc) · 4.86 KB
/
board.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
158
159
160
161
162
163
164
165
#!/usr/bin/env python
import curses
import time
from karel import Karel
class LogicException(Exception):
pass
class Board(object):
KAREL_CHARS = '<^>v'
BEEPER_CHAR = 'o'
def __init__(self, map_path):
self.map = None
self.karel = None
self.screen = None
self.beepers = []
self.construct_map(map_path)
def __enter__(self):
self.start_screen()
return self
def __exit__(self, *args):
self.end_screen()
def start_screen(self):
self.screen = curses.initscr()
curses.start_color()
# wall
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_WHITE)
# Karel
curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK)
# beeper
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
# Karel on beeper
curses.init_pair(4, curses.COLOR_RED, curses.COLOR_BLACK)
def end_screen(self):
self.screen.getch()
curses.endwin()
def construct_map(self, map_path):
directions = {
'>': (1, 0),
'^': (0, 1),
'<': (-1, 0),
'v': (0, -1),
}
map = [[]]
with open(map_path, 'rb') as f:
for y, line in enumerate(f):
row = []
for x, char in enumerate(line.strip()):
if char in self.KAREL_CHARS:
self.karel = Karel((x + 1, y + 1), directions[char])
char = '.'
elif char == self.BEEPER_CHAR:
self.beepers.append((x + 1, y + 1))
char = '.'
row.append(char)
map.append(['#'] + row + ['#'])
map.append([])
for _ in xrange(len(map[1])):
map[0].append('#')
map[-1].append('#')
self.map = map
def draw(self):
screen = self.screen
for y, row in enumerate(self.map):
for x, char in enumerate(row):
args = [y + 1, x, char]
if char == '#':
args.append(curses.color_pair(1))
elif char == 'o':
args.append(curses.color_pair(3))
screen.addstr(*args)
beeper_color = curses.color_pair(3)
for bx, by in self.beepers:
screen.addstr(by + 1, bx, 'o', beeper_color)
screen.addstr(
self.karel.position[1] + 1,
self.karel.position[0],
self.karel_char(),
curses.color_pair(4) if self.beeper_is_present() else curses.color_pair(2)
)
screen.addstr(y + 4, 0, ' ')
screen.refresh()
time.sleep(0.5)
def draw_exception(self, exception):
curses.init_pair(5, curses.COLOR_RED, curses.COLOR_BLACK)
self.screen.addstr(0, 0, str(exception), curses.color_pair(5))
def karel_char(self):
# index will be in (-2, -1, 1, 2)
index = self.karel.facing[0] + 2*self.karel.facing[1]
return ' >v^<'[index]
# Karel passthroughs
def move(self):
if not self.front_is_clear():
raise LogicException('can\'t move. There is a wall in front of Karel')
self.karel.move()
def turn_left(self):
self.karel.turn_left()
def pick_beeper(self):
position = self.karel.position
for i, coord in enumerate(self.beepers):
if coord == self.karel.position:
del self.beepers[i]
self.karel.pick_beeper()
break
else:
raise LogicException('can\'t pick beeper from empty location')
def put_beeper(self):
if not self.holding_beepers():
raise LogicException('can\'t put beeper. Karel has none')
self.beepers.append(self.karel.position)
self.karel.put_beeper()
# world conditions
def front_is_clear(self):
next_x = self.karel.position[0] + self.karel.facing[0]
next_y = self.karel.position[1] + self.karel.facing[1]
return self.map[next_y][next_x] == '.'
def left_is_clear(self):
next_x = self.karel.position[0] + self.karel.facing[1]
next_y = self.karel.position[1] - self.karel.facing[0]
return self.map[next_y][next_x] == '.'
def right_is_clear(self):
next_x = self.karel.position[0] - self.karel.facing[1]
next_y = self.karel.position[1] + self.karel.facing[0]
return self.map[next_y][next_x] == '.'
def facing_north(self):
return self.karel.facing[1] == -1
def facing_south(self):
return self.karel.facing[1] == 1
def facing_east(self):
return self.karel.facing[0] == 1
def facing_west(self):
return self.karel.facing[0] == -1
def beeper_is_present(self):
return self.karel.position in self.beepers
def holding_beepers(self):
return self.karel.holding_beepers()