fork download
  1. <?php
  2.  
  3. class Animal {
  4. public $body;
  5. public $x;
  6. public $y;
  7. public $name;
  8.  
  9. public function printSymbol()
  10. {
  11. echo $this->body;
  12. }
  13. }
  14.  
  15. class Mouse extends Animal {
  16. public $body;
  17. public $x;
  18. public $y;
  19. public $name;
  20. }
  21.  
  22. class Cat extends Animal {
  23. public $body = "K";
  24. public $x;
  25. public $y;
  26. public $name;
  27. }
  28.  
  29. class Dog extends Animal {
  30. public $body = "D";
  31. public $x;
  32. public $y;
  33. public $name;
  34. }
  35.  
  36. class Field {
  37. public $width;
  38. public $height;
  39. public $field = array();
  40. public $animals = array();
  41.  
  42. public function __construct($width, $height)
  43. {
  44. $this->width = $width;
  45. $this->height = $height;
  46. }
  47.  
  48. public function createField()
  49. {
  50. for ($i = 1; $i <= $this->height; $i++) {
  51. for($j = 1; $j <= $this->width; $j++) {
  52. $this->field[$j][$i] = ".";
  53. }
  54. }
  55. }
  56.  
  57. public function placeTheAnimals()
  58. {
  59. $i = 1;
  60. $this->createField();
  61. foreach ($this->animals as $animal) {
  62. if ($animal->x == NULL && $animal->y == NULL) {
  63. do {
  64. $animal->x = mt_rand(1, $this->width);
  65. $animal->y = mt_rand(1, $this->height);
  66. } while ($this->field[$animal->y][$animal->x] != ".");
  67. if ($animal instanceof Mouse) {
  68. $animal->body = $i++;
  69. }
  70. }
  71. $this->field[$animal->y][$animal->x] = $animal;
  72. }
  73. }
  74.  
  75. public function showField()
  76. {
  77. for ($i = 1; $i <= $this->height; $i++) {
  78. for($j = 1; $j <= $this->width; $j++) {
  79. if ($this->field[$i][$j] instanceof Animal) {
  80. $this->field[$i][$j]->printSymbol();
  81. } else {
  82. echo $this->field[$i][$j];
  83. }
  84. }
  85. echo "\n";
  86. }
  87. }
  88.  
  89. }
  90.  
  91. $cat1 = new Cat("cat1");
  92. $cat2 = new Cat("cat2");
  93. $mouse1 = new Mouse("mouse1");
  94. $mouse2 = new Mouse("mouse2");
  95. $mouse3 = new Mouse("mouse3");
  96. $dog1 = new Dog("dog1");
  97. $field = new Field(10,10);
  98. $field->animals = array($cat1, $cat2, $mouse1, $mouse2, $mouse3, $dog1);
  99. $field->placeTheAnimals();
  100. $field->showField();
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
..........
.....1....
..........
..........
.......D..
..........
..........
K...2.....
.......3..
...K......