首页 > 其他分享 >pikafish协议

pikafish协议

时间:2023-12-10 22:11:25浏览次数:36  
标签:协议 pikafish ... else sz lp printf nLen

/* 
Pikafish Proxy - a Chinese Chess Engine Wrapper for XQWizard to run Pikafish
Designed by Morning Yellow, Version: 2023-03-05, Last Modified: Jun. 2023
Copyright (C) 2023 www.xqbase.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdio.h>
#include <string.h>
#include "version.h"
#include "../base/pipe.h"
#include "../base/base.h"
#include "../base/base2.h"
#include "../base/rc4prng.h"
#include "../eleeye/pregen.h"
#include "../eleeye/position.h"
#include "../eleeye/book.h"

int main(void) {
  bool bUseBook;
  int i, nLen, mv, vl;
  uint32_t dwMoveStr;
  char *lp, *lpTime, *lp2;
  char sz[LINE_INPUT_MAX_CHAR], sz2[LINE_INPUT_MAX_CHAR];
  char szTime[LINE_INPUT_MAX_CHAR], szInc[LINE_INPUT_MAX_CHAR], szBookFile[LINE_INPUT_MAX_CHAR];
  BookStruct mvsBook[MAX_GEN_MOVES];
  RC4Struct rc4;
  PipeStruct pipeConsole, pipeEngine;
  PositionStruct pos;

  bUseBook = true;
  LocatePath(sz, "pikafish-modern.exe");
  LocatePath(szBookFile, "BOOK.DAT");
  pipeConsole.Open();
  pipeEngine.Open(sz);
  PreGenInit();
  rc4.InitRand();
  pos.FromFen(cszStartFen);

  while (pipeEngine.nEof == 0) {

    // Read Pikafish's Response
    while (pipeEngine.LineInput(sz)) {
      // printf("info string ENG->GUI: [%s]\n", sz);
      if (false) {

        // "Pikafish ...": Ignore
      } else if (strncmp(sz, "Pikafish ", 9) == 0) {

        // "id ...", "option ...", "uciok": Ignore
      } else if (strncmp(sz, "id ", 3) == 0) {
      } else if (strncmp(sz, "option ", 7) == 0) {
      } else if (strcmp(sz, "uciok") == 0) {

        // "bestmove (none)" -> "nobestmove"
      } else if (strcmp(sz, "bestmove (none)") == 0) {
        printf("nobestmove\n");

        // "info depth ... score cp ..." -> "info depth ... score ..."
      } else if (strncmp(sz, "info depth ", 11) == 0) {
        lp = strstr(sz, " score cp ");
        if (lp != NULL) {
          strcpy(sz2, lp + 10);
          strcpy(lp + 7, sz2);
        }
        printf("%s\n", sz);

        // else: unchanged
      } else {
        printf("%s\n", sz);
      }
      fflush(stdout);
    }

    // Read XQWizard's Request
    while (pipeConsole.LineInput(sz)) {
      if (false) {

        // "ucci" -> "uci", show version and options
      } else if (strcmp(sz, "ucci") == 0) {

        printf("id name Pikafish\n");
        printf("id version " PIKAFISH_VERSION "\n");
        printf("option usemillisec type check default true\n");
        printf("option usebook type check default true\n");
        printf("option bookfiles type string default %s\n", szBookFile);
        printf("option hashsize type spin default 16 min 1 max 33554432\n");
        printf("option threads type spin default 1 min 1 max 1024\n");
        printf("ucciok\n");
        strcpy(sz, "uci");

        // "setoption ...": only accepts "usebook", "bookfiles", "hashsize" and "threads"
      } else if (strncmp(sz, "setoption ", 10) == 0) {

        if (false) {

          // "setoption usebook true/false"
        } else if (strncmp(sz, "setoption usebook ", 18) == 0) {
          lp = sz + 18;
          bUseBook = (strcmp(lp, "true") == 0 || strcmp(lp, "on") == 0);
          continue;

          // "setoption bookfiles ..."
        } else if (strncmp(sz, "setoption bookfiles ", 20) == 0) {
          lp = sz + 20;
          if (AbsolutePath(lp)) {
            strcpy(szBookFile, lp);
          } else {
            LocatePath(szBookFile, lp);
          }
          continue;

          // "setoption hashsize ..." -> "setoption name Hash value ..."
        } else if (strncmp(sz, "setoption hashsize ", 19) == 0) {
          strcpy(sz2, "setoption name Hash value ");
          strcat(sz2, sz + 19);
          strcpy(sz, sz2);

          // "setoption threads ..." -> "setoption name Threads value ..."
        } else if (strncmp(sz, "setoption threads ", 18) == 0) {
          sscanf(sz + 18, "%d", &i);
          sprintf(sz, "setoption name Threads value %d", i < 1 ? 1 : i);
        } else {

          // else: unchanged
          continue;
        }

        // "position ...": parse position for book search
      } else if (strncmp(sz, "position ", 9) == 0) {

        lp = strstr(sz, " fen ");
        if (lp == NULL) {
          pos.FromFen(cszStartFen);
        } else {
          pos.FromFen(lp + 5);
        }
        lp = strstr(sz, " moves ");
        if (lp != NULL) {
          lp += 7;
          nLen = (strlen(lp) + 1) / 5;
          for (i = 0; i < nLen; i ++) {
            mv = COORD_MOVE(*(uint32_t *) lp);
            lp += 5;
            if (mv == 0) {
              break;
            }
            if (pos.ucpcSquares[SRC(mv)] == 0) {
              break;
            }
            pos.MakeMove(mv);
            if (pos.LastMove().CptDrw > 0) {
              pos.SetIrrev();
            }
          }
        }

        // "go ...": search book first, then call engine
      } else if (strncmp(sz, "go ", 3) == 0) {

        // search book
        if (bUseBook) {
          // a. get all moves for this position
          nLen = GetBookMoves(pos, szBookFile, mvsBook);

          if (nLen > 0) {
            vl = 0;
            for (i = 0; i < nLen; i ++) {
              vl += mvsBook[i].wvl;
              dwMoveStr = MOVE_COORD(mvsBook[i].wmv);
              printf("info depth 0 score %d pv %.4s\n", mvsBook[i].wvl, (const char *) &dwMoveStr);
              fflush(stdout);
            }

            // b. pick a random move by move weight
            vl = rc4.NextLong() % (uint32_t) vl;
            for (i = 0; i < nLen; i ++) {
              vl -= mvsBook[i].wvl;
              if (vl < 0) {
                break;
              }
            }

            // c. skip the move that causes repetition
            pos.MakeMove(mvsBook[i].wmv);
            if (pos.RepStatus(3) == 0) {
              dwMoveStr = MOVE_COORD(mvsBook[i].wmv);
              printf("bestmove %.4s", (const char *) &dwMoveStr);
              // d. get ponder move (next move with max weight)
              nLen = GetBookMoves(pos, szBookFile, mvsBook);
              pos.UndoMakeMove();
              if (nLen > 0) {
                dwMoveStr = MOVE_COORD(mvsBook[0].wmv);
                printf(" ponder %.4s", (const char *) &dwMoveStr);
              }
              printf("\n");
              fflush(stdout);
              continue;
            }
            pos.UndoMakeMove();
          }
        }

        // "go time ..." -> "go wtime ... btime ..."
        lp = strstr(sz, " time ");
        if (lp != NULL) {
          lpTime = lp + 6;
          // copy text after "time"
          lp2 = strchr(lpTime, ' ');
          if (lp2 == NULL) {
            strcpy(szTime, lpTime + 6);
          } else {
            nLen = lp2 - lpTime;
            strncpy(szTime, lpTime, nLen);
            szTime[nLen] = '\0';
          }
          // copy text after "increment"
          lp2 = strstr(sz, " increment ");
          if (lp2 == NULL) {
            // copy text after "movestogo"
            lp2 = strstr(sz, " movestogo ");
            if (lp2 == NULL) {
              sprintf(lp, " wtime %s btime %s", szTime, szTime);
            } else {
              lpTime = lp2 + 11;
              lp2 = strchr(lpTime, ' ');
              if (lp2 == NULL) {
                strcpy(szInc, lpTime);
              } else {
                nLen = lp2 - lpTime;
                strncpy(szInc, lpTime, nLen);
                szInc[nLen] = '\0';
              }
              sprintf(lp, " wtime %s btime %s movestogo %s", szTime, szTime, szInc);
            }
          } else {
            lpTime = lp2 + 11;
            lp2 = strchr(lpTime, ' ');
            if (lp2 == NULL) {
              strcpy(szInc, lpTime);
            } else {
              nLen = lp2 - lpTime;
              strncpy(szInc, lpTime, nLen);
              szInc[nLen] = '\0';
            }
            sprintf(lp, " wtime %s btime %s winc %s binc %s", szTime, szTime, szInc, szInc);
          }
        }
        // other options such as "go depth ...": unchanged
      }
      pipeEngine.LineOutput(sz);
      // printf("info string GUI->ENG: [%s]\n", sz);
      fflush(stdout);
    }
    Idle();
  }
  pipeConsole.Close();
  pipeEngine.Close();
  return 0;
}

 

标签:协议,pikafish,...,else,sz,lp,printf,nLen
From: https://www.cnblogs.com/ryueifu-VBA/p/17893348.html

相关文章

  • minipcie接口走usb协议
    由于MiniPCIe接口和USB协议之间的转换涉及到硬件设备,因此无法直接通过编写代码来实现。您需要购买一个PCIe到USB适配器并将其连接到计算机上。安装完成后,您可以像使用其他USB设备一样使用它。MiniPCIe接口是一种小型化的PCIe接口,用于连接各种扩展卡。要将MiniPCIe接口转换为USB......
  • 基于XML的SOAP协议
    基于XML的最著名的通信协议就是SOAP了,全称简单对象访问协议(SimpleObjectAccessProtocol)。它使用XML编写简单的请求和回复消息,并用HTTP协议进行传输。SOAP将请求和回复放在一个信封里面,就像传递一个邮件一样。信封里面的信分抬头和正文。POST/purchaseOrderHTTP/1.1H......
  • 认识HTTP协议与apache
    万维网:(www)并非计算机网络,而是一个大型的数据库,可以实现网页与网页之间的跳转url:资源定位符描述了一个资源在服务器上的具体位置http:超文本传输协议图片视频小程序http:HyperTextTransferProtocol应用层协议,默认端口:80/tcp可以使用http协议的软件apachenginx......
  • robots.txt禁止收录协议写法
    1. 什么是robots.txt?robots.txt 是网站和搜索引擎的协议的纯文本文件。当一个搜索引擎蜘蛛来访问站点时,它首先爬行来检查该站点根目录下是否存在robots.txt,如果存在,根据文件内容来确定访问范围,如果没有,蜘蛛就沿着链接抓取。robots.txt 放在项目的根目录下。2. robots.txt......
  • 1.理论、算法、协议
    1.CAP理论CAP也就是Consistency(一致性)、Availability(可用性)、PartitionTolerance(分区容错性)这三个单词首字母组合。在理论计算机科学中,CAP定理(CAPtheorem)指出对于一个分布式系统来说,当设计读写操作时,只能同时满足以下三点中的两个:一致性(Consistency):所有节点访问......
  • AMQP协议中的,消息队列RabbitMQ,ActiveMQ,Apache Kafka区别是什么?
    都是基于AMQP协议来的一种实现方式。参考chatGPT4回答请使用Markdown表格来展示RabbitMQ、ActiveMQ和ApacheKafka之间的区别:维度RabbitMQActiveMQApacheKafka语言ErlangJavaScala/Java协议AMQP、STOMP、MQTTAMQP、STOMP、OpenWire自定义协议......
  • 网络层协议及ip编址
    网络层协议及ip编址IP协议报文字段长度/位含义Version版本号4指定IP协议的版本;通信双方使用的IP协议版本必须一致;目前使用最广泛的IP协议版本号为4,即IPv4。现在已经开始普及IPv6了IHL首部长度4可表示的最大十进制数值是15,IP首部长度......
  • 解密 ArcGraph 分布式一致性:Raft 协议与分布式事务实现丨技术专栏
    导读:本文提出了一种将事务日志和Raft日志融合在一起的机制,从而实现了分布式事务和数据一致性的场景。01背景介绍分布式系统是伴随着互联网的高速发展而出现的。其出现为了应对单机系统无法解决的高并发、高可用性、容错性等问题。分布式系统将传统的系统扩容模式,从scaleup......
  • 软件测试/人工智能|HTTPS加密协议,你会多少?
    什么是HTTPS?HTTPS是超文本传输协议(HTTP)的安全版本。它通过使用安全套接层协议(SSL)或传输层安全协议(TLS)来加密通信内容,确保数据在客户端和服务器之间传输时得到保护。这种加密机制防止了黑客或恶意用户窃取、篡改或窥视传输的数据。本文将详细介绍HTTPS的加密过程及其工作原理。HTT......
  • Socks VS HTTP 谁才是最快的代理协议
    前言在网络传输中,代理协议扮演着非常重要的角色。Socks协议和HTTP协议是两种常见的代理协议,在网络上使用非常广泛。这两个协议各有优缺点,但是常有人关心这两个协议的速度究竟如何,哪一个更快。在本文中,我们将对Socks和HTTP两个代理协议进行分析比较,并最终得出哪一个更快的结论。一......