-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountries.py
105 lines (88 loc) · 3.29 KB
/
countries.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
# Country data from
# https://github.com/AlexWilton/Risk-World-Domination-Game/blob/master/data/default_map.json
# 6 continents
# 83 connections among countries (all pairs as lists)
import json
import networkx as nx
with open('utils/map.json', 'r') as f:
data = json.load(f)
def load_data():
countries = dict()
for each in data['country_names'].items():
c = Country(int(each[0]), each[1])
for key in data['continents'].keys():
if int(each[0]) in data['continents'][key]:
c.continent = key
break
for conn in data['connections']:
if c.id in conn:
c.neighbors.append(conn)
c.neighbors = [j for i in c.neighbors for j in i if j != c.id]
countries[c.id] = c
return countries
class Country:
def __init__(self, _id, name):
self.id = _id
self.name = name
self.continent = list()
self.neighbors = list()
self.owner = None
self.army = 0
def __str__(self):
return f'Country {self.name} -- id no. {self.id}. Army: {self.army} is in continent {self.continent}\n' \
f'My neighbors are {self.neighbors}\n'
class World:
def __init__(self):
self.countries = load_data()
self.data = data
self.players = list()
self.turn = 0
self.net = self.generate_map()
self.on = True
self.winner = None
self.log = None
self.changed_goal = 0
def generate_map(self):
G = nx.Graph()
G.add_nodes_from(self.countries.keys())
G.add_edges_from(self.data['connections'])
return G
def distribute_countries(self):
if self.turn == 0:
i = 0
while i < len(self.countries):
for p in self.players:
p.add_country(self, self.countries[i])
i += 1
def deploy_army(self):
for p in self.players:
p.allocate_armies(self)
def play_turn(self):
# Animation won't work with while. It has to be an IF
if self.on:
if self.log:
self.log.info(f'Playing turn {self.turn}')
for p in self.players:
# Check if last player did not win with 'destroy' goal
if self.on:
p.attack(self)
# Again checking goals before rearranging
if self.on:
p.rearrange(self)
# Check Winner!
p.goal.update_goal(self.countries)
if p.goal.check_goal(self, p):
if self.log:
arms = sum([c.army for c in p.my_countries.values()])
self.log.info(f"{p.name.capitalize()} is the WINNER, with {arms} armies, "
f"goal: '{p.goal.type}' and enemy {p.goal.enemy} "
f"with strategy {p.strategy}")
self.winner = p
self.on = False
self.turn += 1
if self.turn > 200:
self.on = False
self.winner = 'Tie'
if __name__ == '__main__':
cts = load_data()
print(cts[0], cts[-1])