-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
319 lines (275 loc) · 14.8 KB
/
game.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from field import Field
from team import Team
from player import Player
from abc import ABC, abstractmethod
from typing import List, Dict, Tuple
import numpy as np
from player import Player
import action
from planning import football
from zone import Zone
import random
from planning.planning import ForwardPlan
from planning.search import breadth_first_tree_search
from planning.planning import Action
class Game(ABC) :
def __init__(self, team1 : Team, team2 : Team, field : Field , time , points : list ):
self.team1 = team1
self.team2 = team2
self.field = field
self.points = points
self.time = time
@abstractmethod
def play(self):
pass
class Football(Game):
def __init__(self, team1, team2, field, time):
super().__init__(team1 , team2 , field , time , [0,0])
def play(self):
# print("Start Game")
team_with_ball_possession = self.select_initial_team_with_ball()
player_with_ballposition = self.select_initial_player_with_ball(team_with_ball_possession)
self.initialize_attakers_positions(self.team1 , self.team2)
for i in range(self.time):
if (team_with_ball_possession == self.team1):
team_adversarie = self.team2
else:
team_adversarie = self.team1
if(i!= 0):
self.move_player_in_team_with_ballposicion(player_with_ballposition,team_with_ball_possession , team_adversarie)
actions_defenses = []
if(self.team1 == team_with_ball_possession):
actions_defenses = self.move_player_in_team_without_ballposicion(player_with_ballposition,self.team2)
else:
actions_defenses= self.move_player_in_team_without_ballposicion(player_with_ballposition,self.team1)
variables ,problem =football.football_model(team_with_ball_possession,player_with_ballposition)
sol = breadth_first_tree_search(ForwardPlan(problem))
action_of_player_with_ball, prob = self.get_best_solution(sol,variables)
results = self.choose_player_success((player_with_ballposition, action_of_player_with_ball), variables,actions_defenses, self.field.coords_to_zone)
temp_player_with_ballposition , temp_team_with_ball_possession = self.process_results(results)
if temp_player_with_ballposition != player_with_ballposition:
player_with_ballposition = temp_player_with_ballposition
if temp_team_with_ball_possession != team_with_ball_possession:
team_with_ball_possession = temp_team_with_ball_possession
if self.points[0] > self.points[1]:
return self.team1
elif self.points[0] < self.points[1]:
return self.team2
else:
return self.team1, self.team2
def process_results (self , result):
if result[1] == 'Shoot':
team_with_ball = None
team = result[0].team
if team == self.team1:
self.points[0] += 1
team_with_ball = self.team2
else:
self.points[1] += 1
team_with_ball = self.team1
self.set_players_to_original_positions(self.team1 , self.team2)
self.initialize_attakers_positions(self.team1 , self.team2)
return self.select_initial_player_with_ball(team_with_ball) , team_with_ball
if(result[1] == 'Move'):
action.Move.execute(result[0],result[2])
return result[0] , result[0].team
if(result[1] == 'Pass'):
action.Pass.execute(result[0], result[2])
return result[0] , result[0].team
if(result[1] == 'Intercept'):
action.Intercept.execute(result[0], result[2])
return result[0] , result[0].team
if(result[1] == 'Entry'):
action.Entry.execute(result[0] , result[2])
return result[0] , result[0].team
if(result[1] == 'Defend' ):
action.Tackle.execute(result[0], result[2])
return result[0], result[0].team
def get_best_solution(self , sol , variables):
best = -1
final_sol = None
for s in sol :
sol_act , sol_prec = s.solution(variables)
if(sol_prec > best):
best = sol_prec
final_sol = sol_act
return final_sol.path()[1].action, best
def move_player(self,x:int,y:int,player:Player, field:list[Zone]):
zones = {}
for i in field:
for a in range(3):
for b in range(3):
zones[(a,b)] = i
x = max(0,x)
y = max(0,y)
player.current_position = zones[(x,y)]
# TODO Refactor this function
def choose_player_success(self, player_with_ball: Tuple[Player, Action], dicc: Dict[str, Player], adversaries: List[Tuple[Player, str]], pos_to_zone: Dict[Tuple[int, int], Zone]):
all_players = [player_with_ball] + adversaries
current_player, action = player_with_ball
player_prob = current_player.attributes_score[action.name]
real_adversaries = []
positions_arr = [(0,1), (1,0), (0,-1), (-1, 0), (1,1), (-1,-1), (1,-1), (-1,1), (0,0)]
if action.name == 'Pass':
target_player = dicc[str(action.args[1])]
real_adversaries = [(p, act) for p, act in adversaries if p.current_position == target_player.current_position or p.current_position == current_player.current_position]
total = sum([x[0].attributes_score[x[1]] for x in real_adversaries]) + player_prob
adversaries_probs = []
new_total = 0
for x in real_adversaries:
adv_prov = x[0].attributes_score[x[1]]
adversaries_probs.append(adv_prov)
if len(adversaries_probs) == 0:
adversaries_probs.append(0)
new_total = player_prob + max(adversaries_probs)
succesful_player = np.random.choice([0,1], p=[player_prob / new_total, max(adversaries_probs) / new_total])
if succesful_player == 0:
succesful_player = current_player.name
else:
succesful_player = self.select_sucessful_adversary([x[0] for x in real_adversaries], adversaries_probs)
result = ()
for x in all_players:
if x[0].name == succesful_player:
if type(x[1]) == Action:
result = (target_player, x[1].name, x[0])
else:
result = (x[0], x[1], current_player)
break
return result
elif action.name == 'Shoot':
real_adversaries = [(p, act) for p, act in adversaries if p.current_position == current_player.current_position or p.role == 'G']
total = sum([x[0].attributes_score[x[1]] for x in real_adversaries]) + player_prob
adversaries_probs = []
for x in real_adversaries:
adv_prov = x[0].attributes_score[x[1]]
adversaries_probs.append(adv_prov)
if len(adversaries_probs) == 0:
adversaries_probs.append(0)
new_total = player_prob + max(adversaries_probs)
succesful_player = np.random.choice([0,1], p=[player_prob / new_total, max(adversaries_probs) / new_total])
if succesful_player == 0:
succesful_player = current_player.name
else:
succesful_player = self.select_sucessful_adversary([x[0] for x in real_adversaries], adversaries_probs)
result = ()
for x in all_players:
if x[0].name == succesful_player:
if type(x[1]) == Action:
result = (x[0], x[1].name)
else:
result = (x[0], x[1], current_player)
break
return result
elif action.name == 'Move':
current_zone = current_player.current_position
current_zone_coords = current_zone.get_coords()
possible_coords_zones = [(current_zone_coords[0]+pos[0], current_zone_coords[1]+pos[1]) for pos in positions_arr if (current_zone_coords[0]+pos[0], current_zone_coords[1]+pos[1]) in pos_to_zone]
zones_probs: Dict[Zone, Dict[Player, float]] = {}
for coords in possible_coords_zones:
possible_adversaries = [(p, act) for p, act in adversaries if p.current_position.get_coords() == coords]
total = sum([x[0].attributes_score[x[1]] for x in possible_adversaries]) + player_prob
zones_probs[pos_to_zone[coords]] = {current_player: player_prob/total}
for x in possible_adversaries:
adv_prov = x[0].attributes_score[x[1]]
zones_probs[pos_to_zone[coords]][x[0]] = adv_prov
success_arr = [zones_probs[x][current_player] for x in zones_probs]
total_success = sum(success_arr)
normalized_success_arr = [x/total_success for x in success_arr]
target_index_coords_zone = np.random.choice([i for i in range(len(possible_coords_zones))], p=normalized_success_arr)
target_coords_zone = possible_coords_zones[target_index_coords_zone]
target_zone = pos_to_zone[target_coords_zone]
target_players = zones_probs[target_zone].keys()
target_probs = []
for x in target_players:
target_probs.append(zones_probs[target_zone][x])
if len(target_probs) == 0:
target_probs.append(0)
new_total = player_prob + max(target_probs)
succesful_player = np.random.choice([0,1], p=[player_prob / new_total, max(target_probs) / new_total])
if succesful_player == 0:
succesful_player = current_player.name
else:
succesful_player = self.select_sucessful_adversary([x for x in target_players], target_probs)
result = ()
for x in all_players:
if x[0].name == succesful_player:
if type(x[1]) == Action:
result = (x[0], x[1].name, target_zone)
else:
result = (x[0], x[1], current_player)
break
return result
def select_sucessful_adversary(self, adversaries: List[Player], adv_probs: List[float]):
total = sum(adv_probs)
normalized_probs = [x/total for x in adv_probs]
# print('normalized probs:', normalized_probs)
succesful_adversary = np.random.choice([x.name for x in adversaries], p=normalized_probs)
for x in adversaries:
if x.name == succesful_adversary:
return x.name
def move_player_in_team_with_ballposicion(self, player_with_ball : Player ,team : Team , team_adversarie : Team):
dir_x = [-1,-1,-1,0,0,0,1,1,1]
dir_y = [1,0,-1,1,0,-1,1,0,-1]
for player in team.players:
if player == player_with_ball or player.role == 'G':
continue
possible_new_dir = {}
x , y = player.current_position.get_coords()
for i in range(len(dir_x)):
if(self.IsValid(x+dir_x[i],y+dir_y[i])):
count = 0
for adversaries in team_adversarie.players:
if(adversaries.current_position.row == x + dir_x[i] and adversaries.current_position.column == y + dir_y[i]):
count +=1
possible_new_dir[x + dir_x[i] , y + dir_y[i]] = count
for posicion_back in possible_new_dir:
if(posicion_back[0] < x or posicion_back[1] < y):
possible_new_dir[posicion_back] +=2
else : break
sorted_possible_new_dir = sorted(possible_new_dir.items(), key=lambda x: x[1])
pos = random.randint(0,int(len(sorted_possible_new_dir)/2))
# print(len(sorted_possible_new_dir) , 'len')
if(len(sorted_possible_new_dir) > 0):
new_zone = self.field.coords_to_zone[sorted_possible_new_dir[pos][0][0], sorted_possible_new_dir[pos][0][1]]
player.current_position = new_zone
# def move_player_in_team_with_ballposicion(self , player_with_ball : Player , team : Team):
# for player in team.players:
# if (player_with_ball.current_position.types == 'Attack' or player_with_ball.position.types == 'Midfield'):
# if(player.role == 'G'):
# continue
# if(self.IsValid(player.current_position.row + 1)):
# # player.position.row += 1
# self.move_player(player.current_position.row+1,player.current_position.column,player,self.field.field)
def move_player_in_team_without_ballposicion(self , player_with_ball : Player ,team : Team):
actions = []
for player in team.players:
if(player.role == 'G'):
actions.append((player,'Defend'))
if(player.current_position.types == player_with_ball.current_position.types):
actions.append((player,'Entry'))
elif(not player.current_position.types == player.position.types):
if(self.IsValid(player.current_position.row - 1,0)):
player.position.row -= 1
actions.append((player,'Intercept'))
return actions
def select_initial_team_with_ball(self):
return self.team1 if random.randint(0,1) == 0 else self.team2
def select_initial_player_with_ball(self, team : Team):
att = [player for player in team.players if player.role == 'F']
return random.choice(att)
def set_players_to_original_positions(self , firstTeam : Team , secondTeam : Team):
for player in firstTeam.players:
player.current_position = player.position
for player in secondTeam.players:
player.current_position = player.position
def initialize_attakers_positions(self, firstTeam: Team, secondTeam: Team):
for i in range(len(firstTeam.players)):
if firstTeam.players[i].role == 'F' and firstTeam.players[i].position.types == "Attack":
self.move_player(firstTeam.players[i].position.row-1,firstTeam.players[i].position.column,firstTeam.players[i],self.field.field)
if secondTeam.players[i].role == 'F' and secondTeam.players[i].position.types == "Attack":
self.move_player(secondTeam.players[i].position.row-1,secondTeam.players[i].position.column,secondTeam.players[i],self.field.field)
return firstTeam, secondTeam
def IsValid(self , row , column):
if(row < 3 and column < 3 and row >= 0 and column >=0):
return True
return False