Advertisement
repente

Untitled

Jun 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. <?php
  2.  
  3. namespace My\Dice;
  4.  
  5. /**
  6. * Dice Process Management Class. High level class
  7. */
  8. class Dice implements DiceHistogramInterface
  9. {
  10.  
  11. use DiceHistogramTrait;
  12. /**
  13. * The constructor initializes the game. Adds all members.
  14. * Sets the cursor to 0.
  15. *
  16. * @param array $players The two-dimensional array contains all
  17. * the players who will participate in the game.
  18. *
  19. * @return void
  20. */
  21.  
  22. public function __construct($players)
  23. {
  24. $this->players = [];
  25. for ($i = 0; $i < count($players); $i++) {
  26. $name = $players[$i][0];
  27. $type = $players[$i][1];
  28. $this->players[] = new Player($name, $type);
  29. }
  30. $this->number_of_players = count($players);
  31. $this->pointer = 0;
  32. $this->currentTotal = 0;
  33. $this->throwСache = [6,6];
  34. $this->limitAiTrows = 0;
  35. $this->tempValue = 0;
  36. $this->allTrows = [];
  37. }
  38.  
  39. /**
  40. * The method sorts an array from larger to smaller based on the
  41. * numbers received by players. Simulated a quiz who is first.
  42. *
  43. * @return void
  44. */
  45.  
  46. public function contestWhoIsFirst()
  47. {
  48. $rand = new RandomSeries(1, 6);
  49. $newOrder = [];
  50.  
  51. for ($i = 0; $i < $this->number_of_players; $i++) {
  52. $number = $rand->getSeries()[0];
  53. $player = $this->players[$i];
  54. $newOrder[] = [$player,$number];
  55. $this->allTrows[] = $number;
  56. }
  57. $this->serie = $this->allTrows;
  58. usort($newOrder, function ($value01, $value02) {
  59. return $value01[1] < $value02[1];
  60. });
  61.  
  62. $this->players = [];
  63.  
  64. for ($i = 0; $i < $this->number_of_players; $i++) {
  65. $this->players[] = $newOrder[$i][0];
  66. }
  67. }
  68.  
  69. /**
  70. * The method implements the display of information about players.
  71. *
  72. * @return string Html code
  73. */
  74.  
  75. public function printedPlayers()
  76. {
  77. $info = "";
  78. for ($i = 0; $i < count($this->players); $i++)
  79. {
  80. $cursor = "";
  81. if ( $this->pointer == $i)
  82. {
  83. $cursor = "*";
  84. }
  85. $info .= $cursor . ($i + 1) . ". " . $this->players[$i]->name . "\ttotal = " . $this->players[$i]->total . ' ('. $this->players[$i]->type .') ' . "<br>";
  86. }
  87. return $info;
  88. }
  89.  
  90. /**
  91. * Method checks a condition greater than 100 (points)
  92. *
  93. * @return object class Player or false if the condition is not met
  94. */
  95.  
  96. public function checkWinner()
  97. {
  98. for ($i = 0; $i < count($this->players); $i++)
  99. {
  100. if ($this->players[$i]->total >= 100)
  101. {
  102. return $this->players[$i];
  103. }
  104. }
  105. return false;
  106. }
  107.  
  108. /**
  109. * Method aiBot artificial intelligence method
  110. *
  111. * @return bool decision on the next move
  112. */
  113.  
  114. public function aiBot($bot, $currentValue)
  115. {
  116. $all_total = [];
  117. for ($i = 0; $i < count($this->players); $i++) {
  118. $all_total[] = $this->players[$i]->total;
  119. }
  120.  
  121. rsort($all_total);
  122. $result = $bot->total + $this->currentTotal + $currentValue - $all_total[0];
  123. if ($result > 0 or $this->limitAiTrows == 5 or ($bot->total + $this->currentTotal + $currentValue) > 100)
  124. {
  125. $this->limitAiTrows = 0;
  126. return 0;
  127. }
  128. else
  129. {
  130. $this->limitAiTrows++;
  131. return 1;
  132. }
  133. }
  134.  
  135. /**
  136. * Functional method that implements the game step
  137. *
  138. * @param int $skip Simulated throw skip,
  139. * default 0 (do not miss)
  140. *
  141. * @return array game state
  142. */
  143. public function moves($skip = 0)
  144. {
  145. // init trait varaible
  146. $this->serie = $this->allTrows;
  147. $rand = new RandomSeries(2, 6);
  148. $this->tempValue = 0;
  149. if ($this->checkWinner())
  150. {
  151. $name = $this->checkWinner()->name;
  152. return ["winner", $name];
  153. }
  154. if ($this->players[$this->pointer]->type == 'human')
  155. {
  156. if ($skip)
  157. {
  158. $this->players[$this->pointer]->total += $this->currentTotal;
  159. $this->currentTotal = 0;
  160. $this->pointer += 1;
  161. if ($this->pointer == $this->number_of_players) {
  162. $this->pointer = 0;
  163. };
  164. return [ "next",
  165. $this->players[$this->pointer]->name,
  166. $this->players[$this->pointer]->type,
  167. $this->throwСache,
  168. ];
  169.  
  170. }
  171. $throw = $rand->getSeries();
  172. $this->allTrows = array_merge($this->allTrows, $throw);
  173. $this->throwСache = $throw;
  174. if (in_array(1, $throw))
  175. {
  176. $this->currentTotal = 0;
  177. $this->players[$this->pointer]->total += $this->currentTotal;
  178. $this->pointer += 1;
  179. if ($this->pointer == $this->number_of_players)
  180. {
  181. $this->pointer = 0;
  182. }
  183. return [ "next",
  184. $this->players[$this->pointer]->name,
  185. $this->players[$this->pointer]->type,
  186. $throw,
  187. ];
  188. }
  189. else
  190. {
  191. $this->currentTotal += array_sum($throw);
  192. $this->players[$this->pointer]->count += 1;
  193. return [ "next",
  194. $this->players[$this->pointer]->name,
  195. $this->players[$this->pointer]->type,
  196. $throw,
  197. ];
  198. }
  199. }
  200. if ($this->players[$this->pointer]->type == 'machine')
  201. {
  202. $throw = $rand->getSeries();
  203. $this->allTrows = array_merge($this->allTrows, $throw);
  204. if (in_array(1, $throw))
  205. {
  206. $this->currentTotal = 0;
  207. $this->players[$this->pointer]->total += $this->currentTotal;
  208. $this->pointer += 1;
  209. if ($this->pointer == $this->number_of_players)
  210. {
  211. $this->pointer = 0;
  212. }
  213. return [ "next",
  214. $this->players[$this->pointer]->name,
  215. $this->players[$this->pointer]->type,
  216. $throw,
  217. ];
  218. }
  219. if ($this->aiBot($this->players[$this->pointer], array_sum($throw)))
  220. {
  221. $this->currentTotal += array_sum($throw);
  222. $this->players[$this->pointer]->count += 1;
  223. return [ "next",
  224. $this->players[$this->pointer]->name,
  225. $this->players[$this->pointer]->type,
  226. $throw,
  227. ];
  228. }
  229. else
  230. {
  231. $this->currentTotal += array_sum($throw);
  232. $this->players[$this->pointer]->count += 1;
  233. $this->players[$this->pointer]->total += $this->currentTotal;
  234. $this->tempValue = $this->currentTotal;
  235. $this->currentTotal = 0;
  236. $this->pointer += 1;
  237. if ($this->pointer == $this->number_of_players)
  238. {
  239. $this->pointer = 0;
  240. }
  241. return [ "next",
  242. $this->players[$this->pointer]->name,
  243. $this->players[$this->pointer]->type,
  244. $throw,
  245. ];
  246. }
  247. }
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement