fork download
  1. <?php
  2.  
  3. class field{
  4. public $size = 8;
  5. public $cat = 2;
  6. public $mouse = 15;
  7.  
  8. public $animals = array();
  9. public static $field = array();
  10. public $xy = array();
  11. public $record = array();
  12.  
  13. function create_field(){
  14. for($i=0;$i<$this->size;++$i){
  15. $arr = array();
  16. for($b=0;$b<$this->size;++$b){
  17. $arr[] = ".";
  18. }
  19. $this->field[] = $arr;
  20. }
  21. }
  22.  
  23. function add_animals(){
  24. for($i=1;$i<=$this->cat+$this->mouse;++$i){
  25. $animal_name = $i>$this->cat?"mouse":"cat";
  26. $animal = new $animal_name;
  27.  
  28. do{
  29. $xy = array(rand(0, $this->size), rand(0, $this->size));
  30. }while(
  31. $this->field[$xy[1]][$xy[0]] !== "."
  32. );
  33. $animal->xy = $xy;
  34. $this->field[$xy[1]][$xy[0]] = $i>$this->cat?"M":"C";
  35.  
  36. $this->animals[] = $animal;
  37. }
  38. $this->record[] = $this->field;
  39. }
  40.  
  41. function move($num){
  42. for($q=0;$q<$num;++$q){
  43. foreach($this->animals as $obj){
  44. if(get_class($obj) == "mouse"){
  45. $moves = $this->possible_moves($obj, "mouse");
  46.  
  47. $mouse_sight_zones = array(array(-3,-3,7,3,array(0,-1)), array(1,-3,3,7,array(1,0)), array(-3,1,7,3,array(0,1)), array(-3,-3,3,7,array(-1,0)));
  48. $danger_zones = array();
  49.  
  50. foreach($mouse_sight_zones as $arr){
  51. for($y=0;$y<$arr[3];++$y){
  52. for($x=0;$x<$arr[2];++$x){
  53. if($this->field[($obj->xy[1]+$y+$arr[1])][($obj->xy[0]+$x+$arr[0])] == "C"){
  54. $direction = $arr[4];
  55. if(!array_search($arr, $danger_zones)){
  56. $danger_zones[] = $direction;
  57. }
  58. }
  59. }
  60. }
  61. }
  62.  
  63. $moves = $this->my_array_diff($moves, $danger_zones);
  64.  
  65. $move = $moves[array_rand($moves)];
  66. $this->field[$obj->xy[1]][$obj->xy[0]] = ".";
  67. $obj->xy[0]+=$move[0];
  68. $obj->xy[1]+=$move[1];
  69. $this->field[$obj->xy[1]][$obj->xy[0]] = "M";
  70. }
  71. }
  72. foreach($this->animals as $obj){
  73. if(get_class($obj) == "cat"){
  74. if($obj->sleep == TRUE){
  75. $obj->sleep = FALSE;
  76. continue;
  77. }
  78.  
  79. $moves = $this->possible_moves($obj, "cat");
  80.  
  81. $cat_sight_zones = array(array(-$this->size,-$this->size,(($this->size)*2)+1,$this->size,array(0,-1)), array(1,-$this->size,$this->size,(($this->size)*2)+1,array(1,0)), array(-$this->size,1,(($this->size)*2)+1,$this->size,array(0,1)), array(-$this->size,-$this->size,$this->size,(($this->size)*2)+1,array(-1,0)));
  82. $wanted_zones = array();
  83.  
  84. foreach($cat_sight_zones as $arr){
  85. for($y=0;$y<$arr[3];++$y){
  86. for($x=0;$x<$arr[2];++$x){
  87. if($this->field[($obj->xy[1]+$y+$arr[1])][($obj->xy[0]+$x+$arr[0])] == "M"){
  88. $direction = $arr[4];
  89. if(!array_search($arr, $wanted_zones)){
  90. $wanted_zones[] = $direction;
  91. }
  92. }
  93. }
  94. }
  95. $wanted_zones[] = ((in_array(array(1,0), $wanted_zones)) and (in_array(array(0,1), $wanted_zones)))?array(1,1):"";
  96. $wanted_zones[] = ((in_array(array(-1,0), $wanted_zones)) and (in_array(array(0,1), $wanted_zones)))?array(-1,1):"";
  97. $wanted_zones[] = ((in_array(array(-1,0), $wanted_zones)) and (in_array(array(0,-1), $wanted_zones)))?array(-1,-1):"";
  98. $wanted_zones[] = ((in_array(array(1,0), $wanted_zones)) and (in_array(array(0,-1), $wanted_zones)))?array(1,-1):"";
  99. $wanted_zones = array_filter($wanted_zones,function($el){ return !empty($el);});
  100. }
  101.  
  102. $moves = $this->my_array_match($moves, $wanted_zones);
  103. $move = $moves[array_rand($moves)];
  104.  
  105. $this->field[$obj->xy[1]][$obj->xy[0]] = ".";
  106. $obj->xy[0]+=$move[0];
  107. $obj->xy[1]+=$move[1];
  108. $xxx = $obj->xy[0];
  109. $yyy = $obj->xy[1];
  110. $this->field[$obj->xy[1]][$obj->xy[0]] = "C";
  111.  
  112. $sleep = FALSE;
  113. $unset_counter = 0;
  114.  
  115. foreach($this->animals as $obj2){
  116. if(get_class($obj2) == "mouse"){
  117. if($obj2->xy==array($xxx, $yyy)){
  118. unset($this->animals[$unset_counter]);
  119. sort($this->animals);
  120. $this->field[$obj2->xy[1]][$obj2->xy[0]] = "@";
  121. $obj->sleep = TRUE;
  122. }
  123. }
  124. $unset_counter++;
  125. }
  126. }
  127. }
  128. $this->record[] = $this->field;
  129. }
  130. }
  131.  
  132.  
  133. function possible_moves($obj, $animal){
  134. switch($animal){
  135. case "mouse":
  136. $possible_moves = array(array(1,0), array(0,1), array(-1,0), array(0,-1));
  137. $moves = array();
  138. foreach($possible_moves as $arr){
  139. if(isset($this->field[$obj->xy[1]+$arr[1]][$obj->xy[0]+$arr[0]]) and $this->field[$obj->xy[1]+$arr[1]][$obj->xy[0]+$arr[0]] == "."){
  140. $moves[] = $arr;
  141. }
  142. }
  143. break;
  144. case "cat":
  145. $possible_moves = array(array(1,0), array(0,1), array(-1,0), array(0,-1), array(1,1), array(1,-1), array(-1,1), array(-1,-1));
  146. $moves = array();
  147. foreach($possible_moves as $arr){
  148. if(isset($this->field[$obj->xy[1]+$arr[1]][$obj->xy[0]+$arr[0]]) and ($this->field[$obj->xy[1]+$arr[1]][$obj->xy[0]+$arr[0]] == "." or $this->field[$obj->xy[1]+$arr[1]][$obj->xy[0]+$arr[0]] == "M")){
  149. $moves[] = $arr;
  150. }
  151. }
  152. break;
  153. }
  154. return $moves;
  155. }
  156.  
  157. function my_array_diff($moves, $danger_zones){
  158. for($x=0;$x<=count($moves);++$x){
  159. foreach($danger_zones as $arr){
  160. if($moves[$x] == $arr){
  161. unset($moves[$x]);
  162. }
  163. }
  164. }
  165. return $moves;
  166. }
  167.  
  168. function my_array_match($moves, $wanted_zones){
  169. $moves_temp = array();
  170. for($x=0;$x<=count($moves);++$x){
  171. foreach($wanted_zones as $arr){
  172. if($moves[$x] == $arr){
  173. $moves_temp[] = $moves[$x];
  174. }
  175. }
  176. }
  177. return $moves_temp;
  178. }
  179.  
  180. function show(){
  181. echo "\r\n";
  182. foreach($this->record as $arr=>$arr_2){
  183. foreach($arr_2 as $var=>$var_3){
  184. echo "|";
  185. foreach($var_3 as $var_4){
  186. echo "$var_4|";
  187. }
  188. echo "\r\n";
  189. }
  190. echo "\r\n\r\n";
  191. }
  192. }
  193. }
  194.  
  195. class cat extends field{
  196. public $power = 8;
  197. public $sleep = 2;
  198. }
  199. class mouse extends field{}
  200.  
  201. $f = new field;
  202. $f->create_field();
  203. $f->add_animals();
  204. $f->move(23);
  205. $f->show();
  206. ?>
