-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayground.py
301 lines (225 loc) · 7.3 KB
/
playground.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
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
import sys
import random
from termcolor import colored
class ColorPalette:
fg = ('white', 'blue', 'cyan', 'magenta',
'green', 'red', 'yellow')
bg = ('on_white', 'on_blue', 'on_cyan', 'on_magenta',
'on_green', 'on_red', 'on_yellow')
class OutOfColorsException(Exception):
"""Not enough colors to satisfy request"""
pass
def __init__(self, num_colors=0):
if num_colors == 0:
num_colors = len(ColorPalette.fg)
if num_colors > len(ColorPalette.fg):
raise OutOfColorsException
return None
self.num_colors = num_colors
def get_fg_color(self, num):
return self.fg[num]
def get_bg_color(self, num):
return self.bg[num]
def get_random_idx(self):
return random.randint(0, self.num_colors - 1)
def get_a_fg_color(self):
idx = self.get_random_idx()
return self.fg[idx]
def get_a_bg_color(self):
idx = self.get_random_idx()
return self.bg[idx]
class FieldColorPalette:
"""Set of possible field colors
Specifically set foreground and background colors
"""
def __init__(self, palette=ColorPalette(), num_colors=0, colors=[]):
self.colors = []
# Specific colors given
if colors:
self.colors = colors
return
# Construct field colors, do not use white color
for idx in range(1, num_colors + 1):
c = FieldColor(palette.get_fg_color(idx),
palette.get_bg_color(idx))
self.colors.append(c)
def contains(self, field_color):
"""field_color part of the palette"""
for c in self.colors:
if c.is_equal(field_color):
return True
else:
return False
def add(self, field_color):
"""Add color to palette"""
if self.contains(field_color):
return
self.colors.append(field_color)
def num(self):
"""Number of colors in the palette"""
return len(self.colors)
def get_random_idx(self):
return random.randint(0, self.num() - 1)
def get_a_color(self):
idx = self.get_random_idx()
return self.colors[idx]
def get_color(self, num):
return self.colors[num]
def plot(self):
"""Return palette field color string"""
s = ""
for c in self.colors:
s += "%s" % str(c)
return s
class FieldColor:
"""Color from a palette"""
def __init__(self, fg, bg):
self.fg = fg
self.bg = bg
def __str__(self):
return colored(' ', self.fg, self.bg)
def is_equal(self, color):
return self.get_fg_color() == color.get_fg_color() and \
self.get_bg_color() == color.get_bg_color()
def get_fg_color(self):
return self.fg
def get_bg_color(self):
return self.bg
class Field:
"""Field on the playground"""
def __init__(self, palette):
self.palette = palette
self.color = palette.get_color(0)
self.visited = {}
# TODO: x,y coordinates?
# TODO: Neighbours?
# TODO: n-dimensional?
def __str__(self):
return "Field %s" % self.color
def has_color(self, color):
return self.color.is_equal(color)
def get_color(self):
return self.color
def set_color(self, color):
self.color = color
def set_a_color(self):
self.color = self.palette.get_a_color()
def set_visited(self, tag):
self.visited[tag] = True
def is_visited(self, tag):
return tag in self.visited
def remove_visited(self, tag):
self.visited.pop(tag, None)
def plot(self):
sys.stdout.write(colored(' ', self.color.fg, self.color.bg))
class PlayGroundDimension:
"""Dimension of the Playground
Will allow us to generically walk through the field
- next()
- prev()
- neighbours()
"""
pass
class Position:
"""Position on the field"""
# TODO: Iterate over dimensions
class InvalidPosition(Exception):
"""Requested position is not on the Playground"""
pass
def __init__(self, pg, x, y):
if x < 0 or y < 0:
raise InvalidPosition
return None
self.x = x
self.y = y
self.pg = pg
def left(self):
if self.x - 1 < 0:
return None
else:
return Position(self.pg, self.x - 1, self.y)
def right(self):
if self.x + 1 >= self.pg.x:
return None
else:
return Position(self.pg, self.x + 1, self.y)
def up(self):
if self.y - 1 < 0:
return None
else:
return Position(self.pg, self.x, self.y - 1)
def down(self):
if self.y + 1 >= self.pg.y:
return None
else:
return Position(self.pg, self.x, self.y + 1)
class PlayGround:
"""Playground"""
@staticmethod
def generate_tag():
return uuid.uuid4().bytes
def __init__(self, x, y, palette):
"""Dimensions x, y, number of colors available"""
self.x = x
self.y = y
self.palette = palette
self.field = [[Field(self.palette) for i in range(x)] for j in range(y)]
def inside(self, pos):
"""Is position inside the field"""
if pos.x <= self.x and pos.y <= self.y:
return True
else:
return False
def copy(self):
new = PlayGround(self.x, self.y, self.palette)
for i in range(self.x):
for j in range(self.y):
c = self.field[i][j].get_color()
new.field[i][j].set_color(c)
return new
def fill_random(self):
"""Fill the field with random"""
for i in range(self.x):
for j in range(self.y):
self.field[i][j].set_a_color()
def plot(self):
"""Plot the playground"""
for x in range(self.x):
for y in range(self.y):
self.field[x][y].plot()
print ""
def get_field(self, pos):
"""Get a field by position"""
if not self.inside(pos):
return None
else:
return self.field[pos.x][pos.y]
def remove_visited(self, tag):
"""Remove a visited tag from all fields"""
for i in range(self.x):
for j in range(self.y):
self.field[i][j].remove_visited(tag)
def walk_helper(self, pos, color, handler, tag):
"""Helper function to walk the playground
"""
# Outside of the playground
if pos == None:
return
field = self.get_field(pos)
if field.is_visited(tag):
return
# Execute handler
handler(field, True)
field.set_visited(tag)
# Visit neighbours
self.walk_helper(pos.left(), color, handler, tag)
self.walk_helper(pos.right(), color, handler, tag)
self.walk_helper(pos.up(), color, handler, tag)
self.walk_helper(pos.down(), color, handler, tag)
def walk(self, handler):
"""Visit all fields of the area"""
if not handler:
return
tag = PlayGround.generate_tag()
self.walk_helper(self.pos, self.get_color(), handler, tag)
self.pg.remove_visited(tag)