-
Notifications
You must be signed in to change notification settings - Fork 14
/
player.py
59 lines (48 loc) · 1.55 KB
/
player.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
from copy import deepcopy
class Player(object):
def __init__(self, name, game, ai_class, ai_kwargs):
self.name = name
self.color = 0
self.ord = 32
self.ai = ai_class(self, game, game.world, **ai_kwargs)
self.world = game.world
@property
def territories(self):
for t in self.world.territories.values():
if t.owner == self:
yield t
@property
def territory_count(self):
count = 0
for t in self.world.territories.values():
if t.owner == self:
count += 1
return count
@property
def areas(self):
for a in self.world.areas.values():
if a.owner == self:
yield a
@property
def forces(self):
return sum(t.forces for t in self.territories)
@property
def alive(self):
return self.territory_count > 0
@property
def reinforcements(self):
return max(self.territory_count//3, 3) + sum(a.value for a in self.areas)
def __repr__(self):
return "P;%s;%s" % (self.name, self.ai.__class__.__name__)
def __hash__(self):
return hash(("player", self.name))
def __eq__(self, other):
if isinstance(other, Player):
return self.name == other.name
return False
def __deepcopy__(self, memo):
newobj = Player(self.name, self, lambda *x, **y: None, {})
newobj.color = self.color
newobj.ord = self.ord
newobj.world = deepcopy(self.world, memo)
return newobj