fork download
  1. <?php
  2. abstract class Animal
  3. {
  4. protected $x;
  5. protected $y;
  6. protected $previousX;
  7. protected $previousY;
  8. protected $icon;
  9. protected $field;
  10.  
  11. public function __construct($coordinateX, $coordinateY, $symbol)
  12. {
  13. $this->x = $coordinateX;
  14. $this->y = $coordinateY;
  15. $this->icon = $symbol;
  16. }
  17. public function getCoordinateX()
  18. {
  19. return $this->x;
  20. }
  21. public function getCoordinateY()
  22. {
  23. return $this->y;
  24. }
  25.  
  26. public function showCoordinates()
  27. {
  28. echo "Координата X = [$this->x]";
  29. echo "Координата Y = [$this->y";
  30. }
  31.  
  32.  
  33. public function getIcon()
  34. {
  35. return $this->icon;
  36. }
  37.  
  38.  
  39.  
  40. public function setField(GameField $field)
  41. {
  42. $this->field = $field;
  43. }
  44. }
  45.  
  46. class Mouse extends Animal
  47. {
  48. protected $cats = array();
  49.  
  50. public function findAllCats()
  51. {
  52.  
  53. $animals = $this->field->shareAnimals();
  54. foreach ($animals as $animal) {
  55. if ($animal instanceof Cat && (abs($this->x - $animal->getCoordinateX()) <= 9 || abs($this->y - $animal->getCoordinateY()) <= 9)) {
  56. $this->cats[] = $animal;
  57. }
  58. }
  59. }
  60.  
  61. //временный код для одной кошки на поле:
  62.  
  63. public function makeMove()
  64. {
  65.  
  66. $this->findAllCats();
  67.  
  68. if ($this->x <= $this->cats[0]->getCoordinateX() && $this->x != 0) {
  69. $this->x = $this->x - 1;
  70.  
  71. }
  72.  
  73. elseif ($this->x >= $this->cats[0]->getCoordinateX() && $this->x != 19) {
  74. $this->x = $this->x + 1;
  75. } elseif ($this->y <= $this->cats[0]->getCoordinateY() && $this->y != 0) {
  76. $this->y = $this->y - 1;
  77. } elseif ($this->y >= $this->cats[0]->getCoordinateY() && $this->y != 19) {
  78. $this->y = $this->y + 1;
  79. }
  80.  
  81.  
  82. }
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89. class Cat extends Animal
  90. {
  91. protected $sleepCount = 0;
  92. protected $moveCount = 0;
  93. protected $previousIcon;
  94.  
  95. public function setSleepIcon()
  96. {
  97. $this->previousIcon = $this->icon;
  98. $this->icon = "@";
  99. }
  100.  
  101. public function setDefauldIcon()
  102. {
  103. $this->icon = $this->previousIcon;
  104. }
  105. public function fallAsleep()
  106. {
  107. $this->sleepCount = 1;
  108. $this->setSleepIcon();
  109. $this->moveCount = 0;
  110. }
  111.  
  112. public function move($x, $y)
  113. {
  114.  
  115. if ($this->sleepCount == 0) {
  116.  
  117. $target = $this->findClosestMouse();
  118.  
  119. $this->x = $this->x + $x;
  120. $this->y = $this->y + $y;
  121. $this->moveCount++;
  122. if ($this->moveCount == 8) {
  123. $this->fallAsleep();
  124.  
  125. }
  126.  
  127. if ($this->x == $target['x'] && $this->y == $target['y']) {
  128. $this->field->killMouse($this->x, $this->y);
  129. $this->fallAsleep();
  130.  
  131. }
  132. }
  133.  
  134.  
  135. else {
  136. $this->sleepCount--;
  137. $this->setDefauldIcon();
  138. }
  139.  
  140. }
  141.  
  142. public function makeMove()
  143. {
  144. $x = 0;
  145. $y = 0;
  146. $target = $this->findClosestMouse();
  147. if ($target['x'] > $this->x) {
  148. $x = 1;
  149. } elseif ($target['x'] < $this->x) {
  150. $x = -1;
  151. }
  152.  
  153. if ($target['y'] > $this->y) {
  154. $y = 1;
  155. } elseif ($target['y'] < $this->y) {
  156. $y = -1;
  157. }
  158.  
  159. $this->move($x, $y);
  160. }
  161.  
  162.  
  163. public function findClosestMouse()
  164. {
  165. $previous = INF;
  166. $target = array();
  167. foreach ($this->field->shareAnimals() as $animal) {
  168. if (!($animal instanceof Mouse)) {
  169. continue;
  170. }
  171. $distance = abs($this->x - $animal->getCoordinateX()) + abs($this->y - $animal->getCoordinateY());
  172. if ($distance < $previous) {
  173. $target['x'] = $animal->getCoordinateX();
  174. $target['y'] = $animal->getCoordinateY();
  175. $previous = $distance;
  176. }
  177.  
  178. }
  179.  
  180. return $target;
  181. }
  182.  
  183.  
  184. }
  185.  
  186.  
  187. class GameField
  188. {
  189. protected $field;
  190. protected $animals = array();
  191. protected $height;
  192. protected $width;
  193.  
  194. public function __construct($height, $width)
  195. {
  196. $this->height = $height;
  197. $this->width = $width;
  198. }
  199. public function printField()
  200. {
  201. $this->field = array_fill(0, $this->height, array_fill(0, $this->width, " . "));
  202.  
  203. foreach ($this->animals as $animal) {
  204. $x = $animal->getCoordinateX();
  205. $y = $animal->getCoordinateY();
  206. $this->field[$x][$y] = $animal->getIcon();
  207. }
  208.  
  209. foreach ($this->field as $value) {
  210. foreach ($value as $dot) {
  211. echo $dot;
  212. }
  213. echo "\n";
  214. }
  215. }
  216.  
  217. public function acquireAnimal(Animal $animal)
  218. {
  219. $this->animals[] = $animal;
  220. $animal->setField($this);
  221. }
  222.  
  223. public function shareAnimals()
  224. {
  225. return $this->animals;
  226. }
  227.  
  228. public function killMouse($x, $y)
  229. {
  230.  
  231. for ($i = 0; $i < count($this->animals); $i++) {
  232.  
  233. if ($this->animals[$i] instanceof Mouse && $this->animals[$i]->getCoordinateX() == $x && $this->animals[$i]->getCoordinateY() == $y) {
  234. unset($this->animals[$i]);
  235. }
  236. }
  237.  
  238. }
  239.  
  240.  
  241.  
  242. #Удалить в будущем эту хуйню:
  243.  
  244. public function testTheKitty()
  245. {
  246.  
  247. foreach ($this->animals as $animal) {
  248. if ($animal instanceof Cat) {
  249. $animal->makeMove();
  250.  
  251.  
  252. }
  253.  
  254.  
  255. }
  256. foreach ($this->animals as $animal) {
  257. if ($animal instanceof Mouse) {
  258. $animal->makeMove();
  259. }
  260.  
  261. }
  262.  
  263.  
  264.  
  265.  
  266. }
  267. }
  268.  
  269.  
  270. $test = new GameField(5, 5);
  271. $mouse1 = new Mouse(3, 2, "1");
  272. $mouse2 = new Mouse(4, 0, "2");
  273. $kitty = new Cat(1, 1, "K");
  274.  
  275. $test->acquireAnimal($mouse1);
  276. $test->acquireAnimal($mouse2);
  277. $test->acquireAnimal($kitty);
  278. $test->printField();
  279. echo "\n";
  280. for ($i = 0; $i < 23; $i++) {
  281. $test->testTheKitty();
  282. $test->printField();
  283. echo "\n";
  284.  
  285.  
  286. }
Success #stdin #stdout #stderr 0.01s 20520KB
stdin
Standard input is empty
stdout
 .  .  .  .  . 
 . K .  .  . 
 .  .  .  .  . 
 .  . 1 .  . 
2 .  .  .  . 

 .  .  .  .  . 
 .  .  .  .  . 
 .  . K .  . 
 .  .  .  .  . 
 .  . 1 .  . 
2

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  . K .  . 
 .  .  .  .  . 
1
2

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  . K .  . 
1
2

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
@

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
1
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
12
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
12
@

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
2
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
@

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
K

 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
 .  .  .  .  . 
K

stderr
PHP Notice:  Undefined index: x in /home/QJ27zT/prog.php on line 147
PHP Notice:  Undefined index: x in /home/QJ27zT/prog.php on line 149
PHP Notice:  Undefined index: y in /home/QJ27zT/prog.php on line 153
PHP Notice:  Undefined index: y in /home/QJ27zT/prog.php on line 155
PHP Notice:  Undefined index: x in /home/QJ27zT/prog.php on line 147
PHP Notice:  Undefined index: x in /home/QJ27zT/prog.php on line 149
PHP Notice:  Undefined index: y in /home/QJ27zT/prog.php on line 153
PHP Notice:  Undefined index: y in /home/QJ27zT/prog.php on line 155
PHP Notice:  Undefined index: x in /home/QJ27zT/prog.php on line 127
PHP Notice:  Undefined index: x in /home/QJ27zT/prog.php on line 147
PHP Notice:  Undefined index: x in /home/QJ27zT/prog.php on line 149
PHP Notice:  Undefined index: y in /home/QJ27zT/prog.php on line 153
PHP Notice:  Undefined index: y in /home/QJ27zT/prog.php on line 155
PHP Notice:  Undefined index: x in /home/QJ27zT/prog.php on line 127