fork download
  1. <?php
  2.  
  3.  
  4. class Field
  5. {
  6. private $size;
  7. private $mapOfField = array();
  8. private $numberOfCats = 0;
  9. private $cats = array();
  10. private $numberOfMouses = 0;
  11. private $mouses = array();
  12. public function __construct($size)
  13. {
  14. $this->size = $size;
  15. $string = array();
  16. for ($a = 0; $a <= $size; $a++) {
  17. $string[$a] = null;
  18. }
  19. for ($i = 0; $i <= $size; $i++) {
  20. $this->mapOfField[$i] = $string;
  21. }
  22. }
  23. public function add(Animal $animal)
  24. {
  25. $type = $animal->getType();
  26. if ($type == 'mouse') {
  27. $this->mouses[$this->numberOfMouses] = $animal;
  28. $animal->setNumber($this->numberOfMouses);
  29. $this->numberOfMouses++;
  30. } elseif ($type == 'cat') {
  31. $this->cats[$this->numberOfCats] = $animal;
  32. $animal->setNumber($this->numberOfCats);
  33. $this->numberOfCats++;
  34. }
  35. $x = $animal->getCoordinate('x');
  36. $y = $animal->getCoordinate('y');
  37. $this->mapOfField[$x][$y] = $animal;
  38. }
  39. public function delete($animal)
  40. {
  41. $x = $animal->getCoordinate('x');
  42. $y = $animal->getCoordinate('y');
  43. $this->mapOfField[$x][$y] = null;
  44. $number = $animal->getNumber();
  45. if ($number != $this->numberOfMouses - 1) {
  46. $this->mouses[$number] = array_pop($this->mouses);
  47. } elseif($number == $this->numberOfMouses - 1) {
  48. $this->mouses[$number] = null;
  49. }
  50. $animal->dieMouse();
  51. }
  52. public function getAllCats ()
  53. {
  54. return $this->cats;
  55. }
  56. public function getMouses()
  57. {
  58. return $this->mouses;
  59. }
  60. public function getSize()
  61. {
  62. return $this->size;
  63. }
  64. public function getMap ()
  65. {
  66. return $this->mapOfField;
  67. }
  68. public function updateCoordinates($animal, $x, $y)
  69. {
  70. $type = $animal->getType();
  71. if (($x <= $this->size && $x >= 0) && ($y <= $this->size && $y >= 0)) {
  72. if ($type == 'cat') {
  73. $cell = $this->mapOfField[$x][$y];
  74. if (($cell == null) || (($aimType = $cell->getType()) == 'mouse')) {
  75. $startX = $animal->getCoordinate('x');
  76. $startY = $animal->getCoordinate('y');
  77. $this->mapOfField[$startX][$startY] = null;
  78. $animal->setCoordinate($x,$y);
  79. $this->mapOfField[$x][$y] = $animal;
  80. }
  81. } elseif ($type == 'mouse') {
  82. $cell = $this->mapOfField[$x][$y];
  83. if ($cell == null) {
  84. $startX = $animal->getCoordinate('x');
  85. $startY = $animal->getCoordinate('y');
  86. $this->mapOfField[$startX][$startY] = null;
  87. $animal->setCoordinate($x,$y);
  88. $this->mapOfField[$x][$y] = $animal;
  89. }
  90. }
  91. }
  92. }
  93. }
  94.  
  95. abstract class Animal
  96. {
  97. protected $type;
  98. protected $number;
  99. protected $x;
  100. protected $y;
  101. public function __construct($type, $x, $y)
  102. {
  103. $this->type = $type;
  104. $this->x = $x;
  105. $this->y = $y;
  106. }
  107. public function getCoordinate($orXY)
  108. {
  109. if ($orXY == 'x') {
  110. return $this->x;
  111. } elseif ($orXY == 'y') {
  112. return $this->y;
  113. }
  114. }
  115. public function setCoordinate($x, $y)
  116. {
  117. $this->x = $x;
  118. $this->y = $y;
  119. }
  120. public function setNumber($number)
  121. {
  122. $this->number = $number;
  123. }
  124. public function getType()
  125. {
  126. return $this->type;
  127. }
  128. public function getNumber()
  129. {
  130. return $this->number;
  131. }
  132. abstract public function move($field);
  133. }
  134.  
  135. class Cat extends Animal
  136. {
  137. private $hungry = 1;
  138.  
  139. public function move($field)
  140. {
  141. $x = $this->x;
  142. $y = $this->y;
  143. $min = $field->getSize();
  144. if ($this->hungry == 1) {
  145. $mouses = $field->getMouses();
  146. foreach ($mouses as &$value) {
  147. $mouseX = $value->getCoordinate('x');
  148. $mouseY = $value->getCoordinate('y');
  149. $distance = sqrt(pow($mouseX - $x, 2) + pow($mouseY - $y, 2));
  150. if ($distance <= $min) {
  151. $aimAnimal = $value;
  152. $aimX = $mouseX;
  153. $aimY = $mouseY;
  154. }
  155. }
  156. if ($x < $aimX) {
  157. $x++;
  158. } elseif ($x > $aimX) {
  159. $x--;
  160. } else {
  161. if ($y == $aimY) {
  162. $this->hungry = 0;
  163. $field->delete($aimAnimal);
  164. }
  165. }
  166. if ($y < $aimY) {
  167. $y++;
  168. } elseif ($y > $aimY) {
  169. $y--;
  170. } else {
  171. if ($x == $aimX) {
  172. $this->hungry = 0;
  173. $field->delete($aimAnimal);
  174. }
  175. }
  176. $field->updateCoordinates($this, $x, $y);
  177. } elseif ($this->hungry == 0) {
  178. $this->hungry = 1;
  179. }
  180. }
  181. }
  182.  
  183. function printField($array)
  184. {
  185. foreach ($array as &$value) {
  186. foreach ($value as &$value) {
  187. if ($value == null) {
  188. echo '.';
  189. } else {
  190. $type = $value->getType();
  191. if ($type == 'mouse') {
  192. echo 'M';
  193. } elseif ($type == 'cat') {
  194. echo 'C';
  195. }
  196. }
  197. }
  198. echo "\n";
  199. }
  200. }
  201.  
  202. class Mouse extends Animal
  203. {
  204. public $overview = 5;
  205. public $live =1;
  206. public function move($field)
  207. {
  208. if ($this->live == 1) {
  209. $cats = $field->getAllCats();
  210. $x = $this->x;
  211. $y = $this->y;
  212. foreach ($cats as &$value) {
  213. $catX = $value->getCoordinate('x');
  214. $catY = $value->getCoordinate('y');
  215. $distance = sqrt(pow($mouseX - $x, 2) + pow($mouseY - $y, 2));
  216. if ($distance <= $this->overview) {
  217. $vectorX = $vectorX + (($catX - $x) / pow($distance, 2));
  218. $vectorY = $vectorY + (($catX - $y) / pow($distance, 2));
  219. }
  220. }
  221. $vectorX = $vectorX*(-1);
  222. $vectorY = $vectorY*(-1);
  223. if ($vectorX < 0) {
  224. $x--;
  225. } elseif ($vectorX > 0) {
  226. $x++;
  227. }
  228. if ($vectorY < 0) {
  229. $y--;
  230. } elseif ($vectorY > 0) {
  231. $y++;
  232. }
  233. $field->updateCoordinates($this, $x, $y);
  234. }
  235. }
  236. public function dieMouse()
  237. {
  238. $this->live = 0;
  239. }
  240.  
  241. }
  242.  
  243. $field = new Field(7);
  244. $cat = new Cat('cat', 0, 0);
  245. $cat1 = new Cat ('cat', 4, 4);
  246. $mouse = new Mouse('mouse', 2, 2);
  247. $mouse1 = new Mouse ('mouse', 2, 3);
  248.  
  249. $field->add($cat);
  250. $field->add($cat1);
  251. $field->add($mouse);
  252. $field->add($mouse1);
  253.  
  254. $map = $field->getMap();
  255.  
  256. printField($map);
  257. echo "\n";
  258. function giveLive ($field, $step) {
  259. $map = $field->getMap();
  260. for ($i = 1; $i < $step; $i++) {
  261. $cats = $field->getAllCats();
  262. $mouses = $field->getMouses();
  263. foreach ($mouses as &$value) {
  264. $value->move($field);
  265. }
  266. foreach ($cats as &$value) {
  267. $value->move($field);
  268. }
  269. $map = $field->getMap();
  270. printField($map);
  271. echo "\n";
  272. }
  273. }
  274.  
  275. giveLive($field, 8);
