fork download
  1. <?php
  2.  
  3.  
  4. class Field {
  5. private $size;
  6. public $mapOfField = array();
  7. private $numberOfCats = 0;
  8. private $cats = array();
  9. private $numberOfMouses = 0;
  10. private $mouses = array();
  11. public function __construct ($size)
  12. {
  13. $this->size = $size;
  14. $string = array();
  15. for ($a = 0; $a <= $size; $a++) {
  16. $string[$a] = null;
  17. }
  18. for ($i = 0; $i <= $size; $i++) {
  19. $this->mapOfField[$i] = $string;
  20. }
  21. }
  22. public function add ($animal)
  23. {
  24. $type = $animal->getType();
  25. if ($type == 'mouse') {
  26. $this->mouses[$this->numberOfMouses] = $animal;
  27. $animal->setNumber($this->numberOfMouses);
  28. $this->numberOfMouses++;
  29. } elseif ($type == 'cat') {
  30. $this->cats[$this->numberOfCats] = $animal;
  31. $animal->setNumber($this->numberOfCats);
  32. $this->numberOfCats++;
  33. }
  34. $x = $animal->getCoordinate('x');
  35. $y = $animal->getCoordinate('y');
  36. $this->mapOfField[$x][$y] = $animal;
  37. }
  38. public function delete ($animal)
  39. {
  40. $number = $animal->getNumber();
  41. $this->mouses[$number] = array_pop($this->mouses);
  42. }
  43. public function getMouses ()
  44. {
  45. return $this->mouses;
  46. }
  47. public function getSize ()
  48. {
  49. return $this->size;
  50. }
  51. public function updateCoordinates ($animal, $x, $y)
  52. {
  53. $type = $animal->getType();
  54. if (($x <= $this->size && $x >= 0) && ($y <= $this->size && $y >= 0)) {
  55. if ($type == 'cat') {
  56. $cell = $this->mapOfField[$x][$y];
  57. if (($cell == null) || (($aimType = $cell->getType) == 'mouse')) {
  58. $startX = $animal->getCoordinate('x');
  59. $startY = $animal->getCoordinate('y');
  60. $this->mapOfField[$startX][$startY] = null;
  61. $animal->setCoordinate ('x',$x);
  62. $animal->setCoordinate ('y',$y);
  63. $this->mapOfField[$x][$y] = $animal;
  64. }
  65. } elseif ($type == 'mouse') {
  66. $cell = $this->mapOfField[$x][$y];
  67. if ($cell == null) {
  68. $startX = $animal->getCoordinate('x');
  69. $startY = $animal->getCoordinate('y');
  70. $this->mapOfField[$startX][$startY] = null;
  71. $animal->setCoordinate ('x',$x);
  72. $animal->setCoordinate ('y',$y);
  73. $this->mapOfField[$x][$y] = $animal;
  74. }
  75. }
  76. }
  77. }
  78. }
  79.  
  80. class Animals {
  81. protected $type;
  82. protected $number;
  83. protected $x;
  84. protected $y;
  85. public function __construct ($type, $x, $y)
  86. {
  87. $this->type = $type;
  88. $this->x = $x;
  89. $this->y = $y;
  90. }
  91. public function getCoordinate ($orXY)
  92. {
  93. if ($orXY == 'x') {
  94. return $this->x;
  95. } elseif ($orXY == 'y') {
  96. return $this->y;
  97. }
  98. }
  99. public function setCoordinate ($orXY, $coordinate)
  100. {
  101. if ($orXY == 'x') {
  102. $this->x = $coordinate;
  103. } elseif ($orXY == 'y') {
  104. $this->y = $coordinate;
  105. }
  106. }
  107. public function setNumber ($number)
  108. {
  109. $this->number = $number;
  110. }
  111. public function getType ()
  112. {
  113. return $this->type;
  114. }
  115. }
  116.  
  117. class Cats extends Animals {
  118. private $hungry = 1;
  119. public function __construct ($type, $x, $y)
  120. {
  121. parent::__construct($type, $x, $y);
  122. }
  123. public function move ($field)
  124. {
  125. $x = $this->x;
  126. $y = $this->y;
  127. $min = $field->getSize();
  128. if ($this->hungry == 1) {
  129. $mouses = $field->getMouses();
  130. foreach ($mouses as &$value) {
  131. $mouseX = $value->getCoordinate('x');
  132. $mouseY = $value->getCoordinate('y');
  133. $distance = sqrt(pow($mouseX - $x, 2) + pow($mouseY - $y, 2));
  134. if ($distance <= $min) {
  135. $aimAnimal = $value;
  136. $aimX = $mouseX;
  137. $aimY = $mouseY;
  138. }
  139. }
  140. if ($x < $aimX) {
  141. $x++;
  142. } elseif ($x > $aimX) {
  143. $x--;
  144. } else {
  145. if ($y == $aimY) {
  146. $this->hungry = 0;
  147. $field->delete($aimAnimal);
  148. }
  149. }
  150. if ($y < $aimY) {
  151. $y++;
  152. } elseif ($y > $aimY) {
  153. $y--;
  154. } else {
  155. if ($x == $aimX) {
  156. $this->hungry = 0;
  157. $field->delete($aimAnimal);
  158. }
  159. }
  160. $field->updateCoordinates($this, $x, $y);
  161. } elseif ($this->hungry ==0) {
  162. $this->hungry = 1;
  163. }
  164. }
  165. }
  166.  
  167. function printField($array)
  168. {
  169. foreach ($array as &$value) {
  170. foreach ($value as &$value) {
  171. if ($value == null) {
  172. echo '.';
  173. } else {
  174. $type = $value->getType();
  175. if ($type == 'mouse') {
  176. echo 'M';
  177. } elseif($type == 'cat') {
  178. echo 'C';
  179. }
  180. }
  181. }
  182. echo "\n";
  183. }
  184. }
  185.  
  186. class Mouses extends Animals
  187. {
  188. public $overview = 5;
  189. function __construct($name, $x, $y)
  190. {
  191. parent::__construct($name, $x, $y);
  192. }
  193. }
  194.  
  195. $field = new Field(7);
  196. $cat = new Cats ('cat', 0, 0);
  197. $mouse = new Mouses ('mouse', 2, 2);
  198.  
  199. $field->add($cat);
  200. $field->add($mouse);
  201.  
  202. printField($field->mapOfField);
  203. echo "\n";
  204.  
  205. for ($i = 1; $i < 5; $i++) {
  206. $cat->move($field);
  207. printField($field->mapOfField);
  208. echo "\n";
  209. }
Success #stdin #stdout #stderr 0.01s 20568KB
stdin
Standard input is empty
stdout
C.......
........
..M.....
........
........
........
........
........

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

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

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

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

stderr
PHP Notice:  Undefined property: Mouses::$getType in /home/vP5yEB/prog.php on line 58
PHP Notice:  Undefined property: Mouses::$getType in /home/vP5yEB/prog.php on line 58
PHP Notice:  Undefined property: Mouses::$getType in /home/vP5yEB/prog.php on line 58