<?php
error_reporting(-1);
mb_internal_encoding("utf-8");
 
class World {
 
	public $sideLength;
	public $field = array();
	public $filler = "0";
 	public $underworld = array();

	function __construct($sideLength) {
		$this->sideLength = $sideLength;
		$this->field = array_fill(1, $sideLength, array_fill(1, $sideLength, $this->filler));		
	}
 
	function draw() {
		foreach ($this->field as $line) {
			echo implode("  ",$line);
			echo "\n";
		}

		echo str_repeat("- -", $this->sideLength) . "\n";

		$dead = implode(" ", $this->underworld);

		echo $dead . "\n";

		echo str_repeat("- -", $this->sideLength) . "\n";
	}
 
	function place(Animal $animal) {
 		$try = 100;
		do {
			$x = mt_rand(1, $this->sideLength);
			$y = mt_rand(1, $this->sideLength);
 
			if ($this->field[$x][$y] == $this->filler) {
				$this->field[$x][$y] = $animal->icon;
				$animal->placed = true;
				$animal->x = $x;
				$animal->y = $y;
			}
 			$try--;
 			
		} while ($animal->placed == false and $try > 0);
 		
 		if ($try == 0) {
 			$this->underworld[] = $animal->icon;
 		}
	}
 
}
 
abstract class Animal {
	public $x;
	public $y;
	public $icon;
	public $placed = false;
	public $dead = false;
 
	function __construct($icon) {
		$this->icon = $icon;
	}
}
 
class Mouse extends Animal {
	public function move (World $world) {
		if ($this->dead == false && $world->field[$this->x][$this->y] != $this->icon) {
			$this->dead = true;
			$world->underworld[] = $this->icon;
		}

		if ($this->dead == false) {
			$directions = array();
			if (isset($world->field[$this->x][$this->y - 1]) and ($world->field[$this->x][$this->y - 1] == $world->filler)) {
				$directions[] = array($this->x, $this->y - 1);
			}
			if (isset($world->field[$this->x + 1][$this->y]) and ($world->field[$this->x + 1][$this->y] == $world->filler)) {
				$directions[] = array($this->x + 1, $this->y);
			}
			if (isset($world->field[$this->x][$this->y + 1]) and ($world->field[$this->x][$this->y + 1] == $world->filler)) {
				$directions[] = array($this->x, $this->y + 1);
			}
			if (isset($world->field[$this->x - 1][$this->y]) and ($world->field[$this->x - 1][$this->y] == $world->filler)) {
				$directions[] = array($this->x - 1, $this->y);
			}
			
			if (!empty($directions)) {
				$moveTo = mt_rand(0, count($directions)-1);
				
				$world->field[$this->x][$this->y] = $world->filler;

				$this->x = $directions[$moveTo][0];
				$this->y = $directions[$moveTo][1];

				$world->field[$this->x][$this->y] = $this->icon;
				//var_dump($this);
			}	
		}							
	}
}

class Cat extends Animal {
	public function move (World $world) {

		for ($i = ($this->x - 1); $i <= ($this->x + 1); $i++) {
			for ($j = ($this->y - 1); $j <= ($this->y + 1); $j++) {

				if($i == $this->x and $j == $this->y) {
					continue;
				}

				if (isset($world->field[$i][$j]) && ($world->field[$i][$j] != $world->filler)) {
					
					
					
					$world->field[$this->x][$this->y] = $world->filler;

					$this->x = $i;
					$this->y = $j;

					$world->field[$this->x][$this->y] = $this->icon;

					break 2;
					
				} 
			} 
		}
	}
}

$world1 = new World(5);
 
$mouse1 = new Mouse('1');
$mouse2 = new Mouse('2');
$mouse3 = new Mouse('3');
$mouse4 = new Mouse('4');
$mouse5 = new Mouse('5');

$cat1 = new Cat('#');
 
$world1->place($mouse1);
$world1->place($mouse2);
$world1->place($mouse3);
$world1->place($mouse4);
$world1->place($mouse5);
$world1->place($cat1);


$world1->draw();

for ($i = 0; $i < 6; $i++) {

	$mouse1->move($world1);
	$mouse2->move($world1);
	$mouse3->move($world1);
	$mouse4->move($world1);
	$mouse5->move($world1);

	$cat1->move($world1);

	$world1->draw();

}



 
?>