-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.py
189 lines (160 loc) · 5.73 KB
/
card.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
class Card():
pass
class ActionCard(Card):
def __init__(self, action):
assert action in ['map', 'sabotage', 'mend', 'dynamite'], "The parameter action must be either map, sabotage, mend or dynamite"
self._action = action
def get_action(self):
return self._action
class InvalidTunnel(Exception):
pass
class PathCard(Card):
def __init__(self, tunnels, special_card=None):
assert isinstance(tunnels, list), "The parameter tunnels must be a list of tuples"
assert special_card in ['start', 'goal', 'gold', None], "The parameter special_card must be either None, start, goal or gold"
for tunnel in tunnels:
if not self._is_valid_tunnel(tunnel):
raise InvalidTunnel("The tunnel '{0}' is an invalid one for this card.".format(tunnel))
self._special_card = special_card
self._revealed = True
if special_card:
# special cards are all cross roads
cross_road = PathCard.cross_road()
self._tunnels = cross_road.get_tunnels()
if special_card in ['goal', 'gold']:
self._revealed = False
else:
self._tunnels = tunnels
def cross_road(special_card=None):
return PathCard(
[
('north', 'south'),
('north', 'east'),
('north', 'west'),
('south', 'east'),
('south', 'west'),
('east', 'west')
], special_card=special_card
)
def vertical_tunnel():
return PathCard(
[
('north', 'south')
]
)
def horizontal_tunnel():
return PathCard(
[
('east', 'west')
]
)
def vertical_junction():
return PathCard(
[
('north', 'south'),
('north', 'east'),
('south', 'east')
]
)
def horizontal_junction():
return PathCard(
[
('east', 'north'),
('west', 'north'),
('east', 'west')
]
)
def turn():
return PathCard(
[
('south', 'east')
]
)
def reversed_turn():
return PathCard(
[
('south', 'west')
]
)
def dead_end(directions):
tunnels = []
for direction in directions:
tunnels.append((direction, None))
return PathCard(tunnels)
def _is_valid_tunnel(self, tunnel):
if not isinstance(tunnel, tuple):
return False
if len(tunnel) != 2:
return False
for direction in tunnel:
if direction not in ['north', 'east', 'south', 'west', None]:
return False
if tunnel[0] is None:
return False
if tunnel[0] is None and tunnel[1] is None:
return False
if tunnel[0] == tunnel[1]:
return False
return True
def is_special_card(self):
return self._special_card is not None
def is_gold(self):
return self._special_card == 'gold'
def reveal_card(self):
self._revealed = True
def turn_card(self):
tunnels = []
opposite = {
'north': 'south',
'east': 'west',
'west': 'east',
'south': 'north',
}
for tunnel in self._tunnels:
new_tunnel = (
opposite[tunnel[0]] if tunnel[0] is not None else None,
opposite[tunnel[1]] if tunnel[1] is not None else None
)
tunnels.append(new_tunnel)
self._tunnels = tunnels
def get_tunnels(self):
return self._tunnels.copy()
def is_cross_road(self):
# check if the card is a cross_road type
# Here, I am assuming the distinguishing feature is the list of tunnels. Adjust accordingly.
tunnels = [
('north', 'south'),
('north', 'east'),
('north', 'west'),
('south', 'east'),
('south', 'west'),
('east', 'west')
]
return set(self._tunnels) == set(tunnels)
def __str__(self):
card_rep = [' ', ' ', ' ']
if self._revealed:
for tunnel in self._tunnels:
directions = [(tunnel[0], tunnel[1]), (tunnel[1], tunnel[0])]
for direction in directions:
tunnel_from = direction[0]
tunnel_to = direction[1]
if tunnel_from == 'north':
card_rep[0] = card_rep[0][:1] + '|' + card_rep[0][2:]
if tunnel_to is not None:
card_rep[1] = card_rep[1][:1] + '┼' + card_rep[1][2:]
elif tunnel_from == 'south':
card_rep[2] = card_rep[2][:1] + '|' + card_rep[2][2:]
if tunnel_to is not None:
card_rep[1] = card_rep[1][:1] + '┼' + card_rep[1][2:]
elif tunnel_from == 'east':
card_rep[1] = card_rep[1][:2] + '—'
if tunnel_to is not None:
card_rep[1] = card_rep[1][:1] + '┼' + card_rep[1][2:]
elif tunnel_from == 'west':
card_rep[1] = '—' + card_rep[1][1:]
if tunnel_to is not None:
card_rep[1] = card_rep[1][:1] + '┼' + card_rep[1][2:]
else:
return ' \n ? \n '
return '\n'.join(card_rep)