<?php
header("Content-Type: text/plain; charset=utf-8");
?>

<?php
mb_internal_encoding("UTF-8");
class Player
{
    public $name;
    public $icon;
    public $isWinner;
    public $position;
    public function __construct($name, $icon)
    {
        $this->name     = $name;
        $this->icon     = $icon;
        $this->position = 1;
    }
}
class Desk
{
    public $cases;
    public $players;
    public $round;
    public function __construct($players)
    {
        $this->cases   = array(
            4 => 42,
            16 => 8,
            9 => 30,
            32 => 12,
            14 => 77,
            62 => 19,
            80 => 98,
            96 => 76,
            71 => 67,
            48 => 73,
            37 => 58,
            47 => 26,
            70 => 89
        );
        $this->players = $players;
        $this->round   = 1;
    }
    public function getZone()
    {
        for ($i = 0; $i <= 99; $i++)
        $zone = range(1, 100, 1);           
        $this->zone = $zone;
        return $this->zone;
    }
    public function getRoll()
    {
        $roll = mt_rand(1, 6);
        return $roll;
    }
    public function getGame()
    {
        do {
            foreach ($this->players as $player) {
                
                $this->getMove($player);
                
                if ($player->isWinner == 1) {
                    return $player->name;
                }
                echo $this->getShow();
            }
            
            echo str_repeat('_', 50) . "\n";
            echo "End of $this->round round \n\n";
            echo "Positions after $this->round: \n";
            foreach ($this->players as $player) {
                echo "$player->name ($player->icon) position is: $player->position \n";
            }
            echo "\n";
            $this->round++;
        } while ($player->isWinner == 0);
        
    }
    public function getMove($player)
    {
        $lastPosition = $player->position;
        $roll         = $this->getRoll();
        $player->position += $roll;
        
        echo "\n Rolling the dice for $player->name ($player->icon): $roll \n";
        foreach ($this->cases as $key => $value) {
            if ($player->position == $key) {
                echo "$player->name is moving from $player->position to ";
                $player->position = $value;
                echo "$player->position \n";
            }
        }
        if ($lastPosition + $roll > 100) {
            $player->position = 100;
        }
        
        if ($player->position == 100) {
            echo "CONGRATULATION: $player->name ($player->icon) - THE WINNER!!! \n";
            $player->isWinner = 1;
            return $player->isWinner;
        }
    }
    public function getShow()
    {
        $this->getZone();
        foreach ($this->zone as &$square) {
            foreach ($this->players as $player) {
                if ($square == $player->position) {
                    $square .= $player->icon;
                }
            }
        }
        
        
        $text = '';
        $k    = 1;
        foreach ($this->zone as $position) {
            echo " $position ";
            if ($k == (ceil($k / 10)) * 10)
                echo "\n";
            $k++;
        }
    }
}

$player1 = new Player('Player1', '@');
$player2 = new Player('Player2', '#');
$player3 = new Player('Player3', '*');
$player4 = new Player('Player4', '%');

$players = array(
    $player1,
    $player2,
    $player3,
    $player4
);
$desk    = new Desk($players);
function padRight($string)
{
    $count = 50 - mb_strlen($string);
    if ($count <= 0) {
        return $string;
    }
    $space = str_repeat(' ', $count / 2);
    return $space . $string . $space;
}
$desk->getGame();
