5
5
import constants
6
6
7
7
import project .game .calculations as calculations
8
- import project .game .computer_player as computer_player
9
8
10
9
11
10
class Model :
@@ -15,9 +14,16 @@ def __init__(self, game_name, map_name, players): # only when creating new game
15
14
self .map_name = map_name
16
15
self .game_end = False
17
16
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
+
19
26
self .current_player = self .players [0 ]
20
- self .computer_logic = computer_player .Logic ()
21
27
22
28
self .world = World (self .map_name , self .players ) # assigns settlements to players
23
29
@@ -62,7 +68,7 @@ def cycle_player(self):
62
68
63
69
# Computer takes go, then we go on to find next human player
64
70
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
66
72
if self .current_player .is_dead ():
67
73
self .current_player .units = []
68
74
@@ -175,7 +181,7 @@ def computer_turn(self):
175
181
176
182
class Player :
177
183
""" 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" ):
179
185
self .name = name
180
186
self .colour = colour
181
187
self .control = control
@@ -273,6 +279,61 @@ def set_minimap_status(self, show):
273
279
self .show_minimap = show
274
280
275
281
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
+
276
337
class Tile :
277
338
def __init__ (self , tile_type , position ):
278
339
self .type = tile_type
0 commit comments