-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfigure.py
105 lines (92 loc) · 1.99 KB
/
figure.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
#using python2
from copy import deepcopy
from random import choice
import re
FIGURES= (\
'''
|.#.|.#.|...|.#.|
|###|.##|###|##.|
|...|.#.|.#.|.#.|
'''
, \
'''
|.##|...|.#.|#..|
|.#.|###|.#.|###|
|.#.|..#|##.|...|
'''
, \
'''
|##.|..#|.#.|...|
|.#.|###|.#.|###|
|.#.|...|.##|#..|
'''
, \
'''
|##|
|##|
'''
, \
'''
|##.|.#.|
|.##|##.|
|...|#..|
'''
, \
'''
|.##|#..|
|##.|##.|
|...|.#.|
'''
, \
'''
|....|.#..|
|####|.#..|
|....|.#..|
|....|.#..|
'''
)
class State:
def __init__(self, point, variant):
self.global_marker= list(point)
self.variant= variant
class Figure:
def __init__(self):
self.figure_variants= []
raw_fig= choice(FIGURES)
#print raw_fig
variants= zip(*[filter(None, row.split('|'))
for row in filter(None, raw_fig.split('\n'))])
for variant in variants:
figure_variant=[]
for y in range(len(variant)):
figure_variant.extend(
[(f.start(), y) for f in re.finditer('#', variant[y])])
self.figure_variants.append(tuple(figure_variant))
self.figure_variants= tuple(self.figure_variants)
self.variant_count= len(self.figure_variants)
self.cur_state= State((3, 21), 0)
self.prev_state= None
def down_move(self):
self.prev_state= deepcopy(self.cur_state)
self.cur_state.global_marker[1] -= 1
def left_move(self):
self.prev_state= deepcopy(self.cur_state)
self.cur_state.global_marker[0] -= 1
def right_move(self):
self.prev_state= deepcopy(self.cur_state)
self.cur_state.global_marker[0] += 1
def left_turn(self):
self.prev_state= deepcopy(self.cur_state)
idx= self.cur_state.variant - 1
self.cur_state.variant= idx % self.variant_count #that's cool
def right_turn(self):
self.prev_state= deepcopy(self.cur_state)
idx= self.cur_state.variant + 1
self.cur_state.variant= idx % self.variant_count
def rollback(self):
self.cur_state= deepcopy(self.prev_state)
self.prev_state= None
def get_all(self):
gx, gy= self.cur_state.global_marker
return [(gx+x, gy-y) for x,y in self.figure_variants[self.cur_state.variant]]
Figure()