<?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)
    {
        $this->x = $this->x + $x;
    }
    
    public function moveY($y)
    {
        $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;
    }
    
    
    
    //временный код для одной кошки на поле:
    
    public function makeMove()
    {
        
        $cats = $this->findAllCats();
        if ($cats[0]) {
            $x = $cats[0]->getCoordinateX();
            $y = $cats[0]->getCoordinateY();
        }
        
        
        if ($this->x < $x && $this->x != 0 && !$this->field->checkTile($this->x - 1, $this->y)) {
            $this->moveX(-1);
            
        }
        
        elseif ($this->x > $x && $this->x != 19 && !$this->field->checkTile($this->x + 1, $this->y)) {
            $this->moveX(1);
        } elseif ($this->y < $y && $this->y != 0 && !$this->field->checkTile($this->x, $this->y - 1)) {
            $this->moveY(-1);
        } elseif ($this->y > $y && $this->y != 19 && !$this->field->checkTile($this->x, $this->y + 1)) {
            $this->moveY(1);
        } elseif ($this->x == $x && $this->x != 0 && $this->x != 19) {
            if (19 - $this->x > $this->x) {
                $this->moveX(1);
            } else {
                $this->moveX(-1);
            }
        } elseif ($this->y == $y && $this->y != 0 && $this->y != 19) {
            if (19 - $this->y > $this->y) {
                $this->y = $this->y + 1;
            } else {
                $this->y = $this->y - 1;
            }
        }
        
    }
    
}




class Cat extends Animal
{
    protected $sleepCount = 0;
    protected $moveCount = 0;
    protected $previousIcon;
    
    public function setSleepIcon()
    {
        $this->previousIcon = $this->icon;
        $this->icon         = "@";
    }
    
    public function setDefauldIcon()
    {
        $this->icon = $this->previousIcon;
    }
    public function fallAsleep()
    {
        $this->sleepCount = 1;
        $this->setSleepIcon();
        $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--;
            $this->setDefauldIcon();
        }
        
    }
    
    public function makeMove()
    {
        $x      = 0;
        $y      = 0;
        $target = $this->findClosestMouse();
        if ($target['x'] > $this->x) {
            $x = 1;
        } elseif ($target['x'] < $this->x) {
            $x = -1;
        }
        
        if ($target['y'] > $this->y) {
            $y = 1;
        } elseif ($target['y'] < $this->y) {
            $y = -1;
        }
        
        $this->move($x, $y);
    }
    
    
    public function findClosestMouse()
    {
        $previous = INF;
        $target   = array();
        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['x'] = $animal->getCoordinateX();
                $target['y'] = $animal->getCoordinateY();
                $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 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";
    
    
}