forked from shiluying/dachuang-factory
-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithm.py
47 lines (40 loc) · 1.49 KB
/
algorithm.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
import websockets
import json
import asyncio
import sys
from amazons import Amazons
from mcts import MCTS
async def connect():
board = Amazons()
port = sys.argv[1]
uri = f'ws://localhost:{port}'
async with websockets.connect(uri) as socket:
player = str(await socket.recv())
tree = MCTS(player, budget=int(sys.argv[2]))
print(player)
await socket.recv()
print('start')
if player == 'B':
print('thinking')
cheese, arrow = tree.getMove(board, player)
board.fire(player, cheese, 0)
board.fire(player, arrow, 1)
tree.updateRoot(player)
await socket.send(json.dumps({'move': cheese, 'kw': 0}))
await socket.send(json.dumps({'move': arrow, 'kw': 1}))
while True:
move = await socket.recv()
move = json.loads(move)
board.fire(move['player'], move['move'], move['kw'])
move = await socket.recv()
move = json.loads(move)
board.fire(move['player'], move['move'], move['kw'])
print('thinking')
cheese, arrow = tree.getMove(board, player)
board.fire(player, cheese, 0)
board.fire(player, arrow, 1)
tree.updateRoot(player)
print(cheese, arrow)
await socket.send(json.dumps({'move': cheese, 'kw': 0}))
await socket.send(json.dumps({'move': arrow, 'kw': 1}))
asyncio.get_event_loop().run_until_complete(connect())