fork download
  1. <?php
  2.  
  3.  
  4. abstract class Animals
  5. {
  6. public $height;
  7. public $width;
  8. public $position;
  9.  
  10. public function __construct($height, $width)
  11. {
  12. $this->height = $height;
  13. $this->width = $width;
  14. $this->position = $width + $height;
  15. }
  16.  
  17. protected function hold()
  18. {
  19. $this->height = $this->height;
  20. $this->width = $this->width;
  21. }
  22.  
  23. protected function moveUp()
  24. {
  25. $this->height = $this->height - 1;
  26. $this->width = $this->width;
  27. }
  28.  
  29. protected function moveDown()
  30. {
  31. $this->height = $this->height + 1;
  32. $this->width = $this->width;
  33. }
  34.  
  35. protected function moveLeft()
  36. {
  37. $this->height = $this->height;
  38. $this->width = $this->width - 1;
  39. }
  40.  
  41. protected function moveRight()
  42. {
  43. $this->height = $this->height;
  44. $this->width = $this->width + 1;
  45. }
  46. }
  47.  
  48. class Mouse extends Animals
  49. {
  50.  
  51. }
  52.  
  53. class Cat extends Animals
  54. {
  55.  
  56. }
  57.  
  58. class MovePattern
  59. {
  60. static public function makeMove($field)
  61. {
  62. self::mouseMoveSolution(($field->getMouses()),($field->getCats()));
  63. }
  64. static private function mouseMoveSolution($mouses, $cats)
  65. {
  66. foreach($mouses as $mouse)
  67. {
  68. $i = 0;
  69. $mousePosition = $mouse->position;
  70. foreach($cats as $cat)
  71. {
  72. $catPositions[$i] = $cat->position;
  73. $i++;
  74. }
  75. var_dump($catPositions);
  76. var_dump($mousePosition);
  77. var_dump(self::searchNearest($mousePosition,$catPositions));
  78. }
  79. }
  80. static private function searchNearest ($value, $inArray)
  81. {
  82. $lastKey = null;
  83. $lastDif = null;
  84. foreach ($inArray as $k => $v)
  85. {
  86. if ($v == $value)
  87. {
  88. return $v;
  89. }
  90. $dif = abs ($value - $v);
  91. if (is_null($lastKey) || $dif < $lastDif)
  92. {
  93. $lastKey = $v;
  94. $lastDif = $dif;
  95. }
  96. }
  97. return $lastKey;
  98. }
  99. }
  100.  
  101. class Start
  102. {
  103. public $field;
  104. public $fieldHeight = 15;
  105. public $fieldWidth = 15;
  106.  
  107. public function __construct($mouseQuantity, $catQuantity)
  108. {
  109. for ($i = 0; $i < $mouseQuantity; $i++)
  110. {
  111. $this->field[] = new Mouse(mt_rand(0,$this->fieldHeight), mt_rand(0,$this->fieldWidth));
  112. }
  113.  
  114. for ($i = 0; $i < $catQuantity; $i++)
  115. {
  116. $this->field[] = new Cat(mt_rand(0,$this->fieldHeight), mt_rand(0,$this->fieldWidth));
  117. }
  118. }
  119. public function getMouses()
  120. {
  121. foreach($this->field as $animal)
  122. {
  123. if(get_class($animal) == 'Mouse')
  124. {
  125. $mouses[] = $animal;
  126. }
  127. }
  128. return $mouses;
  129. }
  130. public function getCats()
  131. {
  132. foreach($this->field as $animal)
  133. {
  134. if(get_class($animal) == 'Cat')
  135. {
  136. $cats[] = $animal;
  137. }
  138. }
  139. return $cats;
  140. }
  141. }
  142.  
  143.  
  144. MovePattern::makeMove((new Start(3, 2)));
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
array(2) {
  [0]=>
  int(7)
  [1]=>
  int(19)
}
int(16)
int(19)
array(2) {
  [0]=>
  int(7)
  [1]=>
  int(19)
}
int(12)
int(7)
array(2) {
  [0]=>
  int(7)
  [1]=>
  int(19)
}
int(20)
int(19)