-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomoku_ai_random_webserver.py
executable file
·116 lines (85 loc) · 3.15 KB
/
gomoku_ai_random_webserver.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# a flask webserver that encapsulates a gomoku ai (as example a simple random AI)
from flask import Flask, request, json, Response
from bson import json_util
import logging
import random, time
logging.basicConfig(filename="mylog.log")
app = Flask(__name__)
@app.route("/make_gomoku_move/ai_random", methods=["POST"])
def make_gomoku_move_9g3():
# IncrementalStringDecode
data = request.json
ar_error = []
if data is None or data == {}:
ar_error.append("data missing")
strAllErrors = ""
for strError in ar_error:
strAllErrors += strError
return Response(
response=json.dumps({"Error": strError}),
status=400,
mimetype="application/json",
)
gomoku_ai = gomoku_random_ai_webServer()
move = gomoku_ai.move(data)
# dicResponse,ar_error = temptest(data)
# if(len(ar_error)!=0): return MongoAPI.returnErrors(ar_error)
dicResponse = {}
dicResponse["move"] = move
return Response(
response=json_util.dumps(dicResponse), status=200, mimetype="application/json"
)
# ******************************************************
# End of Flask part. Below is the AI part.
# ******************************************************
class GmGameRules:
winningSeries = 5 # global class variable. will be overridden below.
BOARDWIDTH = 19
BOARDHEIGHT = 19
def isValidMove(board, column, row):
# Returns True if there is an empty space in the given column.
# Otherwise returns False.
return (
(column >= 0)
and (column < len(board))
and (row >= 0)
and (row < len(board[0]))
and (board[column][row] == 0)
)
def getRandomMove(board):
# let's make a random move
# First, make a list of all empty spots
validMoves = []
for col in range(GmGameRules.BOARDWIDTH):
for row in range(GmGameRules.BOARDHEIGHT):
if isValidMove(board, col, row):
validMoves.append((col, row))
return random.choice(validMoves)
# player gives an implementation the basePlayer cl
class randomPlayer:
def __init__(self, black_=True):
self.black = black_
self.max_move_time_ns = 0
self.start_time_ns = 0
def new_game(self, black_):
self.black = black_
def move(self, gamestate, last_move, max_time_to_move=1000):
board = gamestate[0]
# ply=gamesate[1]
self.max_move_time_ns = 0.95 * max_time_to_move * 1000000 # ms to ns
self.start_time_ns = time.time_ns()
return getRandomMove(board)
def id(self):
return "Marius"
class gomoku_random_ai_webServer:
def move(self, dic):
# strData=strUrlEncodedData # urllib.parse.unquote(strUrlEncodedData)
# dic=json.loads(strData)
GmGameRules.winningSeries = dic["winningSeries"]
GmGameRules.BOARDWIDTH = dic["boardSize"]
GmGameRules.BOARDHEIGHT = dic["boardSize"]
gamestate = (dic["board"], dic["ply"])
last_move = dic["last_move"]
# we'll deriver valid_moves ourselves
player = randomPlayer(dic["black"])
return player.move(gamestate, last_move, dic["max_time_to_move"])