<?php
abstract class Animal
{
    protected $x;
    protected $y;
    protected $icon;
    protected $field;
    
    public function __construct($coordinateX, $coordinateY, $symbol)
    {
        $this->x    = $coordinateX;
        $this->y    = $coordinateY;
        $this->icon = $symbol;
    }
    public function getCoordinateX()
    {
        return $this->x;
    }
    public function getCoordinateY()
    {
        return $this->y;
    }
    
    public function showCoordinates()
    {
        echo "Координата X =  [$this->x] ";
        echo "Координата Y = [$this->y]";
    }
    
    
    public function getIcon()
    {
        return $this->icon;
    }
    
    public function moveX($x)
    {
        if (($x < 0 && $this->x > 0) || ($x > 0 && $this->x < $this->field->shareHeight())) {
            $this->x = $this->x + $x;
        }
    }
    
    public function moveY($y)
    {
        if (($y < 0 && $this->y > 0) || ($y > 0 && $this->y < $this->field->shareWidth())) {
            $this->y = $this->y + $y;
        }
        
    }
    
    
    public function setField(GameField $field)
    {
        $this->field = $field;
    }
}

class Mouse extends Animal
{
    
    
    public function findAllCats()
    {
        $cats    = array();
        $animals = $this->field->shareAnimals();
        foreach ($animals as $animal) {
            if ($animal instanceof Cat && (abs($this->x - $animal->getCoordinateX()) <= 9 || abs($this->y - $animal->getCoordinateY()) <= 9)) {
                $cats[] = $animal;
            }
        }
        return $cats;
    }
    
    
    #Cупер тупой костыль, но я не знаю как извлечь наибольшую переменную из 4, т.к max возвращает только значение
    
    public function giveMax($up, $down, $left, $right)
    {
        $array = array(
            'up' => $up,
            'down' => $down,
            'left' => $left,
            'right' => $right
        );
        $temp  = 0;
        foreach ($array as $key => $value) {
            if ($value > $temp) {
                $temp   = $value;
                $string = $key;
            }
        }
        return $string;
        
    }
    
    public function makeMove()
    {
        
        $cats  = $this->findAllCats();
        $up    = INF;
        $down  = INF;
        $left  = INF;
        $right = INF;
        
        foreach ($cats as $cat) {
            if ($cat->getCoordinateX() > $this->x && $cat->getCoordinateX() - $this->x < $down) {
                $down = $cat->getCoordinateX() - $this->x;
                
            }
            if ($cat->getCoordinateX() < $this->x && abs($cat->getCoordinateX() - $this->x) < $up) {
                $up = abs($cat->getCoordinateX() - $this->x);
                
            }
            
            if ($cat->getCoordinateY() > $this->y && $cat->getCoordinateY() - $this->y < $right) {
                $right = $cat->getCoordinateY() - $this->y;
                
            }
            
            if ($cat->getCoordinateY() < $this->y && abs($cat->getCoordinateY() - $this->y) < $left) {
                $left = abs($cat->getCoordinateY() - $this->y);
                
            }
            
            if ($this->x == $this->field->shareHeight()) {
                $down = 0;
            }
            
            if ($this->x == 0) {
                $up = 0;
            }
            
            if ($this->y == $this->field->shareWidth()) {
                $right = 0;
            }
            
            
            if ($this->y == 0) {
                $left = 0;
            }
            
            
            
            #еще больше костылей
            
            $result = $this->giveMax($up, $down, $left, $right);
            
            
            
            switch ($result) {
                case 'up':
                    $this->moveX(-1);
                    break;
                case 'down':
                    $this->moveX(1);
                    break;
                case 'left':
                    $this->moveY(-1);
                    break;
                case 'right':
                    $this->moveY(1);
                    break;
            }
            
        }
        
        
        
    }
    
}






class Cat extends Animal
{
    protected $sleepCount = 0;
    protected $moveCount = 0;
    
    
    public function getIcon()
    {
        if ($this->sleepCount == 0) {
            return parent::getIcon();
        } else {
            return "@";
        }
    }
    
    
    
    
    public function fallAsleep()
    {
        $this->sleepCount = 1;
        
        $this->moveCount = 0;
    }
    