Success #stdin #stdout 0.1s 20520KB
stdin
Standard input is empty
stdout
|.|M|.|M|.|.|.|M|
|C|.|.|M|M|M|.|.|
|.|M|.|M|.|.|M|.|
|.|.|.|C|.|.|M|.|
|.|.|.|.|.|.|M|M|
|.|.|.|.|.|M|.|.|
|.|.|.|.|.|.|.|.|
|.|.|.|.|.|M|.|M|


|M|.|.|M|M|M|.|.|
|C|M|.|.|M|.|M|M|
|.|.|.|.|M|.|M|.|
|.|.|.|C|.|.|.|M|
|.|.|.|.|.|.|.|.|
|.|.|.|.|.|.|M|.|
|.|.|.|.|.|M|.|M|
|.|.|.|.|.|.|M|.|


|M|M|.|M|M|.|M|.|
|.|.|.|.|.|M|M|M|
|C|.|.|.|.|M|.|M|
|.|.|.|.|C|.|.|.|
|.|.|.|.|.|.|.|M|
|.|.|.|.|.|.|.|M|
|.|.|.|.|.|.|M|.|
|.|.|.|.|.|M|.|M|


|M|M|M|.|.|M|M|M|
|.|.|.|.|.|M|.|M|
|.|C|.|.|.|.|M|M|
|.|.|.|.|.|C|.|.|
|.|.|.|.|.|.|.|M|
|.|.|.|.|.|.|.|M|
|.|.|.|.|.|M|.|M|
|.|.|.|.|.|.|M|.|


|M|M|M|.|M|M|M|M|
|.|C|.|.|.|.|M|M|
|.|.|.|.|.|.|.|M|
|.|.|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