<?php
abstract class Animals
{
public $height;
public $width;
public $position;
public function __construct($height, $width)
{
$this->height = $height;
$this->width = $width;
$this->position = $width + $height;
}
protected function hold()
{
$this->height = $this->height;
$this->width = $this->width;
}
protected function moveUp()
{
$this->height = $this->height - 1;
$this->width = $this->width;
}
protected function moveDown()
{
$this->height = $this->height + 1;
$this->width = $this->width;
}
protected function moveLeft()
{
$this->height = $this->height;
$this->width = $this->width - 1;
}
protected function moveRight()
{
$this->height = $this->height;
$this->width = $this->width + 1;
}
}
class Mouse extends Animals
{
}
class Cat extends Animals
{
}
class MovePattern
{
static public function makeMove($field)
{
self::mouseMoveSolution(($field->getMouses()),($field->getCats()));
}
static private function mouseMoveSolution($mouses, $cats)
{
foreach($mouses as $mouse)
{
$i = 0;
$mousePosition = $mouse->position;
foreach($cats as $cat)
{
$catPositions[$i] = $cat->position;
$i++;
}
var_dump(self::searchNearest($mousePosition,$catPositions)); }
}
static private function searchNearest ($value, $inArray)
{
$lastKey = null;
$lastDif = null;
foreach ($inArray as $k => $v)
{
if ($v == $value)
{
return $v;
}
$dif = abs ($value - $v); if (is_null($lastKey) || $dif < $lastDif) {
$lastKey = $v;
$lastDif = $dif;
}
}
return $lastKey;
}
}
class Start
{
public $field;
public $fieldHeight = 15;
public $fieldWidth = 15;
public function __construct($mouseQuantity, $catQuantity)
{
for ($i = 0; $i < $mouseQuantity; $i++)
{
$this->field[] = new Mouse
(mt_rand(0,$this->fieldHeight), mt_rand(0,$this->fieldWidth)); }
for ($i = 0; $i < $catQuantity; $i++)
{
$this->field[] = new Cat
(mt_rand(0,$this->fieldHeight), mt_rand(0,$this->fieldWidth)); }
}
public function getMouses()
{
foreach($this->field as $animal)
{
{
$mouses[] = $animal;
}
}
return $mouses;
}
public function getCats()
{
foreach($this->field as $animal)
{
{
$cats[] = $animal;
}
}
return $cats;
}
}
MovePattern::makeMove((new Start(3, 2)));