-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards.py
196 lines (141 loc) · 4.67 KB
/
cards.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
from constants import VICTORY_POINT, RED, RESOURCE, TILE, BLUE, YELLOW, PLAN, URBANIZE, BUILD_UP, COLORS, RESET
from pieces import Tile
class Card:
def __init__(self, target):
self.target = target
def default(self, o):
return self.__str__()
def __str__(self):
return '/{}{}{}\\'.format(COLORS[self.target.color], self.target.value, COLORS[RESET])
def __repr__(self):
return self.__str__()
def __eq__(self, other):
return self.target == other.target
def toJSON(self):
return {
'target': self.target
}
class ActionCard(Card):
def __init__(self, target):
super().__init__(target)
self.action = PLAN
if self.target.value in [2, 5, 8]:
self.action = URBANIZE
elif self.target.value in [3, 6, 9]:
self.action = BUILD_UP
def benefit(self, action):
if action != self.action:
return (0, None)
kind = VICTORY_POINT
if self.target.color == RED:
kind = RESOURCE
elif self.target.color == BLUE:
kind = TILE
amount = 1
if isinstance(self.target.value, int) and self.target.value >= 7:
amount = 2
return (amount, kind)
def _matching_tiles(board, player, condition):
matches = []
for row in board:
for tile in row:
if tile.owner == player:
if condition(tile):
matches.append(tile)
return matches
class ColorEndgameCard(Card):
def __init__(self, target):
super().__init__(target)
def benefit(self, board, player):
if self.target.number == 10 or self.target.number == 13:
color = self.target.color
elif self.target.number == 11:
color = RED if self.target.color != RED else YELLOW
elif self.target.number == 12:
color = BLUE if self.target.color != BLUE else YELLOW
matching_tiles = _matching_tiles(board, player, lambda tile: tile.color == color)
amount = sum(len(tile.resources) for tile in matching_tiles)
return (amount, VICTORY_POINT)
class HeightEndgameCard(Card):
def __init__(self, target):
super().__init__(target)
def benefit(self, board, player):
condition = lambda tile: tile.resources <= 2
per_tile = 1
if self.target.number == 15:
condition = lambda tile: tile.resources >= 3
per_tile = 3
amount = len(_matching_tiles(board, player, condition))
return (amount * per_tile, VICTORY_POINT)
class CardEndgameCard(Card):
def __init__(self, target):
super().__init__(target)
def benefit(self, player):
action = None
if self.target.number == 16:
action = PLAN
elif self.target.number == 17:
action = URBANIZE
elif self.target.number == 18:
action = BUILD_UP
if action:
condition = lambda card: card.action == action
else:
condition = lambda card: card.target.color == self.target.color
amount = len([card for card in player.cards if condition(card)])
return (amount * 2, VICTORY_POINT)
class ConditionlessEndgameCard(Card):
def __init__(self, target):
super().__init__(target)
def benefit(self):
return (9, VICTORY_POINT)
class MarkerCard(Card):
def __init__(self, target):
super().__init__(target)
def card_factory(target):
if target.value <= 9:
cardClass = ActionCard
elif target.value <= 13:
cardClass = ColorEndgameCard
elif target.value <= 15:
cardClass = HeightEndgameCard
elif target.value <= 19:
cardClass = CardEndgameCard
else:
cardClass = ConditionlessEndgameCard
return cardClass(target)
class HeroCard(ActionCard):
def __init__(self):
self.target = Tile(self.color, self.name)
def toJSON(self):
return {
'color': self.color,
'action': self.action,
'starting': self.starting,
'name': self.name
}
class Scientist(HeroCard):
color = BLUE
action = BUILD_UP
starting = [RESOURCE]
name = 'S'
class Planter(HeroCard):
color = RED
action = URBANIZE
starting = [RESOURCE, VICTORY_POINT, TILE]
name = 'P'
class Architect(HeroCard):
color = YELLOW
action = BUILD_UP
starting = [RESOURCE, RESOURCE, VICTORY_POINT, TILE]
name = 'A'
class Clerk(HeroCard):
color = BLUE
action = PLAN
starting = [RESOURCE, RESOURCE, VICTORY_POINT]
name = 'C'
class Mechanic(HeroCard):
color = RED
action = BUILD_UP
starting = [TILE]
name = 'M'