mano/chessknight

此包最新版本(dev-main)没有提供许可证信息。

无限棋盘上的骑士最短路径

dev-main 2023-08-06 15:51 UTC

This package is auto-updated.

Last update: 2024-09-06 18:09:20 UTC


README

作为一个编程练习的逻辑游戏。找到棋盘上骑士的最短路径。

对此问题有多种策略 - 目前只实现了一种(广度优先搜索)。逐渐构建所有可能移动的树,并在每个深度级别评估结果。

其他建议的策略以提高性能

  • 首先将棋盘划分为更小的部分
  • 最初只尝试指向最终方格的偏移量
  • 同时尝试多个深度级别(也可以更慢 - 可以与前面的点很好地结合)

安装

克隆仓库。

如果您想运行测试,请运行Composer。

php composer install

测试

目前还没有全部覆盖 - 只彻底进行了集成测试。

php vendor/bin/phpunit tests

使用

// create a board o size 8x8
$board = new Board(8);

$finder = new ShortestPathFinder(
    new Knight(), // set a piece that is used
    $board,
    new SquareArrayInterpreter(), // set how you want to get the result as
);

// the strategy to find the path
$finder->setStrategy(new TreeStrategy());

// result will depend on the interpreter, in this case it will return all the visited squares
$result = $finder->findShortestPath(
    $board->getSquareByChessNotation('A1'),
    $board->getSquareByChessNotation('B3')
);

待办事项

  • 调试解释器显示时间和内存消耗。