Runtime error #stdin #stdout #stderr 0.01s 20568KB
stdin
Standard input is empty
stdout
C.......
........
..MM....
........
....C...
........
........
........

........
.C......
..M.M...
....C...
........
........
........
........

........
........
..C..C..
........
........
........
........
........

........
........
...C.C..
........
........
........
........
........

stderr
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: vectorX in /home/y63kBA/prog.php on line 218
PHP Notice:  Undefined variable: vectorY in /home/y63kBA/prog.php on line 219
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: vectorX in /home/y63kBA/prog.php on line 218
PHP Notice:  Undefined variable: vectorY in /home/y63kBA/prog.php on line 219
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: vectorX in /home/y63kBA/prog.php on line 218
PHP Notice:  Undefined variable: vectorY in /home/y63kBA/prog.php on line 219
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: vectorX in /home/y63kBA/prog.php on line 218
PHP Notice:  Undefined variable: vectorY in /home/y63kBA/prog.php on line 219
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: vectorX in /home/y63kBA/prog.php on line 218
PHP Notice:  Undefined variable: vectorY in /home/y63kBA/prog.php on line 219
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: vectorX in /home/y63kBA/prog.php on line 222
PHP Notice:  Undefined variable: vectorY in /home/y63kBA/prog.php on line 223
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: vectorX in /home/y63kBA/prog.php on line 218
PHP Notice:  Undefined variable: vectorY in /home/y63kBA/prog.php on line 219
PHP Notice:  Undefined variable: mouseX in /home/y63kBA/prog.php on line 216
PHP Notice:  Undefined variable: mouseY in /home/y63kBA/prog.php on line 216
PHP Fatal error:  Call to a member function move() on a non-object in /home/y63kBA/prog.php on line 265