定制 p-chess/chess 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

p-chess/chess

Composer 安装命令:

composer require p-chess/chess

包简介

A PHP chess library that is used for chess move generation/validation, piece placement/movement, and check/checkmate/stalemate detection

README 文档

README

Chess is a PHP chess library used for chess move generation/validation, piece placement/movement, and check/checkmate/stalemate detection - basically everything but the AI.

NOTE: this started as a port of chess.js for PHP, forked from ryanhs/chess.php

Latest Stable Version MIT License
Build Status

Installation

use composer with composer require p-chess/chess or put in your composer.json

"require": {
    "p-chess/chess": "^1.0"
}

Local development

This project is designed to run inside Docker via docker compose, with helper targets in Makefile.

Prerequisites

  • Docker + Docker Compose
  • GNU Make

First-time setup

make build
make start
make install

Common development commands

make test
make stan
make cs
make coverage
make stop

You can list all available targets with:

make help

Example Code

The code below plays a complete game of chess... randomly.

<?php

require 'vendor/autoload.php';
use \PChess\Chess\Chess;
use \PChess\Chess\Output\UnicodeOutput;

$chess = new Chess();
while (!$chess->gameOver()) {
    $moves = $chess->moves();
    $move = $moves[random_int(0, count($moves) - 1)];
    $chess->move($move->san);
}

echo (new UnicodeOutput())->render($chess) . PHP_EOL;
   +---+---+---+---+---+---+---+---+
 8 |   | ♜ | ♘ |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 7 | ♞ |   |   |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 6 |   |   |   |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 5 |   |   |   |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 4 |   |   |   |   |   | ♚ | ♟ |   | 
   +---+---+---+---+---+---+---+---+
 3 | ♜ |   |   |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 2 |   |   |   |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 1 | ♔ |   |   |   | ♞ |   |   |   | 
   +---+---+---+---+---+---+---+---+
     a   b   c   d   e   f   g   h

Supported output formats

ASCII

Pieces are displayed with corresponding codes (e.g. "p" for pawn, "q" for queen, etc.).

<?php
// use...
$chess = new Chess();
echo (new AsciiOutput())->render($chess);

Unicode

Pieces are displayed like in the example above.

<?php
// use...
$chess = new Chess();
echo (new UnicodeOutput())->render($chess);

PNG Image

Pieces are displayed inside a png image.

<?php
// use...
$chess = new Chess();
$imagine = new \Imagine\Gd\Imagine();   // or \Imagine\Imagick\Imagine()
$output = new ImageOutput($imagine, '/your/path/to/images', 480);
header('Content-Type: image/png');  
echo $output->render($chess);

See dedicated documentation for detailed instructions.

SVG Image

Pieces are displayed inside an SVG image.

<?php
// use...
$chess = new Chess();
echo (new SvgOutput())->render($chess);

HTML

Pieces are displayed inside an HTML table.

See dedicated documentation for detailed instructions.

Performance

There is still a lot to do in this topic.
akondas/php-grandmaster is a good place to start experiment ;)

Chess::move()

iteration mean comment
1 548.819μs initial
2 447.973μs replace fen with json_encode in history position (inThreefoldRepetition cache)
3 340.375μs replace fen with json_encode in generateMoves
4 333.145μs add boardHash calculation on make/undo move
5 25.917μs 🔥 add cache for moveToSAN method

Other documentation

All classes are documented in the docs directory.

p-chess/chess 适用场景与选型建议

p-chess/chess 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6k 次下载、GitHub Stars 达 47, 最近一次更新时间为 2020 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 p-chess/chess 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 p-chess/chess 我们能提供哪些服务?
定制开发 / 二次开发

基于 p-chess/chess 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 47
  • 点击次数: 32
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 47
  • Watchers: 5
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-14