Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace My\Dice;
- /**
- * Dice Process Management Class. High level class
- */
- class Dice implements DiceHistogramInterface
- {
- use DiceHistogramTrait;
- /**
- * 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;
- $this->currentTotal = 0;
- $this->throwСache = [6,6];
- $this->limitAiTrows = 0;
- $this->tempValue = 0;
- $this->allTrows = [];
- }
- /**
- * 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 contestWhoIsFirst()
- {
- $rand = new RandomSeries(1, 6);
- $newOrder = [];
- for ($i = 0; $i < $this->number_of_players; $i++) {
- $number = $rand->getSeries()[0];
- $player = $this->players[$i];
- $newOrder[] = [$player,$number];
- $this->allTrows[] = $number;
- }
- $this->serie = $this->allTrows;
- usort($newOrder, function ($value01, $value02) {
- return $value01[1] < $value02[1];
- });
- $this->players = [];
- for ($i = 0; $i < $this->number_of_players; $i++) {
- $this->players[] = $newOrder[$i][0];
- }
- }
- /**
- * The method implements the display of information about players.
- *
- * @return string Html code
- */
- public function printedPlayers()
- {
- $info = "";
- for ($i = 0; $i < count($this->players); $i++)
- {
- $cursor = "";
- if ( $this->pointer == $i)
- {
- $cursor = "*";
- }
- $info .= $cursor . ($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 checkWinner()
- {
- for ($i = 0; $i < count($this->players); $i++)
- {
- if ($this->players[$i]->total >= 100)
- {
- return $this->players[$i];
- }
- }
- return false;
- }
- /**
- * Method aiBot artificial intelligence method
- *
- * @return bool decision on the next move
- */
- public function aiBot($bot, $currentValue)
- {
- $all_total = [];
- for ($i = 0; $i < count($this->players); $i++) {
- $all_total[] = $this->players[$i]->total;
- }
- rsort($all_total);
- $result = $bot->total + $this->currentTotal + $currentValue - $all_total[0];
- if ($result > 0 or $this->limitAiTrows == 5 or ($bot->total + $this->currentTotal + $currentValue) > 100)
- {
- $this->limitAiTrows = 0;
- return 0;
- }
- else
- {
- $this->limitAiTrows++;
- return 1;
- }
- }
- /**
- * 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)
- {
- // init trait varaible
- $this->serie = $this->allTrows;
- $rand = new RandomSeries(2, 6);
- $this->tempValue = 0;
- if ($this->checkWinner())
- {
- $name = $this->checkWinner()->name;
- return ["winner", $name];
- }
- if ($this->players[$this->pointer]->type == 'human')
- {
- if ($skip)
- {
- $this->players[$this->pointer]->total += $this->currentTotal;
- $this->currentTotal = 0;
- $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,
- $this->throwСache,
- ];
- }
- $throw = $rand->getSeries();
- $this->allTrows = array_merge($this->allTrows, $throw);
- $this->throwСache = $throw;
- if (in_array(1, $throw))
- {
- $this->currentTotal = 0;
- $this->players[$this->pointer]->total += $this->currentTotal;
- $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,
- ];
- }
- else
- {
- $this->currentTotal += array_sum($throw);
- $this->players[$this->pointer]->count += 1;
- return [ "next",
- $this->players[$this->pointer]->name,
- $this->players[$this->pointer]->type,
- $throw,
- ];
- }
- }
- if ($this->players[$this->pointer]->type == 'machine')
- {
- $throw = $rand->getSeries();
- $this->allTrows = array_merge($this->allTrows, $throw);
- if (in_array(1, $throw))
- {
- $this->currentTotal = 0;
- $this->players[$this->pointer]->total += $this->currentTotal;
- $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,
- ];
- }
- if ($this->aiBot($this->players[$this->pointer], array_sum($throw)))
- {
- $this->currentTotal += array_sum($throw);
- $this->players[$this->pointer]->count += 1;
- return [ "next",
- $this->players[$this->pointer]->name,
- $this->players[$this->pointer]->type,
- $throw,
- ];
- }
- else
- {
- $this->currentTotal += array_sum($throw);
- $this->players[$this->pointer]->count += 1;
- $this->players[$this->pointer]->total += $this->currentTotal;
- $this->tempValue = $this->currentTotal;
- $this->currentTotal = 0;
- $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