fork download
  1. <?php
  2.  
  3. abstract class Animal
  4. {
  5. public $x;
  6. public $y;
  7.  
  8. public function __construct()
  9. {
  10. $this->x = mt_rand(1, Field::WIDTH);
  11. $this->y = mt_rand(1, Field::HEIGHT);
  12. }
  13. }
  14.  
  15. class Mouse extends Animal
  16. {
  17. const NAME = 'm';
  18.  
  19. private function getDangers($field)
  20. {
  21. $yClone = ($this->y + 4 > Field::HEIGHT) ? $this->y + ($this->y + 4 - Field::HEIGHT) : $this->y + 4;
  22. $j = ($this->y - 4 < 0) ? $this->y - 4 : 0;
  23.  
  24. $i = ($this->x - 4 < 0) ? $this->x - 4 : 0;
  25. $xClone = ($this->x + 4 > Field::HEIGHT) ? $this->x + ($this->x + 4 - Field::HEIGHT) : $this->x + 4;
  26.  
  27. for ($j; $j < $yClone; $j++) {
  28. for ($i; $i < $xClone; $i++) {
  29. if ($field[$j][$i] == Cat::NAME) {
  30. $dangers[] = array($j, $i);
  31. }
  32. }
  33. }
  34.  
  35. return $dangers;
  36. }
  37.  
  38. public function getPossibleSteps($field)
  39. {
  40. $dangers = $this->getDangers($field);
  41.  
  42. $possibleSteps[] = $field[$this->y - 1][$this->x];
  43. $possibleSteps[] = $field[$this->y][$this->x + 1];
  44. $possibleSteps[] = $field[$this->y + 1][$this->x];
  45. $possibleSteps[] = $field[$this->y][$this->x - 1];
  46.  
  47. foreach ($possibleSteps as $step) {
  48. foreach ($dangers as $danger) {
  49. if ($step == $danger) {
  50. unset($dangers[$danger]);
  51. sort($dangers);
  52. }
  53. }
  54. }
  55. return $possibleSteps;
  56. }
  57. }
  58.  
  59. class Cat extends Animal
  60. {
  61. const NAME = 'C';
  62.  
  63. }
  64.  
  65. class Field
  66. {
  67. const HEIGHT = 8;
  68. const WIDTH = 16;
  69.  
  70. public $field;
  71.  
  72. public static function getField()
  73. {
  74. for ($y = 0; $y < self::HEIGHT; $y++) {
  75. for ($x = 0; $x < self::WIDTH; $x++) {
  76. $field[$y][$x] = ".";
  77. }
  78. }
  79.  
  80. return $field;
  81. }
  82.  
  83. public function isCorner($x, $y)
  84. {
  85. if ($x == self::WIDTH && $y == self::HEIGHT) {
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. }
  92.  
  93. class Game
  94. {
  95.  
  96. }
  97.  
  98. $field = new Field;
  99.  
  100. $m1 = new Mouse;
  101.  
  102. foreach (Field::getField() as $string) {
  103. echo implode("", $string)."\n";
  104. }
  105.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
................
................
................
................
................
................
................
................