    public function move($x, $y)
    {
        
        if ($this->sleepCount == 0) {
            
            $target = $this->findClosestMouse();
            
            $this->x = $this->x + $x;
            $this->y = $this->y + $y;
            $this->moveCount++;
            if ($this->moveCount == 8) {
                $this->fallAsleep();
                
            }
            $tile = $this->field->checkTile($this->x, $this->y);
            
            if ($tile instanceof Mouse) {
                $this->field->killMouse($tile->getCoordinateX(), $tile->getCoordinateY());
                $this->fallAsleep();
                
            }
        }
        
        
        else {
            $this->sleepCount--;
            
        }
        
    }
    
    public function makeMove()
    {
        $x      = 0;
        $y      = 0;
        $target = $this->findClosestMouse();
        if ($target) {
            if ($target->getCoordinateX() > $this->x) {
                $x = 1;
            } elseif ($target->getCoordinateX() < $this->x) {
                $x = -1;
            }
            
            if ($target->getCoordinateY() > $this->y) {
                $y = 1;
            } elseif ($target->getCoordinateY() < $this->y) {
                $y = -1;
            }
            
            $this->move($x, $y);
        }
    }
    
    
    public function findClosestMouse()
    {
        $previous = INF;
        
        foreach ($this->field->shareAnimals() as $animal) {
            if (!($animal instanceof Mouse)) {
                continue;
            }
            $distance = abs($this->x - $animal->getCoordinateX()) + abs($this->y - $animal->getCoordinateY());
            if ($distance < $previous) {
                $target   = $animal;
                $previous = $distance;
            }
            
        }
        
        return $target;
    }
    
    
}


class GameField
{
    protected $field;
    protected $animals = array();
    protected $height;
    protected $width;
    
    public function __construct($height, $width)
    {
        $this->height = $height;
        $this->width  = $width;
    }
    public function printField()
    {
        $this->field = array_fill(0, $this->height, array_fill(0, $this->width, " . "));
        
        foreach ($this->animals as $animal) {
            $x                   = $animal->getCoordinateX();
            $y                   = $animal->getCoordinateY();
            $this->field[$x][$y] = $animal->getIcon();
        }
        
        foreach ($this->field as $value) {
            foreach ($value as $dot) {
                echo $dot;
            }
            echo "\n";
        }
    }
    
    public function acquireAnimal(Animal $animal)
    {
        $this->animals[] = $animal;
        $animal->setField($this);
    }
    
    public function shareAnimals()
    {
        return $this->animals;
    }
    
    public function shareHeight()
    {
        return $this->height - 1;
    }
    
    public function shareWidth()
    {
        return $this->width - 1;
    }
    
    public function killMouse($x, $y)
    {
        
        for ($i = 0; $i < count($this->animals); $i++) {
            
            if ($this->animals[$i] instanceof Mouse && $this->animals[$i]->getCoordinateX() == $x && $this->animals[$i]->getCoordinateY() == $y) {
                unset($this->animals[$i]);
            }
        }
        
    }
    
    public function checkTile($x, $y)
    {
        
        foreach ($this->animals as $animal) {
            if ($animal->getCoordinateX() == $x && $animal->getCoordinateY() == $y) {
                return $animal;
            }
            
        }
        return false;
    }
    
    
    
    public function testTheKitty()
    {
        
        foreach ($this->animals as $animal) {
            if ($animal instanceof Cat) {
                $animal->makeMove();
                
                
                
            }
            
            
        }
        foreach ($this->animals as $animal) {
            if ($animal instanceof Mouse) {
                $animal->makeMove();
                
                
            }
            
        }
        
        
        
        
    }
}


$test   = new GameField(20, 20);
$mouse1 = new Mouse(3, 15, "1");
$mouse2 = new Mouse(5, 7, "2");
$kitty  = new Cat(19, 10, "K");

$test->acquireAnimal($mouse1);
$test->acquireAnimal($mouse2);
$test->acquireAnimal($kitty);
$test->printField();
echo "\n";
for ($i = 0; $i < 23; $i++) {
    $test->testTheKitty();
    $test->printField();
    echo "\n";
    
    
}