首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用pgn创建游戏资源管理器(就像甘草浏览器一样)

用pgn创建游戏资源管理器(就像甘草浏览器一样)
EN

Stack Overflow用户
提问于 2022-01-02 02:13:14
回答 2查看 330关注 0票数 1

目前,我正与python一起做一个失败的"ia“,这给了我一个极小的成功率(使用最成功的数据库中的大师级部分),并且只采取了至少100个位置已经玩过的动作,这样就不会在一个单一游戏中掉进100%的黑色成功率。我用lichess api成功地做到了这一点,问题是当我请求超过3个深度时,lichess会因为api上生成的垃圾邮件而阻塞我。

我告诉自己,我要做同样的事情,但我将使用一个由大师部件组成的.pgn,而不是使用lichess api,我可以用库“象棋”来做一些类似的事情,但是目前我被阻止了如何通过示例"e2-e4“过滤这个文件,并且有一个递归函数来研究这个文件。

我在国际象棋博士网站上找不到拉客,我的googles搜索也找不到任何东西。

有人有主意吗?

api:https://lichess.org/api#tag/Opening-Explorer

png文件:https://odysee.com/@Toadofsky:b/Lichess-Elite-Database:b

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-02 15:18:16

下面是一个@fougueux的例子(我没有想过用这个文件创建数据库,我错过了"erd“代码,这正是我所需要的)

填充数据库(dbname = "game“、行id、行fen、行结果)

代码语言:javascript
复制
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]))

和搜索最小最大分数的算法(分数=%的风速白* 1,+%的黑* 0,+%的风速平局* 0.5),他采取了一个移动100多个移动游戏。

代码语言:javascript
复制
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))

与python相比,我更习惯于使用php,因此我通过以下代码选择了fen:

代码语言:javascript
复制
<?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)
?>

(谢谢你的帮助:)

票数 1
EN

Stack Overflow用户

发布于 2022-01-02 03:50:35

一种方法是构建像mongodb这样的数据库。

代码语言:javascript
复制
1. Read each game in the pgn file.
2. Record the epd after every move in the game.
3. Get the result of this game.
4. Record white_win, white_loss, white_draw, black_win, black_loss, black_draw, num_game.
5. Save/update to database the following info.
{
  "epd": <epd> 
  "white_win": ...,
  "white_loss": ..., 
  "...": ...
}

你现在可以查询环保署的资料库,看看有甚麽资料。

例如,您可以使用python库解析pgn文件中的每一个游戏。

首先将其保存到数据库的优点是,当您稍后查询时,您将快速获得结果。

如果您不喜欢将它保存在数据库中,您可以这样做,但如果文件很大,则会很昂贵。您仍然可以使用上述算法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70553178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档