-2

Description: Game has to be initiated, then a number of rounds (playRounds (3) must be placed) and, in a play mode call, players must be withdrawn hands and the winner of the round . After the rounds have ended, there must be a winner method that shows which player has won. A round ends when there is a winner - in the same hands the game continues in the same round until a winner is played. Requirements: No Frontend (the game can report what's happening with print - no need for any other visualization) You should easily be able to get an extra option to draw a different kind of "Stone, Scissors, Paper" hand.

Bonus conditions: 1. There have been realized unit tests; 2. Made on a composer package.

file 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();

file 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];
    }
}

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;
    }
}

the code is written on php7.2.

How to fix this fatal error? How to fix the error and solve the problem?

Fatal error: Uncaught TypeError: Return value of Game::playRounds() must be an instance of void, none returned in D:\XAMPP_2\htdocs\php-oop\task\game\entity\Game.php on line 30 TypeError: Return value of Game::playRounds() must be an instance of void, none returned in D:\XAMPP_2\htdocs\php-oop\task\game\entity\Game.php on line 30

random_user_name
  • 23,924
  • 7
  • 69
  • 103
Vladimir Kirov
  • 59
  • 3
  • 12
  • 2
    Are you sure that this is running on 7.2? More likely you're running it on PHP 7.0 – Jonnix Mar 13 '18 at 15:06
  • 2
    Error messages are _useful_. Did you google the _Error_? Additionally, _which line is line 30_? It tells you _exactly_ where the issue is (and honestly, what the issue is). – random_user_name Mar 13 '18 at 15:09
  • @JonStirling - just for my own knowledge, what gives you the sense it's 7.0 instead of 7.2? – random_user_name Mar 13 '18 at 15:10
  • This is *exactly* what you'd see when running on 7.0. The void return type hint was added in 7.1 - before that it's interpreted as a class name. See https://3v4l.org/X5OZl – iainn Mar 13 '18 at 15:11
  • @cale_b Exactly the way Iainn did :) – Jonnix Mar 13 '18 at 15:11
  • I'm not absolutely sure, but I think it works. My version is PHP Version 7.0.13. How to make it work on an older version: 5+ ? – Vladimir Kirov Mar 13 '18 at 15:12
  • 4
    To get it to run in PHP (supported) versions of 5, you'd need to remove the return type hints, and the scalar type hints. – Jonnix Mar 13 '18 at 15:13
  • See this article: https://stackoverflow.com/questions/35482195/php7-void-return-type-not-working – Minos Mar 13 '18 at 15:16
  • See this article also [https://stackoverflow.com/questions/35482195/php7-void-return-type-not-working](https://stackoverflow.com/questions/35482195/php7-void-return-type-not-working) – Minos Mar 13 '18 at 15:18

3 Answers3

1

The void return type has been implemented in PHP 7.1. PHP 7.0 treats the void return type as a class name.

Make sure you are running the code using PHP 7.1 or newer.

Check how your code is executed with various PHP intepreters and see how the error message you posted is reported by PHP 7.0: https://3v4l.org/Tvb6h#v700

axiac
  • 56,918
  • 8
  • 77
  • 110
  • My PHP Version 7.0.13, on newer versions does not make a mistake. How to do it with PHP Version 7.0.13. – Vladimir Kirov Mar 13 '18 at 15:23
  • To make it work with PHP 7.0 remove the `void` return type for the offending method. To make it work with PHP 5 remove all [return types](http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration) and the scalar [parameter types](http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration) (`int`, `string`). – axiac Mar 13 '18 at 15:27
  • yes, I understood, it became clear to me. Thank you very much for your help. (y) :) – Vladimir Kirov Mar 13 '18 at 15:31
1

void type was added to PHP in 7.1 but to make it work in lower version of PHP just remove :void the result will be the same. Especially because you don't use strict mode.

Robert
  • 17,808
  • 4
  • 50
  • 79
1

Checking the PHP version using the console maybe bring you another version. Check your PHP version using phpinfo() function and make sure if you are using the latest.

sudo a2dismod php7.0
sudo a2enmod php7.2
sudo service apache2 restart
Zarpele
  • 493
  • 1
  • 6
  • 10