@ 2018-03-13T15:11:47Z <?php
//
// File Game.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];
}
}
//
// File 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;
}
}
//
// File app.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();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.1.1 , 7.1.4 , 7.1.6 - 7.1.7 , 7.1.9 , 7.1.11 , 7.1.23 , 7.1.25 - 7.1.26 , 7.1.29 - 7.1.33 , 7.2.0 , 7.2.3 , 7.2.6 , 7.2.9 , 7.2.11 - 7.2.12 , 7.2.15 , 7.2.20 , 7.2.23 - 7.2.26 , 7.2.28 - 7.2.29 , 7.2.32 , 7.3.0 - 7.3.1 , 7.3.4 - 7.3.6 , 7.3.10 - 7.3.12 , 7.3.15 - 7.3.16 , 7.3.18 - 7.3.20 , 7.4.1 - 7.4.2 , 7.4.4 , 7.4.6 - 7.4.7 , 8.1.2 Pavel
Pavel Output for 7.1.0 , 7.1.2 - 7.1.3 , 7.1.5 , 7.1.8 , 7.1.10 , 7.1.12 - 7.1.22 , 7.1.24 , 7.1.27 - 7.1.28 , 7.2.1 - 7.2.2 , 7.2.4 - 7.2.5 , 7.2.7 - 7.2.8 , 7.2.10 , 7.2.13 - 7.2.14 , 7.2.16 - 7.2.19 , 7.2.21 - 7.2.22 , 7.2.27 , 7.2.30 - 7.2.31 , 7.2.33 , 7.3.2 - 7.3.3 , 7.3.7 - 7.3.9 , 7.3.13 - 7.3.14 , 7.3.17 , 7.3.21 - 7.3.22 , 7.4.0 , 7.4.3 , 7.4.5 , 7.4.8 - 7.4.10 Gosho
Pavel Output for 7.0.0 - 7.0.33 Fatal error: Uncaught TypeError: Return value of Game::playRounds() must be an instance of void, none returned in /in/Tvb6h:32
Stack trace:
#0 /in/Tvb6h(151): Game->playRounds(3)
#1 {main}
thrown in /in/Tvb6h on line 32
Process exited with code 255 . Output for 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/Tvb6h on line 24
Process exited with code 255 . Output for 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 Parse error: syntax error, unexpected '[' in /in/Tvb6h on line 12
Process exited with code 255 . Output for 5.0.0 - 5.0.5 Parse error: parse error, unexpected '[' in /in/Tvb6h on line 12
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Tvb6h on line 9
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Tvb6h on line 9
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Tvb6h on line 9
Process exited with code 255 . preferences:dark mode live preview
204.09 ms | 401 KiB | 351 Q