fork download
  1. <?php
  2. abstract class Animal
  3. {
  4. protected $x;
  5. protected $y;
  6. protected $icon;
  7. protected $field;
  8.  
  9. public function __construct($coordinateX, $coordinateY, $symbol)
  10. {
  11. $this->x = $coordinateX;
  12. $this->y = $coordinateY;
  13. $this->icon = $symbol;
  14. }
  15. public function getCoordinateX()
  16. {
  17. return $this->x;
  18. }
  19. public function getCoordinateY()
  20. {
  21. return $this->y;
  22. }
  23.  
  24. public function showCoordinates()
  25. {
  26. echo "Координата X = [$this->x] ";
  27. echo "Координата Y = [$this->y]";
  28. }
  29.  
  30.  
  31. public function getIcon()
  32. {
  33. return $this->icon;
  34. }
  35.  
  36. public function moveX($x)
  37. {
  38. if (($this->field->isXOnMap($this->x + $x)) && !$this->field->checkTile($this->x + $x, $this->y)) {
  39. $this->x = $this->x + $x;
  40. }
  41. }
  42.  
  43. public function moveY($y)
  44. {
  45. if (($this->field->isYOnMap($this->y + $y)) && !$this->field->checkTile($this->x, $this->y + $y)) {
  46. $this->y = $this->y + $y;
  47. }
  48.  
  49. }
  50.  
  51.  
  52. public function setField(GameField $field)
  53. {
  54. $this->field = $field;
  55. }
  56.  
  57. public function unsetField()
  58. {
  59. $this->field = null;
  60. }
  61. }
  62.  
  63. class Mouse extends Animal
  64. {
  65.  
  66.  
  67. public function findAllCats()
  68. {
  69. $cats = array();
  70. $animals = $this->field->shareAnimals();
  71. foreach ($animals as $animal) {
  72. if ($animal instanceof Cat && (abs($this->x - $animal->getCoordinateX()) <= 9 || abs($this->y - $animal->getCoordinateY()) <= 9)) {
  73. $cats[] = $animal;
  74. }
  75. }
  76. return $cats;
  77. }
  78.  
  79.  
  80.  
  81. public function defineAllAvaibleTurns()
  82. {
  83. $ways = array(
  84. 'up' => array(
  85. 'x' => 1,
  86. 'y' => 0,
  87. 'score' => INF
  88. ),
  89. 'down' => array(
  90. 'x' => -1,
  91. 'y' => 0,
  92. 'score' => INF
  93. ),
  94. 'left' => array(
  95. 'x' => 0,
  96. 'y' => -1,
  97. 'score' => INF
  98. ),
  99. 'right' => array(
  100. 'x' => 0,
  101. 'y' => 1,
  102. 'score' => INF
  103. )
  104.  
  105.  
  106.  
  107. );
  108. $list = array(
  109. 'stand' => array(
  110. 'x' => 0,
  111. 'y' => 0,
  112. 'score' => 1
  113. )
  114. );
  115.  
  116. foreach ($ways as $key => $value) {
  117.  
  118. if ($this->field->isXOnMap($this->x + $value['x']) && $this->field->isYOnMap($this->y + $value['y']) && !$this->field->checkTile($this->x + $value['x'], $this->y + $value['y'])) {
  119. $list[$key] = $value;
  120.  
  121.  
  122. }
  123. }
  124.  
  125. return $list;
  126.  
  127. }
  128.  
  129. public function calculateScore($list)
  130. {
  131. $cats = $this->findAllCats();
  132.  
  133.  
  134.  
  135.  
  136. $previousUp = INF;
  137. $previousDown = INF;
  138. $previousRight = INF;
  139. $previousLeft = INF;
  140. foreach ($cats as $cat) {
  141. $differenceX = $this->x - $cat->getCoordinateX();
  142. $differenceY = $this->y - $cat->getCoordinateY();
  143.  
  144.  
  145. if (abs($differenceX) > abs($differenceY) && $differenceX < 0 && abs($differenceX) < $previousUp && array_key_exists('up', $list)) {
  146. $list['up']['score'] = abs($differenceX);
  147. $previousUp = abs($differenceX);
  148.  
  149. } elseif (abs($differenceX) > abs($differenceY) && $differenceX > 0 && abs($differenceX) < $previousDown && array_key_exists('down', $list)) {
  150. $list['down']['score'] = abs($differenceX);
  151. $previousDown = abs($differenceX);
  152. } elseif (abs($differenceX) < abs($differenceY) && $differenceY < 0 && abs($differenceY) < $previousRight && array_key_exists('right', $list)) {
  153. $list['right']['score'] = abs($differenceY);
  154. $previousRight = abs($differenceY);
  155.  
  156. } elseif (abs($differenceX) < abs($differenceY) && $differenceY > 0 && abs($differenceY) < $previousLeft && array_key_exists('left', $list)) {
  157. $list['left']['score'] = abs($differenceY);
  158. $previousLeft = abs($differenceY);
  159. }
  160.  
  161.  
  162. }
  163.  
  164.  
  165. return $list;
  166. }
  167.  
  168. public function chooseBestTurn()
  169. {
  170. $list = $this->calculateScore($this->defineAllAvaibleTurns());
  171. $turn = 0;
  172. $previous = 0;
  173. foreach ($list as $key => $value) {
  174. if ($value['score'] > $previous) {
  175. $turn = $value;
  176. }
  177. }
  178.  
  179.  
  180. return $turn;
  181.  
  182. }
  183.  
  184. public function makeMove()
  185. {
  186.  
  187.  
  188. $turn = $this->chooseBestTurn();
  189.  
  190. $this->moveX($turn['x']);
  191. $this->moveY($turn['y']);
  192.  
  193.  
  194.  
  195. }
  196.  
  197.  
  198.  
  199. }
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. class Cat extends Animal
  207. {
  208. protected $sleepCount = 0;
  209. protected $moveCount = 0;
  210.  
  211.  
  212. public function getIcon()
  213. {
  214. if ($this->sleepCount == 0) {
  215. return parent::getIcon();
  216. } else {
  217. return "@";
  218. }
  219. }
  220.  
  221.  
  222.  
  223.  
  224. public function fallAsleep()
  225. {
  226. $this->sleepCount = 1;
  227.  
  228. $this->moveCount = 0;
  229. }
  230.  
  231. public function move($x, $y)
  232. {
  233.  
  234. if ($this->sleepCount == 0) {
  235.  
  236.  
  237. if (!($this->field->checkTile($this->x + $x, $this->y + $y) instanceof Cat)) {
  238.  
  239. $this->x = $this->x + $x;
  240. $this->y = $this->y + $y;
  241. $this->moveCount++;
  242. }
  243.  
  244.  
  245.  
  246. if ($this->moveCount == 8) {
  247. $this->fallAsleep();
  248.  
  249. }
  250. $tile = $this->field->checkTile($this->x, $this->y);
  251.  
  252. if ($tile instanceof Mouse) {
  253. $this->field->killMouse($tile->getCoordinateX(), $tile->getCoordinateY());
  254. $this->fallAsleep();
  255.  
  256. }
  257. }
  258.  
  259.  
  260. else {
  261. $this->sleepCount--;
  262.  
  263. }
  264.  
  265. }
  266.  
  267. public function makeMove()
  268. {
  269. $x = 0;
  270. $y = 0;
  271. $target = $this->findClosestMouse();
  272. if ($target) {
  273. if ($target->getCoordinateX() > $this->x) {
  274. $x = 1;
  275. } elseif ($target->getCoordinateX() < $this->x) {
  276. $x = -1;
  277. }
  278.  
  279. if ($target->getCoordinateY() > $this->y) {
  280. $y = 1;
  281. } elseif ($target->getCoordinateY() < $this->y) {
  282. $y = -1;
  283. }
  284.  
  285. $this->move($x, $y);
  286. }
  287. }
  288.  
  289.  
  290. public function findClosestMouse()
  291. {
  292. $previous = INF;
  293. $target = 0;
  294. foreach ($this->field->shareAnimals() as $animal) {
  295. if (!($animal instanceof Mouse)) {
  296. continue;
  297. }
  298. $distance = abs($this->x - $animal->getCoordinateX()) + abs($this->y - $animal->getCoordinateY());
  299. if ($distance < $previous) {
  300. $target = $animal;
  301. $previous = $distance;
  302. }
  303.  
  304. }
  305. if ($target) {
  306. return $target;
  307. }
  308. }
  309.  
  310.  
  311. }
  312.  
  313.  
  314. class GameField
  315. {
  316. protected $field;
  317. protected $animals = array();
  318. protected $height;
  319. protected $width;
  320.  
  321. public function __construct($height, $width)
  322. {
  323. $this->height = $height;
  324. $this->width = $width;
  325. }
  326. public function printField()
  327. {
  328. $this->field = array_fill(0, $this->height, array_fill(0, $this->width, " . "));
  329.  
  330. foreach ($this->animals as $animal) {
  331. $x = $animal->getCoordinateX();
  332. $y = $animal->getCoordinateY();
  333. $this->field[$x][$y] = $animal->getIcon();
  334. }
  335.  
  336. foreach ($this->field as $value) {
  337. foreach ($value as $dot) {
  338. echo $dot;
  339. }
  340. echo "\n";
  341. }
  342. }
  343.  
  344. public function acquireAnimal(Animal $animal)
  345. {
  346. $this->animals[] = $animal;
  347. $animal->setField($this);
  348. }
  349.  
  350. public function shareAnimals()
  351. {
  352. return $this->animals;
  353. }
  354.  
  355. public function shareHeight()
  356. {
  357. return $this->height - 1;
  358. }
  359.  
  360. public function shareWidth()
  361. {
  362. return $this->width - 1;
  363. }
  364.  
  365. public function killMouse($x, $y)
  366. {
  367.  
  368. for ($i = 0; $i < count($this->animals); $i++) {
  369.  
  370. if ($this->animals[$i] instanceof Mouse && $this->animals[$i]->getCoordinateX() == $x && $this->animals[$i]->getCoordinateY() == $y) {
  371. $this->animals[$i]->unsetField();
  372. unset($this->animals[$i]);
  373. }
  374. }
  375.  
  376. }
  377.  
  378. public function checkTile($x, $y)
  379. {
  380.  
  381. foreach ($this->animals as $animal) {
  382. if ($animal->getCoordinateX() == $x && $animal->getCoordinateY() == $y) {
  383. return $animal;
  384. }
  385.  
  386. }
  387. return false;
  388. }
  389.  
  390. public function isXOnMap($x)
  391. {
  392. if ($x < 0 || $x > ($this->height - 1)) {
  393. return false;
  394. } else {
  395. return true;
  396. }
  397. }
  398.  
  399. public function isYOnMap($y)
  400. {
  401. if ($y < 0 || $y > ($this->width - 1)) {
  402. return false;
  403. } else {
  404. return true;
  405. }
  406. }
  407.  
  408. public function testTheKitty()
  409. {
  410.  
  411. foreach ($this->animals as $animal) {
  412. if ($animal instanceof Cat) {
  413. $animal->makeMove();
  414.  
  415.  
  416.  
  417. }
  418.  
  419.  
  420. }
  421. foreach ($this->animals as $animal) {
  422. if ($animal instanceof Mouse) {
  423. $animal->makeMove();
  424.  
  425.  
  426. }
  427.  
  428. }
  429.  
  430.  
  431.  
  432.  
  433. }
  434. }
  435.  
  436.  
  437. $test = new GameField(7, 7);
  438. $mouse1 = new Mouse(3, 1, "1");
  439. $mouse2 = new Mouse(5, 2, "2");
  440. $kitty = new Cat(6, 6, "K");
  441. $kitty2 = new Cat(5, 5, "k");
  442.  
  443. $test->acquireAnimal($mouse1);
  444. $test->acquireAnimal($mouse2);
  445. $test->acquireAnimal($kitty);
  446. $test->acquireAnimal($kitty2);
  447. $test->printField();
  448. echo "\n";
  449. for ($i = 0; $i < 40; $i++) {
  450. $test->testTheKitty();
  451. $test->printField();
  452. echo "\n";
  453.  
  454.  
  455. }
Success #stdin #stdout #stderr 0.01s 20568KB
stdin
Standard input is empty
stdout
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 . 1 .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  . 2 .  . k . 
 .  .  .  .  .  . K

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

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  . 1 .  .  . 
 .  .  .  .  .  .  . 
 .  .  . @ . K . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . 1 .  . 
 .  .  .  . K .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  .  .  .  . 
 .  .  .  . @ .  . 
 .  .  .  .  .  .  . 
 .  .  . k .  .  . 
 .  .  .  .  .  .  . 

stderr
PHP Notice:  Undefined offset: 1 in /home/n46k2E/prog.php on line 370