-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_API.py
207 lines (172 loc) · 6.22 KB
/
game_API.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
197
198
199
200
201
202
203
204
205
206
207
import json
import sys
class DeathEffects:
def __init__(self, jsn):
self.rock = jsn["Rock"]
self.paper = jsn["Paper"]
self.scissors = jsn["Scissors"]
self.health = jsn["Health"]
self.speed = jsn["Speed"]
class Player:
def __init__(self, player_num):
self.player_num = player_num
self.name = "Player" + str(player_num)
self.stance = "Invalid Stance"
self.health = 20
self.speed = 0
self.movement_counter = 7
self.location = 0
self.destination = 0
self.dead = False
self.rock = 1
self.paper = 1
self.scissors = 1
def update(self, jsn):
self.name = jsn["Name"]
self.stance = jsn["Stance"]
self.health = jsn["Health"]
self.speed = jsn["Speed"]
self.movement_counter = jsn["Movement Counter"]
self.location = jsn["Location"]
self.destination = jsn["Destination"]
self.dead = jsn["Dead"]
self.rock = jsn["Rock"]
self.paper = jsn["Paper"]
self.scissors = jsn["Scissors"]
class Monster:
def __init__(self): # only used to return invalid monster
self.name = "No Monster"
self.stance = "Invalid Stance"
self.health = 0
self.respawn_rate = 0
self.respawn_counter = 0
self.location = 0
self.dead = False
self.death_effects = 0
self.attack = 0
self.base_health = 0
def __init__(self, jsn):
self.name = jsn["Name"]
self.stance = jsn["Stance"]
self.health = jsn["Health"]
self.respawn_rate = 7 - jsn["Speed"]
self.respawn_counter = 0
self.location = jsn["Location"]
self.dead = False
self.death_effects = DeathEffects(jsn["Death Effects"])
self.attack = jsn["Attack"]
self.base_health = self.health
def update(self, jsn):
self.name = jsn["Name"]
self.stance = jsn["Stance"]
self.health = jsn["Health"]
self.respawn_rate = 7 - jsn["Speed"]
self.respawn_counter = jsn["Movement Counter"] - jsn["Speed"]
self.location = jsn["Location"]
self.destination = jsn["Destination"]
self.dead = jsn["Dead"]
self.attack = jsn["Attack"]
self.base_health = jsn["Base Health"]
class Node:
def __init__(self, jsn):
self.adjacents = []
def add_adjacent(self, n):
self.adjacents.append(n)
class Game:
def __init__(self, jsn):
self.turn_number = 0
self.player_num = jsn["player_id"]
map_json = json.loads(jsn["map"])
self.nodes = [Node(j) for j in map_json["Nodes"]]
for edge_json in map_json["Edges"]:
adj = edge_json["Adjacents"]
self.nodes[adj[0]].add_adjacent(adj[1])
self.nodes[adj[1]].add_adjacent(adj[0])
self.monsters = [Monster(j) for j in map_json["Monsters"]]
self.player1 = Player(1)
self.player2 = Player(2)
def update(self, jsn):
self.turn_number = jsn["turn_number"]
for unit_json in jsn["game_data"]:
if unit_json["Type"] == "Player":
if unit_json["Name"] == "Player1":
self.player1.update(unit_json)
else:
self.player2.update(unit_json)
else:
for monster in self.monsters:
if monster.location == unit_json["Location"]:
monster.update(unit_json)
break
def log(str):
if (self.player_num == 1):
std.stderr.write("Player1: " + str + "\n")
else:
std.stderr.write("Player2: " + str + "\n")
def get_duel_turn_num(self):
return 30
def get_turn_num(self):
return self.turn_number
def get_adjacent_nodes(self, node):
return self.nodes[node].adjacents
def get_all_monsters(self):
return self.monsters
def get_self(self):
if self.player_num == 1:
return self.player1
else:
return self.player2
def get_opponent(self):
if self.player_num == 1:
return self.player2
else:
return self.player1
def submit_decision(self, destination, stance):
j = dict()
j["Dest"] = destination
j["Stance"] =stance
print(json.dumps(j))
sys.stdout.flush()
def shortest_paths(self, start, end):
distance = [-1 for n in self.nodes]
parent_dict = {n : [] for n in range(len(self.nodes))}
explored = [False for n in self.nodes]
to_visit = [start]
distance[start] = 0
while not explored[end]:
n = to_visit.pop(0)
for adj in self.nodes[n].adjacents:
if not explored[adj]:
if distance[adj] == -1:
distance[adj] = distance[n] + 1
parent_dict[adj].append(n)
to_visit.append(adj)
elif distance[adj] == distance[n] + 1:
parent_dict[adj].append(n)
explored[n] = True
paths = [[end]]
reached_start = (end == start)
while not reached_start:
new_paths = []
for path in paths:
for parent in parent_dict[path[0]]:
if parent == start:
reached_start = True
else:
new_path = [n for n in path]
new_path.insert(0, parent)
new_paths.insert(0, new_path)
if not reached_start:
paths = new_paths
return paths
def has_monster(self, node):
for monster in self.monsters:
if monster.location == node:
return True
return False
def get_monster(self, node):
for monster in self.monsters:
if monster.location == node:
return monster
return Monster()
#def nearest_monsters(self, node, search_mode):