浏览器里的中国象棋: HTML5 canvas, JS, python

界面程序很短。引擎不是我写的,棋力不是很强——但我写不出来,正在学GNU chess的源码。

全部文件: https://files.cnblogs.com/files/blogs/714801/ccib.zip

引擎是可以换的,如 象棋旋风官方网站--中国象棋第一AI智能引擎 (ccyclone.com) 旋风专业版.zip (41.45 MB) 

ELEEYE.EXE 87KB, BOOK.DAT 95KB ……

电脑下象棋资源微全 - Fun_with_Words - 博客园 (cnblogs.com)

# Universal Chinese Chess Protocol(UCCI)是象棋界面和引擎间的通信协议。国际象棋有UCI.
# 引擎是个.exe,它和界面通过stdin和stdout通信。
# 界面向引擎发送“指令”,引擎向界面发送“反馈”。指令和反馈以“行”为单位(以'\n'结束)。
# 别忘了刷新缓冲区,如fflush().
# 引擎有引导、空闲和思考三种状态。
#  引导状态: 界面用ucci指令让引擎进入空闲状态; 引擎输出ucciok作为初始化结束的标志。
#  空闲状态: 引擎接收思考(go)和退出(quit)指令。
#  思考状态: 引擎收到go指令后进入思考状态,输出bestmove或nobestmove。
# 界面用position指令把局面告诉引擎。如:
# ucci
#   id name ElephantEye 3.1
#   option usemillisec type check default true
#   ucciok
# position fen rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1
# go time 3
#   info depth 0 score 1473 pv b2e2
#   bestmove g3g4 ponder h7g7
from subprocess import Popen, PIPE
class EleEye(Popen):
    def __init__(m):
        wd='ElephantEye/'; Popen.__init__(m,[wd+'ELEEYE.EXE',],cwd=wd,stdin=PIPE,stdout=PIPE)
    def send(m, s): m.stdin.write(s.encode() + b'\n'); m.stdin.flush()
    def recv(m, p):
        p = p.encode(); out = b''
        while True:
            s = m.stdout.readline(); print(s.decode(), end='')
            out += s
            if s.find(p) != -1: return out
 
print('Staring engine...')
ee = EleEye()
ee.send('ucci'); print(ee.recv('ucciok').decode())
 
from http.server import *
from threading import *
import re
import urllib
class HTTPReqHandler(SimpleHTTPRequestHandler):
    def __init__(m, r, c, s): super().__init__(r, c, s, directory='www')
    def do_GET(m):
        path = m.requestline.split(' ')[1]
        if not path.startswith('/ucci'): return super().do_GET()
        param = re.split('[\?\<]', urllib.parse.unquote(path))
        m.send_response(200)
        m.send_header('Content-type', 'text/html')
        m.end_headers()
        ee.send(param[2]); print(param[2])
        if param[1] != 'none': m.wfile.write(ee.recv(param[1]))
    def do_HEAD(m): super().do_HEAD()
    def do_POST(m): super().do_POST() # ERROR: super没有do_POST(). Try Flask.
 
def httpd_thread():
    port = 8000
    svr_addr = ('', port)
    httpd = ThreadingHTTPServer(svr_addr, HTTPReqHandler)
    print('Listening at', port)
    httpd.serve_forever()
 
Thread(daemon=1, target=httpd_thread).start()
while input(): pass

HTML:

JS:

posted @   Fun_with_Words  阅读(526)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?









 和4张牌。

点击右上角即可分享
微信分享提示