alcalyn/awale

Awale游戏PHP实现

1.0.0 2016-01-16 12:19 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:39:53 UTC


README

此库提供了Awale(或Oware)游戏的PHP实现。

安装

下载

使用Composer

{
    "require": {
        "alcalyn/awale": "1.0.x"
    }
}

更新您的composer。

composer update

未使用Composer? 直接安装

用法

创建实例

创建一个Awale实例,它是包含种子的Awale游戏状态的实例。

use Alcalyn\Awale\Awale;

$awale = new Awale();

// Players start with 4 seeds in each container.
$awale->setSeedsPerContainer(4);

// The first player starts.
$awale->setCurrentPlayer(Awale::PLAYER_0);

// Needs to explicitly init the grid (containers seeds number, and attics).
$awale->initGrid();

或者更简洁地

use Alcalyn\Awale\Awale;

$awale = new Awale::createWithSeedsPerContainer(4);

网格

Awale网格代表容器和阁楼。

// Retrieve the grid array from the Awale instance
$grid = $awale->getGrid();

print_r($grid);

/* Outputs:
Array
(
    [0] => Array                // Player 0, or player top.
        (
            [seeds] => Array
                (
                    [0] => 4    // Player 0 seeds, he has 4 seeds in each containers.
                    [1] => 4    // The first container is the top left container.
                    [2] => 4
                    [3] => 4
                    [4] => 4
                    [5] => 4    // The top right container.
                )

            [attic] => 0        // Player 0 has no seeds in his attic.
        )

    [1] => Array                // Player 1, or player bottom.
        (
            [seeds] => Array
                (
                    [0] => 4    // The bottom left container.
                    [1] => 4
                    [2] => 4
                    [3] => 4
                    [4] => 4
                    [5] => 4    // The bottom right container.
                )

            [attic] => 0        // No seeds in player 1 attic.
        )

)
*/

或者获取图形表示

echo $awale;

/* Outputs:

Awale game instance.
     4  4  4  4  4  4
  0                    0
     4  4  4  4  4  4
seeds per container: 4
current player: PLAYER_0
last move: null

*/

执行移动

一旦您有一个游戏实例,您就可以执行移动。

// Top player plays his third container (from left)
$awale->play(Awale::PLAYER_0, 2);

// Bottom player plays his first container (from left)
$awale->play(Awale::PLAYER_1, 0);

play 方法在无效移动时抛出 Alcalyn\Awale\Exception\AwaleException

游戏实用检查

// Last played move
// An array with keys 'players' and 'move', example: {player:1, move:5}, Player 1 played his 5th container
$awale->getLastMove();

// Player's turn to play
$awale->getCurrentPlayer(); // Awale::PLAYER_0 or Awale::PLAYER_1

// Get amount of seeds needed to exceed, depending on seedsPerContainer
$awale->getSeedsNeededToWin(); // 24 if seedsPerContainer = 4

// Check is game is over (a player has more than 24 seeds, or game is looping, or player cannot feeds his opponent)
$awale->isGameOver(); // true or false

// Get winner when game is finished
$awale->getWinner(); // Awale::PLAYER_0 or Awale::PLAYER_1 or Awale::DRAW or null

// Get seeds number in a player attic
$awale->getScore(Awale::PLAYER_1);

// Whether a loop is detected (a same state of the game will appear again and again)
$awale->isGameLooping();

Awale类中还有一些其他方法。

运行单元测试

首先,更新您的composer以获取phpunit,然后运行

vendor/bin/phpunit -c .

许可

此库受MIT许可证约束。