<?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;
    }
    public function getRoll()
    {
        $roll = mt_rand(1, 6);
        return $roll;
    }
    public function getMove()
    {
        global $desk;
        $round        = 1;
        $lastPosition = $this->position;
        $roll         = $this->getRoll();
        $this->position += $roll;
        
        echo "Rolling the dice for $this->name ($this->icon): $roll \n";
        foreach ($desk->cases as $key => $value) {
            if ($this->position == $key) {
                echo "$this->name is moving from $this->position to ";
                $this->position = $value;
                echo "$this->position \n";
            }
        }
        if (100 - $lastPosition < $roll) {            
            $this->position = 100;
        }
        
        if ($this->position == 100) {            
            echo "CONGRATULATION: $this->name ($this->icon) - THE WINNER!!! \n";            
            $this->isWinner = 1;
            return $this->isWinner;
        }
        
    }
    
}
class Desk
{
    public $cases;
    
    public function __construct()
    {
        $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
        );
    }
    public function createDesk()
    {
        $zone = array();
        $k    = 1;
        for ($i = 0; $i <= 99; $i++) {
            $zone[] = $k++;
            
        }
        return $zone;
    }
}
$desk    = new Desk;
$player1 = new Player('Player1', '@');
$player2 = new Player('Player2', '#');
$player3 = new Player('Player3', '*');
$player4 = new Player('Player4', '%');

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

function getShow($players, $desk)
{
    
    $zone = $desk->createDesk();
    foreach ($zone as &$square) {
        foreach ($players as $player) {
            if ($square == $player->position) {
                $square .= $player->icon;
            }
        }
    }
    
    
    $text = '';
    $k    = 1;
    foreach ($zone as $position) {
        echo " $position ";
        if ($k == (ceil($k / 10)) * 10)
        {
            echo "\n";
        }
        $k++;
    }
}




function getGame($array, $desk)
{
    global $round;
    global $players;
    do {
        foreach ($array as $player) {
            
            $player->getMove();
            
            if ($player->isWinner == 1) {
                return $player->name;
            }
            echo getShow($players, $desk);
        }
        
        echo str_repeat('_', 50) . "\n";
        echo "End of $round round \n";
        $round++;
    } while ($player->isWinner == 0);
    
}
getGame($players, $desk);
