<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w...content-available-to-author-only...3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://w...content-available-to-author-only...3.org/1999/xhtml">
<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Snake adventures</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?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()
    {
        $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) . "<br>";
            echo "End of $this->round round <br><br>";
            echo "Positions after $this->round round: <br>";
            foreach ($this->players as $player) {
                echo "$player->name ($player->icon) position is: $player->position <br>";
            }
            echo "<br>";
            $this->round++;
        } while (FALSE);
        return $this->players;
    }
    public function getMove($player)
    {
        $lastPosition = $player->position;
        $roll         = $this->getRoll();
        $player->position += $roll;
        
        echo "Rolling the dice for $player->name ($player->icon): $roll <br>";
        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 <br>";
            }
        }
        if ($lastPosition + $roll > 100) {
            $player->position = 100;
        }
        
        if ($player->position == 100) {
            echo "CONGRATULATION: $player->name ($player->icon) - THE WINNER!!! <br>";
            $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 "<br>";
            $k++;
        }
    }
}

function getPlayers($nick)
{
    
    $player1 = new Player("$nick", '@');
    $player2 = new Player('AI_Player2', '#');
    $player3 = new Player('AI_Player3', '*');
    $player4 = new Player('AI_Player4', '%');
    
    $players = array(
        $player1,
        $player2,
        $player3,
        $player4
    );
    $desk    = new Desk($players);
    return $desk;
}
function padRight($string)
{
    $count = 50 - mb_strlen($string);
    if ($count <= 0) {
        return $string;
    }
    $space = str_repeat(' ', $count / 2);
    return $space . $string . $space;
}
$desk->getGame();
if (isset($_POST['submit1'])) {
    $nick = $_POST["name1"];
    $desk = getPlayers($nick);
}
if (isset($_POST['submit2'])) {
    $desk->getGame();
}
?>

<form method="POST">
    <input type="submit" name="submit2" value="Next Round!" />
</form>
</body>
</html>
