fork download
  1. <!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">
  2. <html xmlns="http://w...content-available-to-author-only...3.org/1999/xhtml">
  3. <head>
  4.  
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6. <title>Snake adventures</title>
  7. <link href="css/style.css" rel="stylesheet" type="text/css" />
  8. </head>
  9.  
  10. <body>
  11. <?php
  12. class Player
  13. {
  14. public $name;
  15. public $icon;
  16. public $isWinner;
  17. public $position;
  18. public function __construct($name, $icon)
  19. {
  20. $this->name = $name;
  21. $this->icon = $icon;
  22. $this->position = 1;
  23. }
  24. }
  25. class Desk
  26. {
  27. public $cases;
  28. public $players;
  29. public $round;
  30. public function __construct($players)
  31. {
  32. $this->cases = array(
  33. 4 => 42,
  34. 16 => 8,
  35. 9 => 30,
  36. 32 => 12,
  37. 14 => 77,
  38. 62 => 19,
  39. 80 => 98,
  40. 96 => 76,
  41. 71 => 67,
  42. 48 => 73,
  43. 37 => 58,
  44. 47 => 26,
  45. 70 => 89
  46. );
  47. $this->players = $players;
  48. $this->round = 1;
  49. }
  50. public function getZone()
  51. {
  52. $zone = range(1, 100, 1);
  53. $this->zone = $zone;
  54. return $this->zone;
  55. }
  56. public function getRoll()
  57. {
  58. $roll = mt_rand(1, 6);
  59. return $roll;
  60. }
  61. public function getGame()
  62. {
  63. do {
  64. foreach ($this->players as $player) {
  65.  
  66. $this->getMove($player);
  67.  
  68. if ($player->isWinner == 1) {
  69. return $player->name;
  70. }
  71.  
  72. }
  73. echo $this->getShow();
  74. echo str_repeat('_', 50) . "<br>";
  75. echo "End of $this->round round <br><br>";
  76. echo "Positions after $this->round round: <br>";
  77. foreach ($this->players as $player) {
  78. echo "$player->name ($player->icon) position is: $player->position <br>";
  79. }
  80. echo "<br>";
  81. $this->round++;
  82. } while (FALSE);
  83. return $this->players;
  84. }
  85. public function getMove($player)
  86. {
  87. $lastPosition = $player->position;
  88. $roll = $this->getRoll();
  89. $player->position += $roll;
  90.  
  91. echo "Rolling the dice for $player->name ($player->icon): $roll <br>";
  92. foreach ($this->cases as $key => $value) {
  93. if ($player->position == $key) {
  94. echo "$player->name is moving from $player->position to ";
  95. $player->position = $value;
  96. echo "$player->position <br>";
  97. }
  98. }
  99. if ($lastPosition + $roll > 100) {
  100. $player->position = 100;
  101. }
  102.  
  103. if ($player->position == 100) {
  104. echo "CONGRATULATION: $player->name ($player->icon) - THE WINNER!!! <br>";
  105. $player->isWinner = 1;
  106. return $player->isWinner;
  107. }
  108. }
  109. public function getShow()
  110. {
  111. $this->getZone();
  112. foreach ($this->zone as &$square) {
  113. foreach ($this->players as $player) {
  114. if ($square == $player->position) {
  115. $square .= $player->icon;
  116. }
  117. }
  118. }
  119.  
  120.  
  121. $text = '';
  122. $k = 1;
  123. foreach ($this->zone as $position) {
  124. echo " $position ";
  125. if ($k == (ceil($k / 10)) * 10)
  126. echo "<br>";
  127. $k++;
  128. }
  129. }
  130. }
  131.  
  132. function getPlayers($nick)
  133. {
  134.  
  135. $player1 = new Player("$nick", '@');
  136. $player2 = new Player('AI_Player2', '#');
  137. $player3 = new Player('AI_Player3', '*');
  138. $player4 = new Player('AI_Player4', '%');
  139.  
  140. $players = array(
  141. $player1,
  142. $player2,
  143. $player3,
  144. $player4
  145. );
  146. $desk = new Desk($players);
  147. return $desk;
  148. }
  149. function padRight($string)
  150. {
  151. $count = 50 - mb_strlen($string);
  152. if ($count <= 0) {
  153. return $string;
  154. }
  155. $space = str_repeat(' ', $count / 2);
  156. return $space . $string . $space;
  157. }
  158. $desk->getGame();
  159. if (isset($_POST['submit1'])) {
  160. $nick = $_POST["name1"];
  161. $desk = getPlayers($nick);
  162. }
  163. if (isset($_POST['submit2'])) {
  164. $desk->getGame();
  165. }
  166. ?>
  167.  
  168. <form method="POST">
  169. <input type="submit" name="submit2" value="Next Round!" />
  170. </form>
  171. </body>
  172. </html>
  173.  
Runtime error #stdin #stdout #stderr 0.01s 24448KB
stdin
Standard input is empty
stdout
<!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>
stderr
PHP Notice:  Undefined variable: desk in /home/EMVrVP/prog.php on line 159
PHP Fatal error:  Call to a member function getGame() on null in /home/EMVrVP/prog.php on line 159