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

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

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

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

........
........
..M.CC..
........
........
........
........
........

........
........
..M.CC..
........
........
........
........
........

........
........
..M.CC..
........
........
........
........
........

........
........
..M.CC..
........
........
........
........
........

stderr
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 215
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 215
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 215
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 215
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 215
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 219
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 220
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 215
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 219
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 220
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 215
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 219
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 220
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 215
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 219
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 220
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 215
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 216
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseX in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: mouseY in /home/iSNvb0/prog.php on line 213
PHP Notice:  Undefined variable: vectorX in /home/iSNvb0/prog.php on line 219
PHP Notice:  Undefined variable: vectorY in /home/iSNvb0/prog.php on line 220