-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomoku_ai_marius_tng_webclient.py
executable file
·79 lines (64 loc) · 2.7 KB
/
gomoku_ai_marius_tng_webclient.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
# by Marius Versteegen, 2022
# import time
import requests
import gomoku
# heuristicalmontecarloplayer_webClient calls the web-based webserver
# via a python flask application.
class gomoku_ai_marius_tng_webclient:
def __init__(self, black_=True, winningSeries_=5, boardSize_=int(gomoku.SIZE)):
self.black = black_
self.winningSeries = winningSeries_
self.boardSize = boardSize_
def new_game(self, black_):
self.black = black_
def move(self, gamestate, last_move, max_time_to_move=1000):
max_webaccess_plus_reply_time_ms = (
600 # .. I hope the ping will nog exceed 0.6s ..
)
# So my server has less time because of the send request en receive response
# back and forth to Denver, Colorado.
max_time_for_server_script = max_time_to_move - max_webaccess_plus_reply_time_ms
# fill a dic with info to post.
dic = {}
dic["board"] = self.convertToList(
gamestate[0]
) # lists can be json serialised, opposed to numpy arrays,therefore convert first.
dic["ply"] = gamestate[1]
dic["last_move"] = self.convertToIntTuple(
last_move
) # (int8,int8) cannot properly be json serialised
dic["max_time_to_move"] = max_time_for_server_script
dic["winningSeries"] = self.winningSeries
dic["boardSize"] = self.boardSize
dic["black"] = self.black
# measure the time spent
# start_time_ns = time.time_ns()
# call the server using POST.
url = "https://themave.pythonanywhere.com/make_gomoku_move/ai_marius_tng"
req = requests.post(url, json=dic)
# print(req.json())
# time_spent_ns = time.time_ns()-start_time_ns
# print(time_spent_ns)
# json kent geen tuples. Die maakt er arrays van. Dus zelf even converteren naar een tuple.
return tuple(req.json()["move"])
def id(self):
return "Marius TNG"
def convertToIntTuple(self, tup):
if tup == None or tup == ():
return None
else:
return (int(tup[0]), int(tup[1]))
def convertToList(self, board):
if type(board) == type([]):
return board # no conversion needed
else: # it must be a numpy array. Convert it to list:
boardAsList = []
for row in board:
lstRow = []
for number in row:
# because e.g. int8 numpy types cannot be json serialised.
lstRow.append(
int(number)
) # because e.g. int8 numpy types cannot be json serialised.
boardAsList.append(lstRow)
return boardAsList