-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay_server.py
63 lines (48 loc) · 1.27 KB
/
play_server.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
from http import server
import socketio
import asyncio
import random
from chesscpp import bestmove
from chesspy.game import import_fen
server_host = "http://localhost:8080"
sio = socketio.AsyncClient()
player_color = ""
@sio.on("color_assignment")
async def on_color(data):
global player_color
player_color = data
print("player color is " + data)
@sio.on("board_state")
async def board_state(data):
board = import_fen(data)
if " w " in data:
current_color = "white"
else:
current_color = "black"
if current_color != player_color and player_color != "":
return
print("received board, starting ai")
en_passant = [board.en_passant_tile] if len(board.en_passant_tile) > 0 else []
move, _ = bestmove(
120.0,
10,
board.board_state,
board.to_move,
en_passant,
*board.can_castle,
board.n_reversible_halfmoves,
board.n_moves
)
print("board state" + data)
print("picked best move: " + move)
await sio.emit("move", move)
@sio.event
def connect():
print("Connected")
async def main():
await sio.connect(server_host)
await sio.emit("join", {})
print("joined game")
await sio.wait()
if __name__ == "__main__":
asyncio.run(main())