<?php
class Animal {
public $body;
public $x;
public $y;
public $name;
public function printSymbol()
{
echo $this->body;
}
}
class Mouse extends Animal {
public $body;
public $x;
public $y;
public $name;
}
class Cat extends Animal {
public $body = "K";
public $x;
public $y;
public $name;
}
class Dog extends Animal {
public $body = "D";
public $x;
public $y;
public $name;
}
class Field {
public $width;
public $height;
public $animals = array();
public function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}
public function createField()
{
for ($i = 1; $i <= $this->height; $i++) {
for($j = 1; $j <= $this->width; $j++) {
$this->field[$j][$i] = ".";
}
}
}
public function placeTheAnimals()
{
$i = 1;
$this->createField();
foreach ($this->animals as $animal) {
if ($animal->x == NULL && $animal->y == NULL) {
do {
$animal->x = mt_rand(1, $this->width); $animal->y = mt_rand(1, $this->height); } while ($this->field[$animal->y][$animal->x] != ".");
if ($animal instanceof Mouse) {
$animal->body = $i++;
}
}
$this->field[$animal->y][$animal->x] = $animal;
}
}
public function showField()
{
for ($i = 1; $i <= $this->height; $i++) {
for($j = 1; $j <= $this->width; $j++) {
if ($this->field[$i][$j] instanceof Animal) {
$this->field[$i][$j]->printSymbol();
} else {
echo $this->field[$i][$j];
}
}
echo "\n";
}
}
}
$cat1 = new Cat("cat1");
$cat2 = new Cat("cat2");
$mouse1 = new Mouse("mouse1");
$mouse2 = new Mouse("mouse2");
$mouse3 = new Mouse("mouse3");
$dog1 = new Dog("dog1");
$field = new Field(10,10);
$field->animals = array($cat1, $cat2, $mouse1, $mouse2, $mouse3, $dog1); $field->placeTheAnimals();
$field->showField();
PD9waHAKCmNsYXNzIEFuaW1hbCB7CiAgICBwdWJsaWMgJGJvZHk7CiAgICBwdWJsaWMgJHg7CiAgICBwdWJsaWMgJHk7CiAgICBwdWJsaWMgJG5hbWU7CgogICAgcHVibGljIGZ1bmN0aW9uIHByaW50U3ltYm9sKCkKICAgIHsKICAgICAgICBlY2hvICR0aGlzLT5ib2R5OwogICAgfQp9CgpjbGFzcyBNb3VzZSBleHRlbmRzIEFuaW1h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gIGZvcmVhY2ggKCR0aGlzLT5hbmltYWxzIGFzICRhbmltYWwpIHsKICAgICAgICAgICAgaWYgKCRhbmltYWwtPnggPT0gTlVMTCAmJiAkYW5pbWFsLT55ID09IE5VTEwpIHsKICAgICAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICAgICAgICAkYW5pbWFsLT54ID0gbXRfcmFuZCgxLCAkdGhpcy0+d2lkdGgpOwogICAgICAgICAgICAgICAgICAgICRhbmltYWwtPnkgPSBtdF9yYW5kKDEsICR0aGlzLT5oZWlnaHQpOwogICAgICAgICAgICAgICAgfSB3aGlsZSAoJHRoaXMtPmZpZWxkWyRhbmltYWwtPnldWyRhbmltYWwtPnhdICE9ICIuIik7CiAgICAgICAgICAgICAgICBpZiAoJGFuaW1hbCBpbnN0YW5jZW9mIE1vdXNlKSB7CiAgICAgICAgICAgICAgICAgICAgJGFuaW1hbC0+Ym9keSA9ICRpKys7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgJHRoaXMtPmZpZWxkWyRhbmltYWwtPnldWyRhbmltYWwtPnhdID0gJGFuaW1hbDsKICAgICAgICB9CiAgICB9CgogICAgcHVibGljIGZ1bmN0aW9uIHNob3dGaWVsZCgpCiAgICB7CiAgICAgICAgZm9yICgkaSA9IDE7ICRpIDw9ICR0aGlzLT5oZWlnaHQ7ICRpKyspIHsKICAgICAgICAgICAgZm9yKCRqID0gMTsgJGogPD0gJHRoaXMtPndpZHRoOyAkaisrKSB7CiAgICAgICAgICAgICAgICBpZiAoJHRoaXMtPmZpZWxkWyRpXVskal0gaW5zdGFuY2VvZiBBbmltYWwpIHsKICAgICAgICAgICAgICAgICAgICAkdGhpcy0+ZmllbGRbJGldWyRqXS0+cHJpbnRTeW1ib2woKTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgZWNobyAkdGhpcy0+ZmllbGRbJGldWyRqXTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBlY2hvICJcbiI7CiAgICAgICAgfQogICAgfQoKfQoKJGNhdDEgICA9IG5ldyBDYXQoImNhdDEiKTsKJGNhdDIgICA9IG5ldyBDYXQoImNhdDIiKTsKJG1vdXNlMSA9IG5ldyBNb3VzZSgibW91c2UxIik7CiRtb3VzZTIgPSBuZXcgTW91c2UoIm1vdXNlMiIpOwokbW91c2UzID0gbmV3IE1vdXNlKCJtb3VzZTMiKTsKJGRvZzEgICA9IG5ldyBEb2coImRvZzEiKTsKJGZpZWxkICA9IG5ldyBGaWVsZCgxMCwxMCk7CiRmaWVsZC0+YW5pbWFscyA9IGFycmF5KCRjYXQxLCAkY2F0MiwgJG1vdXNlMSwgJG1vdXNlMiwgJG1vdXNlMywgJGRvZzEpOwokZmllbGQtPnBsYWNlVGhlQW5pbWFscygpOwokZmllbGQtPnNob3dGaWVsZCgpOw==