<?php
class World {
public $sideLength;
public $filler = "0";
public $underworld = array();
function __construct($sideLength) {
$this->sideLength = $sideLength;
}
function draw() {
foreach ($this->field as $line) {
echo "\n";
}
$dead = implode(" ", $this->underworld);
echo $dead . "\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) {
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)) {
$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();
}
?>
PD9waHAKZXJyb3JfcmVwb3J0aW5nKC0xKTsKbWJfaW50ZXJuYWxfZW5jb2RpbmcoInV0Zi04Iik7CiAKY2xhc3MgV29ybGQgewogCglwdWJsaWMgJHNpZGVMZW5ndGg7CglwdWJsaWMgJGZpZWxkID0gYXJyYXkoKTsKCXB1YmxpYyAkZmlsbGVyID0gIjAiOwogCXB1YmxpYyAkdW5kZXJ3b3JsZCA9IGFycmF5KCk7CgoJZnVuY3Rpb24gX19jb25zdHJ1Y3QoJHNpZGVMZW5ndGgpIHsKCQkkdGhpcy0+c2lkZUxlbmd0aCA9ICRzaWRlTGVuZ3RoOwoJCSR0aGlzLT5maWVsZCA9IGFycmF5X2ZpbGwoMSwgJHNpZGVMZW5ndGgsIGFycmF5X2ZpbGwoMSwgJHNpZGVMZW5ndGgsICR0aGlzLT5maWxsZXIpKTsJCQoJfQogCglmdW5jdGlvbiBkcmF3KCkgewoJCWZvcmVhY2ggKCR0aGlzLT5maWVsZCBhcyAkbGluZSkgewoJCQllY2hvIGltcGxvZGUoIiAgIiwkbGluZSk7CgkJCWVjaG8gIlxuIjsKCQl9CgoJCWVjaG8gc3RyX3JlcGVhdCgiLSAtIiwgJHRoaXMtPnNpZGVMZW5ndGgpIC4gIlxuIjsK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