<?php
abstract class Animal
{
protected $x;
protected $y;
protected $previousX;
protected $previousY;
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 setField(GameField $field)
{
$this->field = $field;
}
}
class Mouse extends Animal
{
protected $cats = array();
public function findAllCats()
{
$animals = $this->field->shareAnimals();
foreach ($animals as $animal) {
if ($animal instanceof Cat
&& (abs($this->x - $animal->getCoordinateX()) <= 9 || abs($this->y - $animal->getCoordinateY()) <= 9)) { $this->cats[] = $animal;
}
}
}
//временный код для одной кошки на поле:
public function makeMove()
{
$this->findAllCats();
if ($this->x <= $this->cats[0]->getCoordinateX() && $this->x != 0) {
$this->x = $this->x - 1;
}
elseif ($this->x >= $this->cats[0]->getCoordinateX() && $this->x != 19) {
$this->x = $this->x + 1;
} elseif ($this->y <= $this->cats[0]->getCoordinateY() && $this->y != 0) {
$this->y = $this->y - 1;
} elseif ($this->y >= $this->cats[0]->getCoordinateY() && $this->y != 19) {
$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();
}
if ($this->x == $target['x'] && $this->y == $target['y']) {
$this->field->killMouse($this->x, $this->y);
$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;
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()
{
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 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(5, 5);
$mouse1 = new Mouse(3, 2, "1");
$mouse2 = new Mouse(4, 0, "2");
$kitty = new Cat(1, 1, "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";
}