fork download
  1. <?php
  2.  
  3. class World {
  4.  
  5. public $sideLength;
  6. public $field = array();
  7. public $filler = "0";
  8.  
  9. function __construct($sideLength) {
  10. $this->sideLength = $sideLength;
  11. $this->field = array_fill(1, $sideLength, array_fill(1, $sideLength, $this->filler));
  12. }
  13.  
  14. function draw() {
  15. foreach ($this->field as $line) {
  16. echo implode(" ",$line);
  17. echo "<br>";
  18. }
  19. }
  20.  
  21. function place(Animal $animal) {
  22.  
  23. do {
  24. $x = mt_rand(1, $this->sideLength);
  25. $y = mt_rand(1, $this->sideLength);
  26.  
  27. if ($this->field[$x][$y] == $this->filler) {
  28. $this->field[$x][$y] = $animal->icon;
  29. $animal->placed = true;
  30. $animal->x = $x;
  31. $animal->y = $y;
  32. }
  33.  
  34.  
  35. } while ($animal->placed == false);
  36.  
  37. }
  38.  
  39. }
  40.  
  41. abstract class Animal {
  42. public $x;
  43. public $y;
  44. public $icon;
  45. public $placed = false;
  46.  
  47. function __construct($icon) {
  48. $this->icon = $icon;
  49. }
  50. }
  51.  
  52. class Mouse extends Animal {
  53. public function move (World $world) {
  54.  
  55. }
  56. }
  57.  
  58. $world1 = new World(10);
  59.  
  60. $mouse1 = new Mouse('<bold>1</bold>');
  61. $mouse2 = new Mouse('2');
  62. $mouse3 = new Mouse('3');
  63. $mouse4 = new Mouse('4');
  64.  
  65.  
  66. $world1->place($mouse1);
  67. $world1->place($mouse2);
  68. $world1->place($mouse3);
  69. $world1->place($mouse4);
  70.  
  71. $world1->draw();
  72.  
  73. ?>
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
0  0  0  0  2  0  0  0  0  0<br>0  0  0  0  0  0  0  0  0  0<br>0  0  0  0  0  0  0  0  0  0<br>0  0  0  0  0  0  0  4  0  0<br>0  0  0  0  0  0  0  <bold>1</bold>  0  0<br>0  0  0  0  0  0  0  0  0  0<br>0  0  0  0  0  0  0  0  0  0<br>0  0  0  0  0  0  0  0  0  3<br>0  0  0  0  0  0  0  0  0  0<br>0  0  0  0  0  0  0  0  0  0<br>