welhott/librockpaperscissor

一个基于剪刀石头布的 PHP 库,用于创建游戏。

v1.3 2016-11-01 18:45 UTC

This package is not auto-updated.

Last update: 2024-09-23 12:32:16 UTC


README

使用你想要的任何组合创建基于剪刀石头布规则的游戏。验证规则将确保你创建有效游戏的道路上。

这个项目对我来说主要是一个娱乐的练习。在看到 101 移动 剪刀石头布风格的游戏后,我觉得在 PHP 中重造这个游戏会很酷。然而,我决定创建一个库。也许游戏很快就会出现。

还有很多改进的空间,但我相信总的来说,它正在做它的工作。

安装/入门

有一个 Dockerfile,我使用它来开发库。我只是用它来快速设置 PHP 环境,然后将 PHPUnit 和 Composer 下载到容器中。我还安装了 SSH,这样我就可以使用 PHPStorm 进行远程调试和远程单元测试。这个 Dockerfile 缺少很多内容,包括配置 PHP 以用于开发(例如 display_errors)。这在我的 TODOs 中。

现在,只需构建容器并运行它。

docker build -t librps .
docker run -ti [-p 22] -v YOUR_PATH:CONTAINER_PATH librps

然后,当你处于容器内部时,你必须启动 ssh 守护进程,这样你就可以使用远程开发工具。

开发

当然,第一步是克隆存储库。然后你应该更新 composer 库,然后运行测试套件。

git clone https://github.com/rvelhote/librockpaperscissor.git
cd librockpaperscissor
composer update
phpunit

如果你还没有安装 Composer 或 PHPUnit,请参阅它们各自的网站以获取当前的安装说明。

使用库

创建基于剪刀石头布、剪刀石头布蜥蜴飞龙以及你内心所想的一切规则的游戏。

一个名叫 David C. Lovelace 的了不起的人创造了最令人震惊的剪刀石头布游戏。你可以 看看这些 并用它们创建一个超级酷的游戏。这些规则相当复杂(嗯,并不总是)... 如果有一个 PHP 库帮助人们实现这个功能会怎么样。哦,等等。

有关实现细节,您可以参考测试套件,它将展示一些示例。无论如何,这里是它的精髓。对于经典的剪刀石头布游戏,你需要

  • 2 名玩家
  • 3 条规则
  • 每条规则必须击败其他 50% 的规则,并且必须被其他 50% 的规则击败(排除自身)。这就是为什么武器数量总是奇数。
  • 每个玩家必须选择一个武器进行游戏
  • 最后,你必须确定获胜者、失败者或平局情况
// Players of the gaaaaame!
$player1 = new Player("Ricardo V.", "Rock");
$player2 = new Player("Anna B.", "Paper");

// The ruleset for a regular Rock Paper Scissor game.
$rules = new RuleCollection();
$rules->add(new Rule("Paper", "Rock", "Covers"));
$rules->add(new Rule("Scissor", "Paper", "Cuts"));
$rules->add(new Rule("Rock", "Scissor", "Smashes"));

// You should validate it first to make sure it's all good
$validationResult = $rules->validate();
if($validationResult->isValid()) {
    $game = new Game($player1, $player2, $rules);
    $result = $game->result();

    // A game result can be either a Win or a Tie. A Win contains the players that participated (and their plays) as well
    // as the winning rule. A Tie just contains the players. You can do whatever you want with the data.
    if($result instanceof Tie) {
        /** @var Welhott\RockPaperScissor\Game\Result\Tie $result */
        print "\n» ".$result->getPlayer1()->getName()." tied ".$result->getPlayer2()->getName()."\n";
        print "» ".$result->getPlayer1()->getName(). " played ".$result->getPlayer1()->getPlay()."\n";
        print "» ".$result->getPlayer2()->getName(). " played ".$result->getPlayer2()->getPlay()."\n";
    } else if($result instanceof Win) {
        /** @var Welhott\RockPaperScissor\Game\Result\Win $result */
        print "\n» ".$result->getRule()->getText()."\n================\n";

        // Detailed
        print "» ".$result->getWinner()->getName(). " played ".$result->getWinner()->getPlay()."\n";
        print "» ".$result->getLoser()->getName(). " played ".$result->getLoser()->getPlay()."\n";
        print "» ".$result->getRule()->getWinner()." ".$result->getRule()->getOutcome()." ".$result->getRule()->getLoser()."\n";
        print "» ".$result->getWinner()->getName()." Win(s)!\n\n";
    } else {
        echo "Oops :P";
    }
} else {
    $reflection = new ReflectionClass("\\Welhott\\RockPaperScissor\\Validation\\ValidationMessage");
    $constants = $reflection->getConstants();

    /** @var \Welhott\RockPaperScissor\Validation\ValidationMessage $message */
    foreach($validationResult->getMessages() as $message) {
        $constant = array_search($message->getType(), $constants);
        print "» ".$constant." » ".$message->getMessage()."\n";
    }
}

贡献

欢迎你提出对库的改进建议,并亲自贡献 :)

许可

此项目的代码受 MIT 许可协议许可。