【发布时间】:2018-08-21 21:45:09
【问题描述】:
描述:游戏必须开始,然后进行若干回合(必须放置 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();
文件 Game.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上编写的。
如何解决这个致命错误?如何修复错误并解决问题?
致命错误: Uncaught TypeError: Return value of Game::playRounds() must be an instance of void, none return in D:\XAMPP_2\htdocs\php-oop\task\game\entity\Game.php 上线30 TypeError:Game::playRounds()的返回值必须是void实例,D:\XAMPP_2\htdocs\php-oop\task\game\entity\Game.php第30行没有返回
【问题讨论】:
-
你确定这是在 7.2 上运行的吗?你更有可能在 PHP 7.0 上运行它
-
错误信息有用。你用谷歌搜索了错误吗?另外,第 30 行是哪一行?它准确地告诉您问题出在哪里(老实说,问题是什么)。
-
@JonStirling - 据我所知,是什么让你觉得它是 7.0 而不是 7.2?
-
这正是您在 7.0 上运行时看到的完全。 void 返回类型提示是在 7.1 中添加的——在此之前它被解释为类名。见3v4l.org/X5OZl
-
要让它在 PHP(支持)版本 5 中运行,您需要删除返回类型提示和标量类型提示。