Advertisement
Guest User

Dice

a guest
Feb 25th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.18 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Класс "Игрок".
  4.  */
  5. class Player
  6. {
  7.     private $memory;
  8.     private $dice;
  9.  
  10.     /**
  11.      * Конструктор.
  12.      */
  13.     public function __construct()
  14.     {
  15.         // Инициализируем память
  16.         $this->memory = new Memory();
  17.         // Даём игроку кубик
  18.         $this->dice = new Dice();
  19.     }
  20.  
  21.     /**
  22.      * Сделать бросок кости
  23.      */
  24.     public function makeMove()
  25.     {
  26.         // Бросаем кость
  27.         $points = $this->dice->drop();
  28.         // Сохраняем счет
  29.         $this->memory->add($points);
  30.     }
  31.  
  32.     /**
  33.      * Сбросить счёт
  34.      */
  35.     public function resetPoints()
  36.     {
  37.         $this->memory->reset();
  38.     }
  39.  
  40.     /**
  41.      * Получить результаты в виде HTML таблицы
  42.      */
  43.     public function getResult()
  44.     {
  45.         $out = '<table><caption>Результаты</caption><tr>';
  46.         foreach ($this->memory->get() as $step => $points) {
  47.             $step++;
  48.             $out .= "<td>Бросок #$step<br>Кость: $points</td>";
  49.         }
  50.         $out .= '</tr>';
  51.         $out .= '</table>';
  52.  
  53.         return $out;
  54.     }
  55. }
  56.  
  57.  
  58. /**
  59.  * Класс "Игровая кость"
  60.  */
  61. class Dice
  62. {
  63.     /**
  64.      * Сделать бросок кости
  65.      */
  66.     public function drop()
  67.     {
  68.         return mt_rand(1, 6);
  69.     }
  70. }
  71.  
  72. /**
  73.  * Класс "памяти"
  74.  */
  75. class Memory
  76. {
  77.     private $storage;
  78.  
  79.     /**
  80.      * Конструктор
  81.      */
  82.     public function __construct() {
  83.         @session_start();
  84.         $this->storage = &$_SESSION;
  85.         $this->init();
  86.     }
  87.  
  88.     /**
  89.      * Инициализация
  90.      */
  91.     public function init()
  92.     {
  93.         if (!isset($this->storage['game']) || !is_array(($this->storage['game']))) {
  94.             $this->storage['game'] = [];
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * Получить данные из памяти
  100.      */
  101.     public function get()
  102.     {
  103.         return $this->storage['game'];
  104.     }
  105.  
  106.     /**
  107.      * Добавить данные в память
  108.      */
  109.     public function add($value)
  110.     {
  111.         $this->storage['game'][] = $value;
  112.     }
  113.  
  114.     /**
  115.      * Сбросить память
  116.      */
  117.     public function reset()
  118.     {
  119.         $this->storage['game'] = [];
  120.     }
  121.  
  122.     /**
  123.      * Получить количество занятых слотов
  124.      */
  125.     public function getCount()
  126.     {
  127.         return count($this->storage['game']);
  128.     }
  129. }
  130.  
  131. /**
  132.  * Класс мини-контроллера.
  133.  */
  134. class Controller
  135. {
  136.     /**
  137.      *
  138.      */
  139.     public $errorPage = '404.php';
  140.  
  141.     private $request;
  142.     private $param;
  143.     private $safe;
  144.  
  145.     /**
  146.      *
  147.      */
  148.     public function __construct($request, $param = 'method', $safe = false)
  149.     {
  150.         @session_start();
  151.         $this->request = $request;
  152.         $this->param = $param;
  153.         $this->safe = $safe;
  154.     }
  155.  
  156.     /**
  157.      *
  158.      */
  159.     private function getSafePage()
  160.     {
  161.         if (isset($_SESSION['page'])) {
  162.             $page = $_SESSION['page'];
  163.             unset($_SESSION['page']);
  164.             return $page;
  165.         }
  166.     }
  167.  
  168.     /**
  169.      *
  170.      */
  171.     private function setSafePage($page)
  172.     {
  173.         $_SESSION['page'] = $page;
  174.         $this->redirect($_SERVER['PHP_SELF']);
  175.     }
  176.  
  177.     /**
  178.      *
  179.      */
  180.     private function redirect($to)
  181.     {
  182.         header('Location: ' . $to);
  183.     }
  184.  
  185.     /**
  186.      *
  187.      */
  188.     private function render()
  189.     {
  190.         if (isset($this->request[$this->param])) {
  191.             $method = $this->request[$this->param];
  192.             if (method_exists($this, $method)) {
  193.                 return $this->$method();
  194.             } else {
  195.                 // HTTP 404. Page not found
  196.                 $this->redirect($this->errorPage);
  197.             }
  198.         }
  199.  
  200.         return $this->index();
  201.     }
  202.  
  203.     /**
  204.      *
  205.      */
  206.     public function run()
  207.     {
  208.         if ($page = $this->getSafePage()) {
  209.             echo $page;
  210.         } elseif ($page = $this->render()) {
  211.             if ($this->safe) {
  212.                 $this->setSafePage($page);
  213.             }
  214.  
  215.             echo $page;
  216.         }
  217.     }
  218.  
  219.     /*** User action's ***/
  220.  
  221.     /**
  222.      * Action: Главная страница. Отображает результаты игры.
  223.      */
  224.     public function index()
  225.     {
  226.         $player = new Player();
  227.         return $player->getResult();
  228.     }
  229.  
  230.     /**
  231.      * Action: Бросить игровую кость
  232.      */
  233.     public function go()
  234.     {
  235.         $player = new Player();
  236.         $player->makeMove();
  237.         return $player->getResult();
  238.     }
  239.  
  240.     /**
  241.      * Action: Сбросить данные игрока
  242.      */
  243.     public function reset()
  244.     {
  245.         $player = new Player();
  246.         $player->resetPoints();
  247.         return $player->getResult();
  248.     }
  249. }
  250. ?>
  251.  
  252. <?php
  253. // Запуск приложения
  254. $controller = new Controller($_GET, 'method', true);
  255. $controller->run();
  256. ?>
  257.  
  258. <html>
  259.     <a href="?method=go">Бросить кость</a>
  260.     <a href="?method=reset">RESET</a>
  261. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement