diff --git a/__pycache__/ball.cpython-37.pyc b/__pycache__/ball.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..11b71f198abec0ead762c7b20172c70e0be9a756 Binary files /dev/null and b/__pycache__/ball.cpython-37.pyc differ diff --git a/__pycache__/game.cpython-37.pyc b/__pycache__/game.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5b8432af0d8c5acac0f831247a37219ab050868e Binary files /dev/null and b/__pycache__/game.cpython-37.pyc differ diff --git a/__pycache__/online_game.cpython-37.pyc b/__pycache__/online_game.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7d74433ffb4345c231cf44f2afa9be4cc30a94a1 Binary files /dev/null and b/__pycache__/online_game.cpython-37.pyc differ diff --git a/__pycache__/player.cpython-37.pyc b/__pycache__/player.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1f9ad5ed24cfe02864f5c9666417d01e2f757f0a Binary files /dev/null and b/__pycache__/player.cpython-37.pyc differ diff --git a/__pycache__/team.cpython-37.pyc b/__pycache__/team.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..262ff0e70bc7906aed32675318ebda02b1e273fc Binary files /dev/null and b/__pycache__/team.cpython-37.pyc differ diff --git a/automafut_2.py b/automafut_2.py deleted file mode 100644 index d84b8a1838805a10c37dd956b685891837448849..0000000000000000000000000000000000000000 --- a/automafut_2.py +++ /dev/null @@ -1,10 +0,0 @@ -from online_game import OnlineGame - -def naive_ia(game): - return [1,1,1,1,1,1,1,1,1,1,1] - -game = OnlineGame('127.0.0.1',12345) # Server's address -game.connect() -game.set_ia(game.team,naive_ia) - -game.play() \ No newline at end of file diff --git a/game.py b/game.py index 23593ffb3dafb5d5d876ec183cac62ffbdf6db80..44dcad8e7ea42a1e98f34a5b7af09ef01b3861fe 100644 --- a/game.py +++ b/game.py @@ -33,9 +33,9 @@ class Game: def output(self,team): if team == 1: - return self.team_1.ia(self) + return self.team_1.ia.predict(self,1) if team == 2: - return self.team_2.ia(self) + return self.team_2.ia.predict(self,2) def input(self): input = [] @@ -43,8 +43,8 @@ class Game: input.append(self.team_1.players[i].get_pos_x()) input.append(self.team_1.players[i].get_pos_y()) for i in range(5): - input.append(self.team_1.players[i].get_pos_x()) - input.append(self.team_1.players[i].get_pos_y()) + input.append(self.team_2.players[i].get_pos_x()) + input.append(self.team_2.players[i].get_pos_y()) input.append(self.ball.get_pos_x()) input.append(self.ball.get_pos_y()) return input diff --git a/ias/__init__.py b/ias/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/ias/__pycache__/__init__.cpython-37.pyc b/ias/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..185d59954407db9115ed14b0a678ef231514fb4a Binary files /dev/null and b/ias/__pycache__/__init__.cpython-37.pyc differ diff --git a/ias/__pycache__/ia.cpython-37.pyc b/ias/__pycache__/ia.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3216aa425a1e9073ef7ffa850512f172c6f53510 Binary files /dev/null and b/ias/__pycache__/ia.cpython-37.pyc differ diff --git a/ias/__pycache__/ia_0000001.cpython-37.pyc b/ias/__pycache__/ia_0000001.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e02aa5a2def2e8e1079fdb021a4b0ed2bd45f74a Binary files /dev/null and b/ias/__pycache__/ia_0000001.cpython-37.pyc differ diff --git a/ias/__pycache__/ia_1900251.cpython-37.pyc b/ias/__pycache__/ia_1900251.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f59562ac17635910b207210e89fd8ccbd5c6708d Binary files /dev/null and b/ias/__pycache__/ia_1900251.cpython-37.pyc differ diff --git a/ias/ia.py b/ias/ia.py new file mode 100644 index 0000000000000000000000000000000000000000..e125213ce33fcc72bb217fbecabd9d2b3f7f2326 --- /dev/null +++ b/ias/ia.py @@ -0,0 +1,7 @@ +class IA: + def __init__(self): + self.name = '' + self.author = '' + def predict(self,game,team): + return [1,1,1,1,1,1,1,1,1,1,1] + \ No newline at end of file diff --git a/ias/ia_0000001.py b/ias/ia_0000001.py new file mode 100644 index 0000000000000000000000000000000000000000..4ac4d4909f0382937029bf319a009f3822c28495 --- /dev/null +++ b/ias/ia_0000001.py @@ -0,0 +1,28 @@ +from ias.ia import IA +from math import atan2 +class my_ia(IA): + def __init__(self): + IA.__init__(self) + self.name = 'follow the ball' + self.author = 'faruk' + + def predict(self,game,team): + l = [] + for i in range(5): + if team == 1: + player = game.team_1.players[i] + elif team == 2: + player = game.team_2.players[i] + ball = game.ball + + dx = ball.get_pos_x() - player.get_pos_x() + dy = ball.get_pos_y() - player.get_pos_y() + + ang = atan2(dy,dx) + + l.append(1) + l.append(ang) + + l.append(1) + + return l diff --git a/ias/ia_1900251.py b/ias/ia_1900251.py new file mode 100644 index 0000000000000000000000000000000000000000..758faba3fafcbf0fa4c099a8fea7fd3f94f5eafc --- /dev/null +++ b/ias/ia_1900251.py @@ -0,0 +1,19 @@ +from ias.ia import IA + +class my_ia(IA): + def __init__(self): + IA.__init__(self) + self.name = 'spectrum rl' + self.author = 'faruk' + self.radial_divisions = 360 + + def spectrum(self,game,player): + spectrum = [] + for i in range(self.radial_divisions): + ang = i/self.radial_divisions + #... + spectrum.append('ennemy') + return spectrum + + def predict(self,game,team): + return [1,1,-1,1,1,1,1,1,1,1,1] diff --git a/automafut_1.py b/online_example.py similarity index 52% rename from automafut_1.py rename to online_example.py index d84b8a1838805a10c37dd956b685891837448849..6645feae6588137ad3845e1a4ac9635c6426cb7d 100644 --- a/automafut_1.py +++ b/online_example.py @@ -1,10 +1,9 @@ from online_game import OnlineGame - -def naive_ia(game): - return [1,1,1,1,1,1,1,1,1,1,1] +from ias.ia_0000001 import my_ia game = OnlineGame('127.0.0.1',12345) # Server's address game.connect() -game.set_ia(game.team,naive_ia) +game.set_ia(game.team,my_ia()) + +game.play() -game.play() \ No newline at end of file diff --git a/online_game.py b/online_game.py index 86d7463f58cebfb289d920c6d0626f287fa991b4..1c20d468b6725c1e28a184283b5a77454a9ea49f 100644 --- a/online_game.py +++ b/online_game.py @@ -12,6 +12,7 @@ class OnlineGame(Game): self.port = port self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.team = 1 + self.connected = False def send_message(self,d): try: @@ -22,12 +23,14 @@ class OnlineGame(Game): return st except socket.error as err: print ("socket creation failed with error",err) + self.connected = False return None def connect(self): self.sock.connect((self.server_ip, self.port)) d = {'msg_type':'[GET] Connection Request'} self.team = int(self.send_message(d)) + self.connected = True print('Connected as team '+str(self.team)) def get_input(self): @@ -46,9 +49,11 @@ class OnlineGame(Game): self.send_message(d) def play(self): - while(True): + print('Playing ...') + while(self.connected): self.get_input() self.send_output() + print('Finished.') diff --git a/stadium/game.pde b/stadium/game.pde index fa7b1060e4d3dd1ddecde833972ac54a57ed3b0f..e5c6227afaf10731f080854a6ee0f2117d9691ef 100644 --- a/stadium/game.pde +++ b/stadium/game.pde @@ -4,30 +4,23 @@ class Game { Team team_2; Ball ball; Game() { - this.team_1 = new Team(); - this.team_2 = new Team(); + this.team_1 = new Team(1); + this.team_2 = new Team(2); this.ball = new Ball(); } - void keep_distance(Player player_1, boolean inverted_1, Player player_2, boolean inverted_2) { - int s1 = 1; - int s2 = 1; - if (inverted_1){ - s1 = -1; - } - if (inverted_2){ - s2 = -1; - } - float xm = s1*player_1.get_pos_x()/2.0 + s2*player_2.get_pos_x()/2.0; + void keep_distance(Player player_1, Player player_2) { + + float xm = player_1.get_pos_x()/2.0 + player_2.get_pos_x()/2.0; float ym = player_1.get_pos_y()/2.0 + player_2.get_pos_y()/2.0; - float dx = s1*player_1.get_pos_x() - xm; + float dx = player_1.get_pos_x() - xm; float dy = player_1.get_pos_y() - ym; float n = sqrt(pow(dx, 2)+pow(dy, 2)); if (n < 6) { dx *= 6/n; dy *= 6/n; - player_1.set_position(s1*(xm+dx), ym+dy); - player_2.set_position(s2*(xm-dx), ym-dy); + player_1.set_position(xm+dx, ym+dy); + player_2.set_position(xm-dx, ym-dy); } } void verify_colision() { @@ -39,7 +32,7 @@ class Game { for (int i=0; i<10; i++) { for (int j=0; j<10; j++) { if (i != j) { - keep_distance(players[i], i>=5, players[j],j>=5); + keep_distance(players[i], players[j]); } } } @@ -61,8 +54,8 @@ class Game { for (int i=0; i<5; i++) { input[2*i] = this.team_1.players[i].get_pos_x(); input[2*i+1] = this.team_1.players[i].get_pos_y(); - input[10+2*i] = this.team_1.players[i].get_pos_x(); - input[10+2*i+1] = this.team_1.players[i].get_pos_y(); + input[10+2*i] = this.team_2.players[i].get_pos_x(); + input[10+2*i+1] = this.team_2.players[i].get_pos_y(); } input[20] = this.ball.get_pos_x(); input[21] = this.ball.get_pos_y(); @@ -76,10 +69,7 @@ class Game { } void show() { this.team_1.show(); - pushMatrix(); - scale(-1, 1); this.team_2.show(); - popMatrix(); this.ball.show(); } } diff --git a/stadium/player.pde b/stadium/player.pde index 571e6aefca78ee1a333f3c67aea47835c8fe9079..88c9cbc1896d409f80589586b85f5cf3a578b5f8 100644 --- a/stadium/player.pde +++ b/stadium/player.pde @@ -29,6 +29,9 @@ class Player { this.pos_x = pos_x; this.pos_y = pos_y; } + void set_angle(float angle){ + this.angle = angle; + } float get_pos_x() { return this.pos_x; } diff --git a/stadium/team.pde b/stadium/team.pde index 5d4ed7140f81fb69985b2a11790679c5d11e8f0f..5d7cea53d946d902ed5fe5a92e57988ffc34e95d 100644 --- a/stadium/team.pde +++ b/stadium/team.pde @@ -1,14 +1,17 @@ class Team { - + + int team = 1; Player[] players = new Player[5]; int score = 0; + - Team() { + Team(int team) { + this.team = team; this.players[0] = new Player(true); for (int i = 1; i<5; i++) { this.players[i] = new Player(false); } - this.initial_position(); + this.initialize_players(); this.set_color(color(random(255), random(255), random(255))); } void refresh_team(float[] output) { @@ -17,12 +20,21 @@ class Team { this.players[i].set_direction(output[i*2+1]); } } - void initial_position() { - this.players[0].set_position(-80, 0); - this.players[1].set_position(-50, -30); - this.players[2].set_position(-50, 30); - this.players[3].set_position(-20, -20); - this.players[4].set_position(-20, 20); + void initialize_players() { + int s = 1; + if(this.team == 2){ + s = -1; + } + this.players[0].set_position(-80*s, 0); + this.players[0].set_angle((PI-(s*PI))/2.0); + this.players[1].set_position(-50*s, -30); + this.players[1].set_angle((PI-(s*PI))/2.0); + this.players[2].set_position(-50*s, 30); + this.players[2].set_angle((PI-(s*PI))/2.0); + this.players[3].set_position(-20*s, -20); + this.players[3].set_angle((PI-(s*PI))/2.0); + this.players[4].set_position(-20*s, 20); + this.players[4].set_angle((PI-(s*PI))/2.0); } int insert_names(String[] names) { for (int i = 0; i<5; i++) { diff --git a/team.py b/team.py index d6e795cfa49a9d6603bed9ef818db4323585269c..c0651075c2da7f4e2e08adcd28e7ef2f910b8f3b 100644 --- a/team.py +++ b/team.py @@ -1,9 +1,11 @@ from player import Player +from ias.ia import IA + class Team: def __init__(self): self.players = [] - self.ia = lambda x: None + self.ia = IA() self.players.append(Player(True)) for i in range(1,5): self.players.append(Player(False))