Skip to content

Commit e0998d7

Browse files
committed
GH-2: Refactored to add the ComputerPlayer(Player) class.
1 parent e88e04d commit e0998d7

File tree

3 files changed

+67
-108
lines changed

3 files changed

+67
-108
lines changed

constants.py

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"o": [10, 100, 50],
7272
"c": [100, 50, 25], # default of settlement store (level 1)
7373
}
74+
MAP_WALLS = ["s", "l", "m", "o"] # used in path algorithms to distinguish where the player can move.
7475

7576

7677
UNIT_SPECS = {

project/game/computer_player.py

-103
This file was deleted.

project/game/model.py

+66-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import constants
66

77
import project.game.calculations as calculations
8-
import project.game.computer_player as computer_player
98

109

1110
class Model:
@@ -15,9 +14,16 @@ def __init__(self, game_name, map_name, players): # only when creating new game
1514
self.map_name = map_name
1615
self.game_end = False
1716

18-
self.players = [Player(p["name"], p["colour"], p["control"]) for p in players]
17+
self.players = []
18+
for player in players:
19+
if player["control"] == "computer":
20+
self.players.append(ComputerPlayer(player["name"], player["colour"]))
21+
elif player["control"] == "human":
22+
self.players.append(Player(player["name"], player["colour"]))
23+
else:
24+
raise Exception("Invalid Player Control Given: %s" % player["control"])
25+
1926
self.current_player = self.players[0]
20-
self.computer_logic = computer_player.Logic()
2127

2228
self.world = World(self.map_name, self.players) # assigns settlements to players
2329

@@ -62,7 +68,7 @@ def cycle_player(self):
6268

6369
# Computer takes go, then we go on to find next human player
6470
if self.current_player.get_control() == "computer":
65-
self.computer_logic.take_go(self) # of current player
71+
self.current_player.take_go(self) # of current player
6672
if self.current_player.is_dead():
6773
self.current_player.units = []
6874

@@ -175,7 +181,7 @@ def computer_turn(self):
175181

176182
class Player:
177183
""" Each player of the game, which holds their units, key values and links to settlements etc"""
178-
def __init__(self, name, colour, control):
184+
def __init__(self, name, colour, control="human"):
179185
self.name = name
180186
self.colour = colour
181187
self.control = control
@@ -273,6 +279,61 @@ def set_minimap_status(self, show):
273279
self.show_minimap = show
274280

275281

282+
class ComputerPlayer(Player):
283+
def __init__(self, name, colour):
284+
super().__init__(name, colour, "computer")
285+
286+
def take_go(self, model):
287+
model.current_player.start_turn()
288+
289+
self.handle_cities(model)
290+
self.handle_units(model)
291+
292+
model.current_player.end_turn()
293+
294+
def handle_cities(self, model):
295+
for city in model.current_player.settlements:
296+
# Spawning Units
297+
affordable_units = [name for name, values in constants.UNIT_SPECS.items()
298+
if model.current_player.get_ap() - values["spawn_cost"] > 0]
299+
unit_choice = random.choice(affordable_units)
300+
model.try_spawn(unit_choice, city.get_position())
301+
302+
def handle_units(self, model):
303+
for unit in model.current_player.units:
304+
if False: # in city conquer it
305+
pass
306+
else:
307+
# breadth_search = breadth_first.BreadthSearch(model.world.get_format())
308+
#
309+
# nearest_city = breadth_search.get_nearest(unit.position, "c")
310+
# path = shortest_path.GridPath(
311+
# model.world.get_format(),
312+
# unit.position, nearest_city,
313+
# walls=MAP_WALLS
314+
# ).get_path()
315+
# print(path)
316+
317+
# See if unit can take a city
318+
if model.check_conquer(unit):
319+
model.conquer(unit.position)
320+
else:
321+
# Make Random Move
322+
all_moves = model.get_moves(unit)
323+
if len(all_moves) > 0:
324+
move = random.choice(all_moves)
325+
model.move_unit(move, unit)
326+
327+
# Make Random Attack (if possible)
328+
all_attacks = model.get_attacks(unit)
329+
if len(all_attacks) > 0:
330+
all_units = sorted(
331+
[model.get_unit(position) for position in all_attacks],
332+
key=lambda x: x.health)
333+
enemy_unit = all_units[0] # target the weakest enemy
334+
model.make_attack(unit, enemy_unit)
335+
336+
276337
class Tile:
277338
def __init__(self, tile_type, position):
278339
self.type = tile_type

0 commit comments

Comments
 (0)