By Vladimir Kirov


2018-03-13 15:05:04 8 Comments

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

3 comments

@Zarpele 2018-08-27 18:09:23

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

@axiac 2018-03-13 15:12:46

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

@Vladimir Kirov 2018-03-13 15:23:41

My PHP Version 7.0.13, on newer versions does not make a mistake. How to do it with PHP Version 7.0.13.

@axiac 2018-03-13 15:27:19

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 and the scalar parameter types (int, string).

@Vladimir Kirov 2018-03-13 15:31:04

yes, I understood, it became clear to me. Thank you very much for your help. (y) :)

@Robert 2018-03-13 15:26:40

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.

@Vladimir Kirov 2018-03-13 15:31:53

Thank you very much for your help. (y) :) :)

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Fatal error: Class 'ZipArchive' not found in

  • 2010-10-06 12:47:01
  • Parag Chaure
  • 191031 View
  • 116 Score
  • 11 Answer
  • Tags:   php zip ziparchive

8 Answered Questions

[SOLVED] Fatal error: Class 'SoapClient' not found

  • 2012-07-09 08:27:52
  • Figen Güngör
  • 264580 View
  • 152 Score
  • 8 Answer
  • Tags:   php soap fatal-error

18 Answered Questions

[SOLVED] PHP Fatal error: Cannot redeclare class

0 Answered Questions

Neither the property "parent" build form Symfony 2 Self-Referenced mapping

  • 2018-02-13 11:45:54
  • Tomek Kowalczyk
  • 71 View
  • 1 Score
  • 0 Answer
  • Tags:   php symfony doctrine

18 Answered Questions

7 Answered Questions

[SOLVED] Can't use method return value in write context

  • 2009-07-02 16:58:37
  • Extrakun
  • 217317 View
  • 448 Score
  • 7 Answer
  • Tags:   php

1 Answered Questions

adventure class of text based adventure game

  • 2016-04-29 23:38:36
  • Srey
  • 462 View
  • 0 Score
  • 1 Answer
  • Tags:   java oop

3 Answered Questions

[SOLVED] Create a Rock-Paper-Scissors game using a class for each

  • 2015-11-25 06:15:26
  • ScottVMeyers
  • 2434 View
  • 0 Score
  • 3 Answer
  • Tags:   c# oop

13 Answered Questions

[SOLVED] Missing return statement in a non-void method compiles

  • 2013-05-28 10:32:48
  • c.P.u1
  • 8055 View
  • 186 Score
  • 13 Answer
  • Tags:   c# java oop design

Sponsored Content