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) . "
"; echo "End of $this->round round

"; echo "Positions after $this->round round:
"; foreach ($this->players as $player) { echo "$player->name ($player->icon) position is: $player->position
"; } echo "
"; $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
"; 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
"; } } if ($lastPosition + $roll > 100) { $player->position = 100; } if ($player->position == 100) { echo "CONGRATULATION: $player->name ($player->icon) - THE WINNER!!!
"; $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 "
"; $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(); } ?>