这是一个带有@fougueux 指示的示例(我没有考虑使用此文件创建数据库,我错过了正是我需要的“erd”代码)
填写数据库(dbname = "game", row id, row fen, row result)
import json
import requests
import chess
import chess.pgn
import chess.polyglot
import pymysql
pgn = open('C:/Users/pierr/OneDrive/Bureau/lichess_elite_2020-05.pgn')
result = []
while True:
game = chess.pgn.read_game(pgn)
if game is not None:
board = game.board()
for move in game.mainline_moves():
board.push(move)
result.append([board.epd(), game.headers["Result"]])
else:
break
connection = pymysql.connect(host='localhost',
user='root',
password='',
database='chess-master',
cursorclass=pymysql.cursors.DictCursor)
with connection:
with connection.cursor() as cursor:
# Create a new record
for i in result:
sql = "INSERT INTO `game` (`fen`, `result`) VALUES (%s, %s)"
cursor.execute(sql, (i[0], i[1]))
以及搜索 minmax 得分的算法(得分 = %winrate white * 1, + %winrate black * 0, + %winrate draw * 0.5),他采取了超过 100 次行动的行动
import json
import requests
import chess
import chess.pgn
import chess.polyglot
board = chess.Board()
def parcourir(board, depth, traitblanc):
scorelist = []
for move in board.legal_moves:
board.push(move)
url = 'http://localhost/chess-master/?fen='
url += board.epd() # fen
r = requests.get(url)
data = json.loads(r.text)
somme = int(data[0]['COUNT(result)']) + int(data[1]['COUNT(result)']) + int(data[2]['COUNT(result)'])
if(somme > 100):
score = (int(data[0]['COUNT(result)']) * 1) + (int(data[1]['COUNT(result)']) * 0) + (int(data[2]['COUNT(result)']) * 0.5)
scorelist.append(score)
board.pop()
if(depth != 0):
score = []
for move in board.legal_moves:
board.push(move)
score.append(parcourir(board, depth-1, not traitblanc))
board.pop()
if(traitblanc):
if(not scorelist):
return -100
return max(scorelist)
else:
if(not scorelist):
return 100
return min(scorelist)
print (parcourir(board, 1, True))
使用 php 比使用 python 更舒服,我通过以下代码选择了 fen:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "chess-master";
$doCount = array("1-0","0-1","1/2-1/2");
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$results = array();
foreach($doCount as $c) {
$sql = "SELECT COUNT(result) FROM `game` WHERE fen = '".$_GET['fen']."' and result = '".$c."'";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$results[] = $row;
#print_r($row);
}
}
echo json_encode($results)
?>
谢谢你的帮忙 :)