-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayers.py
196 lines (174 loc) · 7.69 KB
/
players.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
from collections import Counter
import networkx as nx
from numpy import random
import battle
class Player:
def __init__(self, _id):
self.id = _id
self.name = None
self.strategy = None
self.goal = None
self.my_countries = dict()
self.playing = True
self.dice = list()
def add_country(self, world, country, army=1):
self.my_countries[country.id] = country
country.owner = self
country.army = army
nx.set_node_attributes(world.net, {country.id: {'owner': self.name}})
def remove_country(self, world, country):
country.owner = None
country.army = 0
nx.set_node_attributes(world.net, {country.id: {'owner': None}})
del self.my_countries[country.id]
def num_countries(self):
return len(self.my_countries)
def full_continent(self, world):
armies = dict()
stats = self.prop_continent(world)
if 1 in stats.values():
for k, v in stats.items():
if v == 1:
armies[k] = world.data['continent_values'][k]
return armies
def prop_continent(self, world):
prop = Counter([i.continent for i in self.my_countries.values()])
return {k: prop[k] / len(world.data['continents'][k]) for k in prop.keys()}
def ordered_continent(self, world):
stats = self.prop_continent(world)
return sorted(stats, key=(lambda k: stats[k]), reverse=True)
def calculate_army(self, world):
armies = self.full_continent(world)
armies['general'] = self.num_countries() // 2
# Assign minimum of three armies per player
if armies['general'] < 3:
armies['general'] = 3
return armies
def define_priorities(self, world):
c_ids = self.my_countries.keys()
neighbors = [n for c in self.my_countries.values() for n in c.neighbors if n not in c_ids]
# Independent of strategy, seek continent completion first
priority = list()
for i in self.ordered_continent(world):
for j in world.data['continents'][i]:
if (j in neighbors) and (j not in c_ids):
priority.append(j)
# Completing the rest of the list
for each in neighbors:
if each not in priority:
priority.append(each)
# Which country to attack with which priority
tup_results = [(c.id, p) for p in priority for c in self.my_countries.values() if p in c.neighbors]
# Eliminating double targets
targets = list()
attack_priority = list()
for each in tup_results:
if each[1] not in targets:
targets.append(each[1])
attack_priority.append(each)
return attack_priority
def allocate_armies(self, world):
armies = self.calculate_army(world)
attack_priority = self.define_priorities(world)
# Allocating 'general' armies
for a in attack_priority:
if self.strategy == 'blitz':
max_territory = armies['general']
else:
max_territory = 3
while armies['general'] > 0 and max_territory > 0:
self.my_countries[a[0]].army += 1
max_territory -= 1
armies['general'] -= 1
# Allocating continent army
if len(armies.keys()) > 1:
for key in armies.keys():
if len(key) == 1 and armies[key] > 0:
if self.my_countries[a[0]] in world.data['continents'][key]:
self.my_countries[a[0]].army = armies[key]
armies[key] = 0
break
else:
for i in range(armies[key]):
# Pick a random country within the continent
c = random.choice(world.data['continents'][key])
self.my_countries[c].army += 1
armies[key] = 0
def attack(self, world):
if world.turn != 0:
self.allocate_armies(world)
attack_priority = self.define_priorities(world)
for a in attack_priority:
attacker = self.my_countries[a[0]]
if attacker.army > 1:
defender = world.countries[a[1]]
# Attack until winning or exhausting army
a, d = battle.battle(attacker, defender)
if d == 0:
temp_defender = defender.owner
defender.owner.remove_country(world, defender)
# Check number of armies to pass
if a > 4:
self.add_country(world, defender, a - 3)
attacker.army -= a - 3
else:
self.add_country(world, defender, a - 1)
attacker.army -= a - 1
# Check if loser is done
if len(temp_defender.my_countries) == 0:
temp_defender.playing = False
if world.log:
world.log.info(f'{temp_defender.name} is out of the game')
# Checking if 'destroy' objective is done immediately at every successful attack
if attacker.owner.goal.enemy == temp_defender.name:
world.winner = attacker.owner
world.on = False
if world.log:
arms = sum([c.army for c in attacker.owner.my_countries.values()])
world.log.info(f"{attacker.owner.name.capitalize()} is the WINNER, with {arms} armies, "
f"goal: '{attacker.owner.goal.type}' "
f"and enemy {attacker.owner.goal.enemy} "
f"with strategy {attacker.owner.strategy}")
return
if self.strategy == 'minimalist':
return
else:
attacker.army = a
defender.army = d
def rearrange(self, world):
if self.strategy == 'sensible':
# No rearranging of armies at end of turn
pass
else:
if self.strategy == 'blitz':
# protect hubs
min_army = 1
else:
# minimalist, protection across countries
min_army = 2
# collect extra armies
extra = 0
for key in self.my_countries.keys():
if self.my_countries[key].army > min_army:
adendum = self.my_countries[key].army
self.my_countries[key].army -= adendum - min_army
extra += adendum - min_army
priorities = self.define_priorities(world)
if self.strategy == 'minimalist':
priorities = set([priorities[i][0] for i in range(len(priorities))])
priorities = list(priorities)
else:
priorities = [priorities[i][0] for i in range(len(priorities))]
i = 0
while extra > 0:
if len(priorities) != 0:
self.my_countries[priorities[i]].army += 1
extra -= 1
i += 1
i = i % len(priorities)
else:
k = random.choice(list(self.my_countries.keys()))
self.my_countries[k].army += 1
extra -= 1
if __name__ == '__main__':
p1 = Player(0)