Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace My\Dice;
- /**
- * Create a randoms numbers, a class allows you to create an array of
- * random numbers arbitrary length.
- */
- class RandomSeries
- {
- /**
- *
- * The constructor initializes the class RandomSeries.
- *
- * @param int $count The number of random numbers or the length of the array,
- * default 1
- * @param int $range random number range,
- * default 10.
- */
- public function __construct(int $count = 1, int $range = 10)
- {
- $this->count = $count;
- $this->range = $range;
- $this->series = $this->generate_series($count, $range);
- }
- /**
- * The method implements a set of random numbers based on the rand function.
- *
- * @param int $count Specifies the number of random numbers or the length of the array
- *
- * @param int $range Specifies the end range of a random number.
- *
- * @return array of random numbers
- */
- public function generate_series (int $count, int $range) {
- $series = [];
- for ($i = 0; $i < $count; $i++) {
- $series[] = rand(1, $range);
- }
- return $series;
- }
- /**
- * The method queries the current series of random numbers,
- * and then updates the series.
- *
- * @return array current of random numbers
- */
- public function get_series() {
- $series = $this->series;
- $this->update_series();
- return $series;
- }
- /**
- * The method updates a series of random numbers
- *
- * @return void
- */
- public function update_series() {
- $this->series = $this->generate_series($this->count, $this->range);
- }
- }
- /**
- * Сreate a player, a class creates a player object with basic attributes
- */
- class Player
- {
- /**
- * Constructor to initiate an object with the current settings.
- */
- public function __construct ($name, $type) {
- $this->name = $name;
- $this->count = 0 ;
- $this->total = 0 ;
- $this->type = mb_strtolower($type);
- }
- /**
- * Method asks for total player points
- *
- * @return int Number of points
- */
- public function get_total() {
- return $this->total;
- }
- }
- /**
- * Dice Process Management Class. High level class
- */
- class Dice
- {
- /**
- * The constructor initializes the game. Adds all members.
- * Sets the cursor to 0.
- *
- * @param array $players The two-dimensional array contains all
- * the players who will participate in the game.
- *
- * @return void
- */
- public function __construct ($players) {
- $this->players = [];
- for ($i = 0; $i < count($players); $i++){
- $name = $players[$i][0];
- $type = $players[$i][1];
- $this->players[] = new Player($name, $type);
- }
- $this->number_of_players = count($players);
- $this->pointer = 0;
- }
- /**
- * The method sorts an array from larger to smaller based on the
- * numbers received by players. Simulated a quiz who is first.
- *
- * @return void
- */
- public function contest_who_is_first () {
- $rand = new RandomSeries(1, 6);
- $new_order = [];
- for ($i = 0; $i < $this->number_of_players; $i++) {
- $number = $rand->get_series()[0];
- $player = $this->players[$i];
- $new_order[] = [$player,$number];
- }
- usort($new_order, function($a, $b){
- return $a[1] < $b[1];
- });
- $this->players = [];
- for ($i = 0; $i < $this->number_of_players; $i++) {
- $this->players[] = $new_order[$i][0];
- }
- }
- /**
- * The method implements the display of information about players.
- *
- * @return string Html code
- */
- public function printed_players () {
- $info = "";
- for ($i = 0; $i < count($this->players); $i++) {
- $info .= ($i + 1) . ". " . $this->players[$i]->name . "\ttotal = " . $this->players[$i]->total . ' ('. $this->players[$i]->type .') ' . "<br>";
- }
- return $info;
- }
- /**
- * Method checks a condition greater than 100 (points)
- *
- * @return object class Player or false if the condition is not met
- */
- public function check_winner(){
- for ($i = 0; $i < count($this->players); $i++) {
- if ($this->players[$i]->total >= 100) {
- return $this->players[$i];
- }
- }
- return false;
- }
- /**
- * Functional method that implements the game step
- *
- * @param int $skip Simulated throw skip,
- * default 0 (do not miss)
- *
- * @return array game state
- */
- public function moves($skip = 0) {
- $rand = new RandomSeries(2,6);
- if($this->check_winner()) {
- $name = $this->check_winner()->name;
- return ["winner", $name];
- }
- $throw = $rand->get_series();
- if ($skip) {
- "skip the throw (Human)";
- } else {
- if (in_array(1, $throw)) {
- "hahahahha zero total";
- // $this->players[$this->pointer]->total = 0;
- } else {
- $this->players[$this->pointer]->total += array_sum($throw);
- $this->players[$this->pointer]->count += 1;
- }
- }
- $this->pointer += 1;
- if ($this->pointer == $this->number_of_players) {
- $this->pointer = 0;
- }
- return [ "next",
- $this->players[$this->pointer]->name,
- $this->players[$this->pointer]->type,
- $throw,
- ];
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement