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

<?php
mb_internal_encoding("UTF-8");
class Player
{
    public $name;
    public $icon;
    public $x;
    public $y;
    public $isWinner;
    public $position;
    public function __construct($name, $icon)
    {
        $this->name     = $name;
        $this->icon     = $icon;
        $this->x        = 1;
        $this->y        = 0;
        $this->position = 1;
    }
    public function getMove()
    {
        $round          = 1;
        $roll           = mt_rand(1, 6);
        $lastX          = $this->x;
        $lastY          = $this->y;
        $this->x        = $this->x + $roll;
        $position       = $this->y * 10 + $this->x;
        $this->position = $position;
        echo "Rolling the dice for $this->name ($this->icon): $roll \n";
        if ($position < 100) {
            
            $length = 10 - $lastX;
            
            if ($length < $roll) {
                $roll    = $roll - $length;
                $this->x = $roll;
                $this->y = $this->y + 1;
            }
            if (100 - $position < $roll) {
                $position       = 100;
                $this->position = $position;
            }
            switch ($position) {
                case 4:
                    echo "Congratulation: $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 2;
                    $this->y        = 4;
                    $this->position = 42;
                    echo "to new position  $this->y$this->x \n";
                    break;
                case 16:
                    echo "Sorry but $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x = 8;
                    $this->y = 0;
                    echo "to new position is $this->y$this->x \n";
                    $this->position = 8;
                    break;
                case 9:
                    echo "Congratulation: $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 10;
                    $this->y        = 3;
                    $this->position = 30;
                    echo "to new position  $this->position \n";
                    break;
                case 32:
                    echo "Sorry but $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 2;
                    $this->y        = 1;
                    $this->position = 12;
                    echo "to new position  $this->y$this->x \n";
                    break;
                case 14:
                    echo "Congratulation: $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 7;
                    $this->y        = 7;
                    $this->position = 77;
                    echo "to new position  $this->y$this->x \n";
                    break;
                case 62:
                    echo "Sorry but $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 9;
                    $this->y        = 1;
                    $this->position = 19;
                    echo "to new position  $this->y$this->x \n";
                    break;
                case 80:
                    echo "Congratulation: $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 8;
                    $this->y        = 9;
                    $this->position = 98;
                    echo "to new position  $this->y$this->x \n";
                    break;
                case 96:
                    echo "Sorry but $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 6;
                    $this->y        = 7;
                    $this->position = 76;
                    echo "to new position  $this->y$this->x \n";
                    break;
                case 71:
                    echo "Sorry but $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 7;
                    $this->y        = 6;
                    $this->position = 67;
                    echo "to new position  $this->y$this->x \n";
                    break;
                case 48:
                    echo "Congratulation: $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 3;
                    $this->y        = 7;
                    $this->position = 73;
                    echo "to new position  $this->y$this->x \n";
                    break;
                case 37:
                    echo "Congratulation: $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 8;
                    $this->y        = 5;
                    $this->position = 58;
                    echo "to new position  $this->y$this->x \n";
                    break;
                case 47:
                    echo "Sorry but $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 6;
                    $this->y        = 2;
                    $this->position = 26;
                    echo "to new position  $this->y$this->x \n";
                    break;
                case 70:
                    echo "Congratulation: $this->name ($this->icon) is moving from $this->y$this->x ";
                    $this->x        = 9;
                    $this->y        = 8;
                    $this->position = 89;
                    echo "to new position  $this->y$this->x \n";
                    break;
                default:
                    echo "$this->name new position is $position \n";
            }
        } else {
            echo "CONGRATULATION: $this->name ($this->icon) - THE WINNER!!! \n";
            $this->position = 100;
            $this->x        = 10;
            $this->y        = 9;
            $this->isWinner = 1;
            return $this->isWinner;
        }
        
    }
    
}
$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)
{
    $zone = array();
    $k    = 1;
    for ($i = 0; $i <= 9; $i++) {
        for ($j = 1; $j <= 10; $j++) {
            $zone[$i][$j] = $k++;
        }
    }
    
    foreach ($players as $player) {
        $zone[$player->y][$player->x] .= $player->icon;
    }
    
    $text = '';
    foreach ($zone as $line) {
    	$string = '';
        foreach ($line as $position) {
            $string .= " $position ";
        }
        $text .= padRight($string) . "\n";        
    }
    return $text;
}


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


echo getShow($players);
