Skip to content

Commit a3de571

Browse files
committed
GH-2: Refactored branch to be compatible with changes added in 365040f from GH-5 work.
1 parent 365040f commit a3de571

File tree

2 files changed

+54
-31
lines changed

2 files changed

+54
-31
lines changed

project/game/model.py

+45-31
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ def __init__(self, save_data):
1414
self.map_name = save_data["map_name"]
1515
self.game_end = save_data["game_end"]
1616

17-
self.players = [Player(self, player_data) for player_data in save_data["players"]]
17+
self.players = []
18+
for player_data in save_data["players"]:
19+
if player_data["control"] == "human":
20+
self.players.append(Player(self, player_data))
21+
elif player_data["control"] == "computer":
22+
self.players.append(ComputerPlayer(self, player_data))
23+
else:
24+
raise Exception("Invalid player control given: %s" % player_data["control"])
1825

1926
self.world = World(self, save_data["world"]) # assigns settlements to players
2027

@@ -73,20 +80,19 @@ def cycle_player(self):
7380
valid_choice = False
7481

7582
while not valid_choice: # wont be infinite, as to be called at least two players are left.
76-
if self.players.index(self.current_player) < len(self.players) - 1:
77-
self.current_player = self.players[self.players.index(self.current_player) + 1]
83+
if self.players.index(self.get_current_player()) < len(self.players) - 1:
84+
self.current_player_name = self.players[self.players.index(self.get_current_player()) + 1].get_name()
7885
else:
79-
self.current_player = self.players[0]
86+
self.current_player_name = self.players[0].get_name()
8087

8188
# Computer takes go, then we go on to find next human player
82-
if self.current_player.get_control() == "computer":
83-
self.current_player.take_go(self) # of current player
84-
if self.current_player.is_dead():
85-
self.current_player.units = []
86-
87-
valid_choice = not self.current_player.is_dead() and self.current_player.get_control() == "human"
89+
current_player = self.get_current_player()
90+
if current_player.get_control() == "computer":
91+
current_player.take_go() # of current player
92+
if current_player.is_dead():
93+
current_player.units = []
8894

89-
return self.current_player
95+
valid_choice = not self.get_current_player().is_dead() and self.get_current_player().get_control() == "human"
9096

9197
def try_spawn(self, unit_type, position):
9298
if not self.get_unit(position):
@@ -198,6 +204,8 @@ class Player:
198204
def __init__(self, model_link, saved_data):
199205
self.model_link = model_link
200206

207+
self.control = saved_data["control"]
208+
201209
self.name = saved_data["name"]
202210
self.colour = saved_data["colour"]
203211
self.camera_focus = saved_data["camera_focus"]
@@ -216,6 +224,8 @@ def __init__(self, model_link, saved_data):
216224

217225
def get_save_data(self):
218226
return {
227+
"control": self.get_control(),
228+
219229
"name": self.get_name(),
220230
"colour": self.get_colour(),
221231
"camera_focus": self.get_camera_focus(),
@@ -316,27 +326,31 @@ def set_minimap_status(self, show):
316326

317327

318328
class ComputerPlayer(Player):
319-
def __init__(self, name, colour):
320-
super().__init__(name, colour, "computer")
329+
def __init__(self, model_link, save_data):
330+
super().__init__(model_link, save_data)
321331

322-
def take_go(self, model):
323-
model.current_player.start_turn()
332+
def take_go(self, ):
333+
self.model_link.get_current_player().start_turn()
324334

325-
self.handle_cities(model)
326-
self.handle_units(model)
335+
self.handle_cities()
336+
self.handle_units()
327337

328-
model.current_player.end_turn()
338+
self.model_link.get_current_player().end_turn()
329339

330-
def handle_cities(self, model):
331-
for city in model.current_player.settlements:
340+
def handle_cities(self):
341+
current_player = self.model_link.get_current_player()
342+
for city_position in current_player.settlements:
332343
# Spawning Units
333344
affordable_units = [name for name, values in constants.UNIT_SPECS.items()
334-
if model.current_player.get_ap() - values["spawn_cost"] > 0]
345+
if current_player.get_ap() - values["spawn_cost"] >= 0]
335346
unit_choice = random.choice(affordable_units)
336-
model.try_spawn(unit_choice, city.get_position())
337347

338-
def handle_units(self, model):
339-
for unit in model.current_player.units:
348+
city = self.model_link.world.get_tile(city_position)
349+
self.model_link.try_spawn(unit_choice, city.get_position())
350+
351+
def handle_units(self):
352+
current_player = self.model_link.get_current_player()
353+
for unit in current_player.units:
340354
if False: # in city conquer it
341355
pass
342356
else:
@@ -351,23 +365,23 @@ def handle_units(self, model):
351365
# print(path)
352366

353367
# See if unit can take a city
354-
if model.check_conquer(unit):
355-
model.conquer(unit.position)
368+
if self.model_link.check_conquer(unit):
369+
self.model_link.conquer(unit.position)
356370
else:
357371
# Make Random Move
358-
all_moves = model.get_moves(unit)
372+
all_moves = self.model_link.get_moves(unit)
359373
if len(all_moves) > 0:
360374
move = random.choice(all_moves)
361-
model.move_unit(move, unit)
375+
self.model_link.move_unit(move, unit)
362376

363377
# Make Random Attack (if possible)
364-
all_attacks = model.get_attacks(unit)
378+
all_attacks = self.model_link.get_attacks(unit)
365379
if len(all_attacks) > 0:
366380
all_units = sorted(
367-
[model.get_unit(position) for position in all_attacks],
381+
[self.model_link.get_unit(position) for position in all_attacks],
368382
key=lambda x: x.health)
369383
enemy_unit = all_units[0] # target the weakest enemy
370-
model.make_attack(unit, enemy_unit)
384+
self.model_link.make_attack(unit, enemy_unit)
371385

372386

373387
class Tile:

project/game/new.py

+9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ def make(game_name, map_name, players):
3131

3232

3333
def get_player_data(player_name, player_colour):
34+
35+
control = ""
36+
if "comp" in player_name:
37+
control = "computer"
38+
else:
39+
control = "human"
40+
3441
return {
42+
"control": control,
43+
3544
"name": player_name,
3645
"colour": player_colour,
3746
"camera_focus": [None, None], # will be generated on load.

0 commit comments

Comments
 (0)