描述:游戏必须开始,然后进行若干轮(必须放置 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 中没有返回