-2

描述:游戏必须开始,然后进行若干轮(必须放置 playRounds (3)),并且在游戏模式调用中,玩家必须撤出手牌并成为该轮的获胜者。回合结束后,必须有一个获胜者方法来显示哪个玩家获胜。当有获胜者时,一轮结束 - 在同一手牌中,游戏在同一轮中继续进行,直到出现获胜者。要求:无前端(游戏可以报告打印发生的情况 - 不需要任何其他可视化)您应该能够轻松获得额外的选项来绘制不同类型的“石头、剪刀、纸”手。

奖励条件: 1. 已实现单元测试;2. 在作曲家包上制作。

文件 app.php

<?php

    include "entity/Game.php";
    include "entity/Player.php";

    $pavel = new Player("Pavel");
    $gosho = new Player("Gosho");

    $game = new Game([$pavel, $gosho]);
    $game->playRounds(3);
    $game->play();

    echo $game->getWinner()->getName();

    $anotherGame = new Game([$pavel, $gosho]);
    $pavel->setWeapon("paper");
    $gosho->setWeapon("stone");
    echo PHP_EOL . $anotherGame->fight()->getName();

文件游戏.php

<?php

class Game
{
    /** @var Player[] */
    private $players;
    private $rounds;
    private $winner;
    private $weapons = ["stone", "scissors", "paper"];

    public function __construct(array $players)
    {
        $this->players = $players;
    }

    public function setRounds(int $rounds)
    {
        $this->rounds = $rounds;
    }

    public function getRounds(): int
    {
        return $this->rounds;
    }

    public function playRounds(int $rounds): void
    {
        $this->rounds = $rounds;
    }

    public function getWinner(): Player
    {
        return $this->winner;
    }

    public function setWinner(Player $winner): void
    {
        $this->winner = $winner;
    }

    public function play()
    {
        while ($this->getRounds() !== 0) {
            $this->players[0]->setWeapon($this->weapons[rand(0, 2)]);
            $this->players[1]->setWeapon($this->weapons[rand(0, 2)]);

            if ($this->players[0]->getWeapon() === $this->players[1]->getWeapon()) {
                continue;
            }

            $this->fight()->addScore();
            $this->setRounds($this->getRounds() - 1);
        }

        if ($this->players[0]->getScore() > $this->players[1]->getScore()) {
            $this->setWinner($this->players[0]);
        } else {
            $this->setWinner($this->players[1]);
        }
    }

    public function fight(): Player
    {

        if ($this->players[0]->getWeapon() === $this->weapons[0]) {
            if ($this->players[1]->getWeapon() === $this->weapons[1]) {
                return $this->players[0];
            }

            if ($this->players[1]->getWeapon() === $this->weapons[2]) {
                return $this->players[1];
            }
        }

        if ($this->players[0]->getWeapon() === $this->weapons[1]) {
            if ($this->players[1]->getWeapon() === $this->weapons[0]) {
                return $this->players[1];
            }

            if ($this->players[1]->getWeapon() === $this->weapons[2]) {
                return $this->players[0];
            }
        }

        if ($this->players[0]->getWeapon() === $this->weapons[2]) {
            if ($this->players[1]->getWeapon() === $this->weapons[0]) {
                return $this->players[0];
            }
        }

        return $this->players[1];
    }
}

文件 Player.php

class Player
{
    private $name;
    private $score = 0;
    private $weapon;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function getWeapon(): string
    {
        return $this->weapon;
    }

    public function setWeapon(string $weapon): void
    {
        $this->weapon = $weapon;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getScore(): int
    {
        return $this->score;
    }

    public function addScore(int $score = 1): void
    {
        $this->score += $score;
    }
}

代码写在php7.2上。

如何解决这个致命错误?如何修复错误并解决问题?

致命错误:未捕获的类型错误:Game::playRounds() 的返回值必须是 void 的实例,在第 30 行的 D:\XAMPP_2\htdocs\php-oop\task\game\entity\Game.php 中没有返回类型错误: Game::playRounds() 的返回值必须是 void 的实例,在第 30 行的 D:\XAMPP_2\htdocs\php-oop\task\game\entity\Game.php 中没有返回

4

3 回答 3

2

void返回类型已在PHP 7.1 中实现。PHP 7.0 将void返回类型视为类名。

确保您使用 PHP 7.1 或更高版本运行代码。

检查您的代码如何使用各种 PHP 解释器执行,并查看 PHP 7.0 如何报告您发布的错误消息:https ://3v4l.org/Tvb6h#v700

于 2018-03-13T15:12:46.350 回答
1

voidtype 在7.1中添加到 PHP 中,但要使其在较低版本的 PHP 中工作,只需删除:void结果将是相同的。特别是因为您不使用严格模式。

于 2018-03-13T15:26:40.267 回答
1

使用控制台检查 PHP 版本可能会给您带来另一个版本。使用 phpinfo() 函数检查您的 PHP 版本,并确保您使用的是最新版本。

sudo a2dismod php7.0
sudo a2enmod php7.2
sudo service apache2 restart
于 2018-08-27T18:09:23.137 回答