Advertisement
repente

Untitled

Jun 1st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. <?php
  2.  
  3. namespace My\Dice;
  4.  
  5. /**
  6. * Create a randoms numbers, a class allows you to create an array of
  7. * random numbers arbitrary length.
  8. */
  9.  
  10. class RandomSeries
  11. {
  12. /**
  13. *
  14. * The constructor initializes the class RandomSeries.
  15. *
  16. * @param int $count The number of random numbers or the length of the array,
  17. * default 1
  18. * @param int $range random number range,
  19. * default 10.
  20. */
  21.  
  22. public function __construct(int $count = 1, int $range = 10)
  23. {
  24. $this->count = $count;
  25. $this->range = $range;
  26. $this->series = $this->generate_series($count, $range);
  27.  
  28. }
  29.  
  30. /**
  31. * The method implements a set of random numbers based on the rand function.
  32. *
  33. * @param int $count Specifies the number of random numbers or the length of the array
  34. *
  35. * @param int $range Specifies the end range of a random number.
  36. *
  37. * @return array of random numbers
  38. */
  39.  
  40. public function generate_series (int $count, int $range) {
  41. $series = [];
  42. for ($i = 0; $i < $count; $i++) {
  43. $series[] = rand(1, $range);
  44. }
  45. return $series;
  46. }
  47.  
  48. /**
  49. * The method queries the current series of random numbers,
  50. * and then updates the series.
  51. *
  52. * @return array current of random numbers
  53. */
  54.  
  55. public function get_series() {
  56. $series = $this->series;
  57. $this->update_series();
  58. return $series;
  59. }
  60.  
  61. /**
  62. * The method updates a series of random numbers
  63. *
  64. * @return void
  65. */
  66.  
  67. public function update_series() {
  68. $this->series = $this->generate_series($this->count, $this->range);
  69. }
  70.  
  71.  
  72. }
  73.  
  74. /**
  75. * Сreate a player, a class creates a player object with basic attributes
  76. */
  77.  
  78. class Player
  79. {
  80.  
  81. /**
  82. * Constructor to initiate an object with the current settings.
  83. */
  84.  
  85. public function __construct ($name, $type) {
  86. $this->name = $name;
  87. $this->count = 0 ;
  88. $this->total = 0 ;
  89. $this->type = mb_strtolower($type);
  90. }
  91.  
  92. /**
  93. * Method asks for total player points
  94. *
  95. * @return int Number of points
  96. */
  97.  
  98. public function get_total() {
  99. return $this->total;
  100. }
  101.  
  102. }
  103.  
  104. /**
  105. * Dice Process Management Class. High level class
  106. */
  107.  
  108. class Dice
  109. {
  110. /**
  111. * The constructor initializes the game. Adds all members.
  112. * Sets the cursor to 0.
  113. *
  114. * @param array $players The two-dimensional array contains all
  115. * the players who will participate in the game.
  116. *
  117. * @return void
  118. */
  119.  
  120. public function __construct ($players) {
  121. $this->players = [];
  122. for ($i = 0; $i < count($players); $i++){
  123. $name = $players[$i][0];
  124. $type = $players[$i][1];
  125. $this->players[] = new Player($name, $type);
  126. }
  127. $this->number_of_players = count($players);
  128. $this->pointer = 0;
  129. }
  130.  
  131. /**
  132. * The method sorts an array from larger to smaller based on the
  133. * numbers received by players. Simulated a quiz who is first.
  134. *
  135. * @return void
  136. */
  137.  
  138. public function contest_who_is_first () {
  139. $rand = new RandomSeries(1, 6);
  140. $new_order = [];
  141.  
  142. for ($i = 0; $i < $this->number_of_players; $i++) {
  143. $number = $rand->get_series()[0];
  144. $player = $this->players[$i];
  145. $new_order[] = [$player,$number];
  146. }
  147.  
  148. usort($new_order, function($a, $b){
  149. return $a[1] < $b[1];
  150. });
  151.  
  152. $this->players = [];
  153.  
  154. for ($i = 0; $i < $this->number_of_players; $i++) {
  155. $this->players[] = $new_order[$i][0];
  156. }
  157.  
  158. }
  159.  
  160. /**
  161. * The method implements the display of information about players.
  162. *
  163. * @return string Html code
  164. */
  165.  
  166. public function printed_players () {
  167. $info = "";
  168. for ($i = 0; $i < count($this->players); $i++) {
  169. $info .= ($i + 1) . ". " . $this->players[$i]->name . "\ttotal = " . $this->players[$i]->total . ' ('. $this->players[$i]->type .') ' . "<br>";
  170. }
  171. return $info;
  172. }
  173.  
  174. /**
  175. * Method checks a condition greater than 100 (points)
  176. *
  177. * @return object class Player or false if the condition is not met
  178. */
  179.  
  180. public function check_winner(){
  181. for ($i = 0; $i < count($this->players); $i++) {
  182. if ($this->players[$i]->total >= 100) {
  183. return $this->players[$i];
  184. }
  185. }
  186. return false;
  187. }
  188.  
  189. /**
  190. * Functional method that implements the game step
  191. *
  192. * @param int $skip Simulated throw skip,
  193. * default 0 (do not miss)
  194. *
  195. * @return array game state
  196. */
  197.  
  198. public function moves($skip = 0) {
  199. $rand = new RandomSeries(2,6);
  200.  
  201. if($this->check_winner()) {
  202. $name = $this->check_winner()->name;
  203. return ["winner", $name];
  204. }
  205. $throw = $rand->get_series();
  206. if ($skip) {
  207. "skip the throw (Human)";
  208. } else {
  209. if (in_array(1, $throw)) {
  210. "hahahahha zero total";
  211. // $this->players[$this->pointer]->total = 0;
  212. } else {
  213. $this->players[$this->pointer]->total += array_sum($throw);
  214. $this->players[$this->pointer]->count += 1;
  215. }
  216. }
  217. $this->pointer += 1;
  218. if ($this->pointer == $this->number_of_players) {
  219. $this->pointer = 0;
  220. }
  221. return [ "next",
  222. $this->players[$this->pointer]->name,
  223. $this->players[$this->pointer]->type,
  224. $throw,
  225. ];
  226. }
  227. }
  228.  
  229. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement