Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Класс "Игрок".
- */
- class Player
- {
- private $memory;
- private $dice;
- /**
- * Конструктор.
- */
- public function __construct()
- {
- // Инициализируем память
- $this->memory = new Memory();
- // Даём игроку кубик
- $this->dice = new Dice();
- }
- /**
- * Сделать бросок кости
- */
- public function makeMove()
- {
- // Бросаем кость
- $points = $this->dice->drop();
- // Сохраняем счет
- $this->memory->add($points);
- }
- /**
- * Сбросить счёт
- */
- public function resetPoints()
- {
- $this->memory->reset();
- }
- /**
- * Получить результаты в виде HTML таблицы
- */
- public function getResult()
- {
- $out = '<table><caption>Результаты</caption><tr>';
- foreach ($this->memory->get() as $step => $points) {
- $step++;
- $out .= "<td>Бросок #$step<br>Кость: $points</td>";
- }
- $out .= '</tr>';
- $out .= '</table>';
- return $out;
- }
- }
- /**
- * Класс "Игровая кость"
- */
- class Dice
- {
- /**
- * Сделать бросок кости
- */
- public function drop()
- {
- return mt_rand(1, 6);
- }
- }
- /**
- * Класс "памяти"
- */
- class Memory
- {
- private $storage;
- /**
- * Конструктор
- */
- public function __construct() {
- @session_start();
- $this->storage = &$_SESSION;
- $this->init();
- }
- /**
- * Инициализация
- */
- public function init()
- {
- if (!isset($this->storage['game']) || !is_array(($this->storage['game']))) {
- $this->storage['game'] = [];
- }
- }
- /**
- * Получить данные из памяти
- */
- public function get()
- {
- return $this->storage['game'];
- }
- /**
- * Добавить данные в память
- */
- public function add($value)
- {
- $this->storage['game'][] = $value;
- }
- /**
- * Сбросить память
- */
- public function reset()
- {
- $this->storage['game'] = [];
- }
- /**
- * Получить количество занятых слотов
- */
- public function getCount()
- {
- return count($this->storage['game']);
- }
- }
- /**
- * Класс мини-контроллера.
- */
- class Controller
- {
- /**
- *
- */
- public $errorPage = '404.php';
- private $request;
- private $param;
- private $safe;
- /**
- *
- */
- public function __construct($request, $param = 'method', $safe = false)
- {
- @session_start();
- $this->request = $request;
- $this->param = $param;
- $this->safe = $safe;
- }
- /**
- *
- */
- private function getSafePage()
- {
- if (isset($_SESSION['page'])) {
- $page = $_SESSION['page'];
- unset($_SESSION['page']);
- return $page;
- }
- }
- /**
- *
- */
- private function setSafePage($page)
- {
- $_SESSION['page'] = $page;
- $this->redirect($_SERVER['PHP_SELF']);
- }
- /**
- *
- */
- private function redirect($to)
- {
- header('Location: ' . $to);
- }
- /**
- *
- */
- private function render()
- {
- if (isset($this->request[$this->param])) {
- $method = $this->request[$this->param];
- if (method_exists($this, $method)) {
- return $this->$method();
- } else {
- // HTTP 404. Page not found
- $this->redirect($this->errorPage);
- }
- }
- return $this->index();
- }
- /**
- *
- */
- public function run()
- {
- if ($page = $this->getSafePage()) {
- echo $page;
- } elseif ($page = $this->render()) {
- if ($this->safe) {
- $this->setSafePage($page);
- }
- echo $page;
- }
- }
- /*** User action's ***/
- /**
- * Action: Главная страница. Отображает результаты игры.
- */
- public function index()
- {
- $player = new Player();
- return $player->getResult();
- }
- /**
- * Action: Бросить игровую кость
- */
- public function go()
- {
- $player = new Player();
- $player->makeMove();
- return $player->getResult();
- }
- /**
- * Action: Сбросить данные игрока
- */
- public function reset()
- {
- $player = new Player();
- $player->resetPoints();
- return $player->getResult();
- }
- }
- ?>
- <?php
- // Запуск приложения
- $controller = new Controller($_GET, 'method', true);
- $controller->run();
- ?>
- <html>
- <a href="?method=go">Бросить кость</a>
- <a href="?method=reset">RESET</a>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement