fork download
  1. <?php
  2.  
  3. class World {
  4.  
  5. public $sideLength;
  6. public $field = array();
  7. public $filler = "0";
  8. public $underworld = array();
  9.  
  10. function __construct($sideLength) {
  11. $this->sideLength = $sideLength;
  12. $this->field = array_fill(1, $sideLength, array_fill(1, $sideLength, $this->filler));
  13. }
  14.  
  15. function draw() {
  16. foreach ($this->field as $line) {
  17. echo implode(" ",$line);
  18. echo "\n";
  19. }
  20.  
  21. echo str_repeat("- -", $this->sideLength) . "\n";
  22.  
  23. $dead = implode(" ", $this->underworld);
  24.  
  25. echo $dead . "\n";
  26.  
  27. echo str_repeat("- -", $this->sideLength) . "\n";
  28. }
  29.  
  30. function place(Animal $animal) {
  31. $try = 100;
  32. do {
  33. $x = mt_rand(1, $this->sideLength);
  34. $y = mt_rand(1, $this->sideLength);
  35.  
  36. if ($this->field[$x][$y] == $this->filler) {
  37. $this->field[$x][$y] = $animal->icon;
  38. $animal->placed = true;
  39. $animal->x = $x;
  40. $animal->y = $y;
  41. }
  42. $try--;
  43.  
  44. } while ($animal->placed == false and $try > 0);
  45.  
  46. if ($try == 0) {
  47. $this->underworld[] = $animal->icon;
  48. }
  49. }
  50.  
  51. }
  52.  
  53. abstract class Animal {
  54. public $x;
  55. public $y;
  56. public $icon;
  57. public $placed = false;
  58. public $dead = false;
  59.  
  60. function __construct($icon) {
  61. $this->icon = $icon;
  62. }
  63. }
  64.  
  65. class Mouse extends Animal {
  66. public function move (World $world) {
  67. if ($this->dead == false && $world->field[$this->x][$this->y] != $this->icon) {
  68. $this->dead = true;
  69. $world->underworld[] = $this->icon;
  70. }
  71.  
  72. if ($this->dead == false) {
  73. $directions = array();
  74. if (isset($world->field[$this->x][$this->y - 1]) and ($world->field[$this->x][$this->y - 1] == $world->filler)) {
  75. $directions[] = array($this->x, $this->y - 1);
  76. }
  77. if (isset($world->field[$this->x + 1][$this->y]) and ($world->field[$this->x + 1][$this->y] == $world->filler)) {
  78. $directions[] = array($this->x + 1, $this->y);
  79. }
  80. if (isset($world->field[$this->x][$this->y + 1]) and ($world->field[$this->x][$this->y + 1] == $world->filler)) {
  81. $directions[] = array($this->x, $this->y + 1);
  82. }
  83. if (isset($world->field[$this->x - 1][$this->y]) and ($world->field[$this->x - 1][$this->y] == $world->filler)) {
  84. $directions[] = array($this->x - 1, $this->y);
  85. }
  86.  
  87. if (!empty($directions)) {
  88. $moveTo = mt_rand(0, count($directions)-1);
  89.  
  90. $world->field[$this->x][$this->y] = $world->filler;
  91.  
  92. $this->x = $directions[$moveTo][0];
  93. $this->y = $directions[$moveTo][1];
  94.  
  95. $world->field[$this->x][$this->y] = $this->icon;
  96. //var_dump($this);
  97. }
  98. }
  99. }
  100. }
  101.  
  102. class Cat extends Animal {
  103. public function move (World $world) {
  104.  
  105. for ($i = ($this->x - 1); $i <= ($this->x + 1); $i++) {
  106. for ($j = ($this->y - 1); $j <= ($this->y + 1); $j++) {
  107.  
  108. if($i == $this->x and $j == $this->y) {
  109. continue;
  110. }
  111.  
  112. if (isset($world->field[$i][$j]) && ($world->field[$i][$j] != $world->filler)) {
  113.  
  114.  
  115.  
  116. $world->field[$this->x][$this->y] = $world->filler;
  117.  
  118. $this->x = $i;
  119. $this->y = $j;
  120.  
  121. $world->field[$this->x][$this->y] = $this->icon;
  122.  
  123. break 2;
  124.  
  125. }
  126. }
  127. }
  128. }
  129. }
  130.  
  131. $world1 = new World(5);
  132.  
  133. $mouse1 = new Mouse('1');
  134. $mouse2 = new Mouse('2');
  135. $mouse3 = new Mouse('3');
  136. $mouse4 = new Mouse('4');
  137. $mouse5 = new Mouse('5');
  138.  
  139. $cat1 = new Cat('#');
  140.  
  141. $world1->place($mouse1);
  142. $world1->place($mouse2);
  143. $world1->place($mouse3);
  144. $world1->place($mouse4);
  145. $world1->place($mouse5);
  146. $world1->place($cat1);
  147.  
  148.  
  149. $world1->draw();
  150.  
  151. for ($i = 0; $i < 6; $i++) {
  152.  
  153. $mouse1->move($world1);
  154. $mouse2->move($world1);
  155. $mouse3->move($world1);
  156. $mouse4->move($world1);
  157. $mouse5->move($world1);
  158.  
  159. $cat1->move($world1);
  160.  
  161. $world1->draw();
  162.  
  163. }
  164.  
  165.  
  166.  
  167.  
  168. ?>
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
0  0  0  2  0
0  0  1  0  0
0  0  0  0  0
0  #  0  5  0
0  0  3  4  0
- -- -- -- -- -

- -- -- -- -- -
0  0  1  0  2
0  0  0  0  0
0  0  0  0  0
0  0  #  0  0
0  0  4  5  0
- -- -- -- -- -

- -- -- -- -- -
0  0  0  0  0
0  0  1  0  2
0  0  0  0  0
0  0  0  0  0
0  #  0  0  5
- -- -- -- -- -
3
- -- -- -- -- -
0  0  0  0  2
0  0  0  1  0
0  0  0  0  0
0  0  0  0  5
0  #  0  0  0
- -- -- -- -- -
3 4
- -- -- -- -- -
0  0  0  2  0
0  0  0  0  1
0  0  0  0  0
0  0  0  5  0
0  #  0  0  0
- -- -- -- -- -
3 4
- -- -- -- -- -
0  0  0  0  2
0  0  0  0  0
0  0  0  5  1
0  0  0  0  0
0  #  0  0  0
- -- -- -- -- -
3 4
- -- -- -- -- -
0  0  0  2  0
0  0  0  0  0
0  0  5  0  0
0  0  0  0  1
0  #  0  0  0
- -- -- -- -- -
3 4
- -- -- -- -- -