-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
311 lines (261 loc) · 10.5 KB
/
Game.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
301
302
303
304
305
306
307
308
#!/usr/bin/python
from Player import *
from Board import *
from Card import *
from Unit import *
from Effect import *
from Const import *
import math
import logging
BOARD_LENGTH = 19
BOARD_WIDTH = 5
DRAW_FREQUENCY = 2
UPKEEP_GOLD = 2
MAX_HAND_SIZE = 5
class Game:
def __init__(self):
self.players = {}
self.board = None
self.setup_phase()
self.turn = 0
self.turn_advantage = 0
def reset(self):
self.board.clear()
def setup_phase(self):
# Set up board
self.board = Board(self, field_length=BOARD_LENGTH, field_width=BOARD_WIDTH)
# Instantiate Players
self.players[0] = Player(0)
self.players[1] = Player(1)
self.players[1].set_direction(Const.FACING_LEFT)
# Initial Decks
self.players[0].set_deck([
Card.get_card('Rifleman'),
Card.get_card('Footman'),
Card.get_card('Footman'),
Card.get_card('Peasant'),
Card.get_card('Peasant'),
])
self.players[0].set_hand([
Card.get_card('Footman'),
Card.get_card('Rifleman'),
Card.get_card('Scout'),
Card.get_card('Peasant'),
Card.get_card('Peasant'),
])
self.players[1].set_deck([
Card.get_card('Rifleman'),
Card.get_card('Footman'),
Card.get_card('Footman'),
Card.get_card('Peasant'),
Card.get_card('Peasant'),
])
self.players[1].set_hand([
Card.get_card('Footman'),
Card.get_card('Rifleman'),
Card.get_card('Scout'),
Card.get_card('Peasant'),
Card.get_card('Peasant'),
])
# Initial Heroes
middle = (BOARD_WIDTH - 1) / 2
self.put_in_play(Card.get_card('Arius'), 0, (-1, middle))
self.put_in_play(Card.get_card('Arius'), 1, (BOARD_LENGTH - 2, middle))
def main_loop(self):
# Main Loop
while (True):
winner = self.main_loop_once()
if winner is not None:
return winner
def main_loop_once(self):
self.pre_main_phases()
gameover = self.post_main_phases()
if gameover is not None:
return gameover
def pre_main_phases(self):
logging.info("********************")
logging.info("Beginning of Turn {0}".format(self.turn))
self.upkeep_phase()
self.cleanup_phase()
self.draw_phase()
def post_main_phases(self):
self.spell_phase()
self.damage_phase()
self.cleanup_phase()
self.move_phase()
self.money_phase()
result = self.cleanup_phase()
if result is not None:
return result
logging.info("End of Turn {0}".format(self.turn))
logging.info("********************")
self.increment_turn()
def increment_turn(self):
self.turn += 1
def upkeep_phase(self):
self.turn_advantage = self.calculate_advantage()
logging.info("Player {0} has advantage for this turn".format(self.turn_advantage))
# Refresh must occur first
self.board.refresh_units()
# Let modifiers do updates. This must occur before effects, as a
# effects may apply modifiers.
for location, unit in self.board.grid.iteritems():
for modifier in unit.modifiers:
modifier.upkeepLogic(self.board)
for id, player in self.players.iteritems():
for modifier in player.modifiers:
modifier.upkeepLogic(self.board)
for spell in self.board.spells[id]:
if spell is not None:
for modifier in spell.modifiers:
modifier.upkeepLogic(self.board)
for building in self.board.buildings[id]:
if building is not None:
for modifier in building.modifiers:
modifier.upkeepLogic(self.board)
for id, player in self.players.iteritems():
player.gold += UPKEEP_GOLD
self.apply_phase_effects()
def draw_phase(self):
if self.turn % DRAW_FREQUENCY == 0:
for id, player in self.players.iteritems():
self.players[id].draw()
def damage_phase(self):
first = self.get_turn_advantage()
second = (first + 1) % 2
self.board.do_combat(self.players[first], self.players[second])
# Reduce the health of every unit that is out of will by 1/3
for location, unit in self.board.grid.iteritems():
if unit.get_curr_ammo() <= 0:
unit._hp -= int(math.ceil(float(unit._max_hp) / 3.0))
def move_phase(self):
first = self.get_turn_advantage()
second = (first + 1) % 2
self.board.do_movements(self.players[first], self.players[second])
def money_phase(self):
"""
Workers get money from the sector they end up in, but should only be
paid for a sector once per life.
"""
for location, unit in self.board.grid.iteritems():
if isinstance(unit, Worker):
sector = self.board.get_sector_for_position(location)
# Flip sector for p1
if unit.owner_id == 1:
sector = len(self.board.SECTOR_COLS) - 1 - sector
if sector not in unit.visited_sectors:
payout = unit.payout[len(unit.visited_sectors)]
players[unit.owner_id].gold += payout
logging.info("{0} gold gained for sector {1}".format(payout, sector))
unit.visited_sectors.append(sector)
def spell_phase(self):
# Spell logic
first = self.get_turn_advantage()
second = (first + 1) % 2
self.board.process_spells(self.players[first], self.players[second])
self.board.process_spells(self.players[second], self.players[first])
def cleanup_phase(self):
logging.info("Start of cleanup phase")
num_removed = True
while (num_removed):
"""
We run cleanup whenever shit may be needed to be removed from
the board. Cleanup will go through every modifier and check if it
needs to go, then check if any units need to go. It'll keep doing
this until no units die (meaning no modifiers will change, meaning
we've reached a stable state)
"""
for location, unit in self.board.grid.iteritems():
for modifier in unit.modifiers:
modifier.cleanupLogic(self.board)
for id, player in self.players.iteritems():
for modifier in player.modifiers:
modifier.cleanupLogic(self.board)
for spell in self.board.spells[id]:
if spell is not None:
for modifier in spell.modifiers:
modifier.cleanupLogic(self.board)
for building in self.board.buildings[id]:
if building is not None:
for modifier in building.modifiers:
modifier.cleanupLogic(self.board)
num_removed = self.board.remove_dead()
# Calculate Gameover
is_tie = True
for id, player in self.players.iteritems():
if player.get_curr_health() > 0:
is_tie = False
for id, player in self.players.iteritems():
if player.get_curr_health() <= 0:
return 'Tie' if is_tie else (id + 1) % 2
return None
def get_turn_advantage(self):
return self.turn_advantage
def calculate_advantage(self):
return self.board.get_next_turn_advantage()
def play_unit(self, card_name, id, position):
"""plays a card for player id from his hand at position (u,v)"""
if not self.board.is_playable(self.players[id], position):
logging.debug("{0} not playable at {1}".format(card_name, position))
return False
card = self.players[id].play(card_name)
if card == None:
return False
unit = self.board.place_unit(card, self.players[id], position)
if unit.play_effect != None:
Effect.applyEffect(
unit.play_effect,
self.players[id],
self.players[(id + 1) % 2],
unit,
self.board,
unit.play_effect_args
)
return True
def play_spell(self, spell_name, id, slot):
""" Plays a spell at a given position (0-4 inclusive) for id"""
if (self.board.spells[id][slot]):
logging.debug("{0} not playable at {1}".format(spell_name, slot))
return False
card = self.players[id].play(spell_name)
if card == None:
return False
self.board.place_spell(card, self.players[id], slot)
return True
def play_building(self, building_name, id, slot):
""" Plays a buidling at a given position (0-4 inclusive) for id"""
if (self.board.buildings[id][slot]):
logging.debug("{0} not playable at {1}".format(building_name, slot))
return False
card = self.players[id].play(building_name)
if card == None:
return False
self.board.place_building(card, self.players[id], slot)
return True
def put_in_play(self, card, id, position):
""" puts a unit into play without paying the cost """
self.board.is_playable(self.players[id], position)
self.players[id].inplay.append(card)
self.board.grid[(position)] = Unit.get_unit(card, self.players[id])
def apply_phase_effects(self):
"""
Go through each building and apply its Effect
TODO: Should go through everything on the board and apply effects.
TODO: Make this phase independent
"""
first = self.get_turn_advantage()
second = (first + 1) % 2
self.apply_phase_effects_for_player(first, second)
self.apply_phase_effects_for_player(second, first)
def apply_phase_effects_for_player(self, player_id, opponent_id):
for object in self.board.get_everything():
if object and object.upkeep_effect and \
object.owner_id == player_id:
Effect.applyEffect(
object.upkeep_effect,
self.players[player_id],
self.players[opponent_id],
object,
self.board,
object.upkeep_effect_args
)