-
Notifications
You must be signed in to change notification settings - Fork 0
/
totito_client.py
68 lines (60 loc) · 2.16 KB
/
totito_client.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
import socketio
import argparse
from minimax import *
from totitoChino import *
## Argument list
par=argparse.ArgumentParser(description='This is an AI that plays dots and boxes using the Minimax algorithm using alpha-beta puring an k-look ahead.')
par.add_argument('--ip','-i',dest='ip',type=str,help='IP adress of the tournament host.',required=True)
par.add_argument('--port','-p',dest='port',type=str,help='Listening port of the tournament host',required=True)
par.add_argument('--tournament','-t',dest='tournament',type=int,help='Tournament ID',required=True)
par.add_argument('--user','-u',dest='user',type=str,help='Username for this AI',required=True)
args=par.parse_args()
HOST = "http://"+args.ip+":"+args.port
USERNAME = args.user
tournamentID = args.tournament
print('Connecting to: '+HOST)
print('With username: '+USERNAME)
print('To the tournament: ', tournamentID)
sio = socketio.Client()
sio.connect(HOST)
# On connect -> signin
@sio.on('connect')
def on_connect():
sio.emit('signin', {
'user_name': USERNAME,
'tournament_id': tournamentID,
'user_role': "player"
})
print("Conectado: "+ USERNAME)
# On ready -> calculate move -> play
@sio.on('ready')
def on_ready(data):
if data["player_turn_id"] == 1:
player1 = True
is_max = True
else:
player1 = False
is_max = False
totito = TotitoChino(data['board'])
movement = minimax_ab(totito,is_max=is_max,player=player1)[0]
# movement = totito.dumb_move
sio.emit('play',{
'player_turn_id': data["player_turn_id"],
'tournament_id': tournamentID,
'game_id': data["game_id"],
'movement': movement
})
@sio.on('finish')
def on_finish(data):
print('Game ',data["game_id"],' has finished')
if data['winner_turn_id'] == data['player_turn_id']:
print('YOU WIN!')
else:
print('YOU LOSE')
print('Player id:',data['player_turn_id'])
print('Ready to play again')
sio.emit('player_ready', {
'tournament_id': tournamentID,
'game_id': data["game_id"],
'player_turn_id': data["player_turn_id"]
})