-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess_api.py
More file actions
59 lines (53 loc) · 1.79 KB
/
chess_api.py
File metadata and controls
59 lines (53 loc) · 1.79 KB
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
import csv
from flask import Flask, jsonify, request, abort
from flask_cors import *
from Search.abSearch import ABSearch
from Search.State import State
import copy
app = Flask(__name__)
move_red = []
move_black = []
CORS(app, supports_credentials=True)
abSearch = ABSearch()
with open('chessred_wc2.csv') as f: # 移动黑子
reader = csv.reader(f)
for i in reader:
move_black.append(i)
@app.route('/suggest/<string:state64>/<int:flag>')
def login(state64: str = None, flag: int = None):
if flag == 1:
move = getMoveForBlackByWordsCount(state64)
if move is not None:
return jsonify([{
'move': move
}])
move = getMoveForBlackBySearch(state64)
return jsonify([{
'move': move
}])
else:
move = getMoveForBlackFirstBySearch(state64)
return jsonify([{
'move': move
}])
def getMoveForBlackByWordsCount(state64):
for x in move_black:
if x[0] == state64:
print("words count result:" + x[1])
return x[1]
return None
def getMoveForBlackBySearch(state64):
stateForRed = State("Black", "Red")
stateForRed.updateBoardState(state64)
board = copy.deepcopy(stateForRed.board)
searchResult = abSearch.dfsSearch(stateForRed, 2, float("inf"), float("-inf"))
print("search result:" + str(searchResult))
return abSearch.getMoveForApi(board, searchResult)
def getMoveForBlackFirstBySearch(state64):
stateForRed = State("Black", "Red")
stateForRed.updateBoardState(state64)
board = copy.deepcopy(stateForRed.board)
searchResult = abSearch.dfsSearch(stateForRed, 2, float("-inf"), float("inf"))
print("search result:" + str(searchResult))
return abSearch.getMoveForApi(board, searchResult)
app.run(debug=True)