123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- require_once "dbase.inc";
- require_once "login.inc";
-
- class game
- {
-
- //properties
- protected $shipList = ['duikboot', 'torpedojager', 'kruiser', 'slagschip', 'vliegdekship'];
- public $difficulty;
- public $ammo;
- public $shotCoor = [];
- public $hitCoor = [];
- protected $hitCount = 0;
- public $gameResult = null;
- public $ships;
-
- //methods
- protected function setDifficulty()
- {
- if (isset($_POST['difficulty']) && $_POST['difficulty'] != "") {
- $this->difficulty = $_POST['difficulty'];
- }
- }
-
- protected function createShips()
- {
- $tempShips = [];
- $usedCoord = [];
-
- for ($i = 0; $i < 2; $i++) {
- $tempShips[] = new $this->shipList[rand(0, 4)]();
- $usedCoord[] = $tempShips[$i]->printCoord();
-
- //kijk na of de schepen overlappen
- if ($i > 0) {
- if (
- $usedCoord[1][0] >= $usedCoord[0][0] &&
- $usedCoord[1][0] <= $usedCoord[0][1] ||
- $usedCoord[1][1] >= $usedCoord[0][0] &&
- $usedCoord[1][1] <= $usedCoord[0][1]
- ) {
- array_pop($tempShips);
- array_pop($usedCoord);
- $i--;
- }
- }
- }
- $this->ships = $tempShips;
- }
-
- //amount of ammo depends on difficulty
- protected function setAmmo()
- {
- if ($this->difficulty == 'easy') {
- $this->ammo = 35;
- } elseif ($this->difficulty == 'medium') {
- $this->ammo = 25;
- } elseif ($this->difficulty == 'hard') {
- $this->ammo = 15;
- }
- }
-
- //after shot, check if ship was hit
- public function hitCheck($shot)
- {
- $this->shotCoor[] = $shot;
-
- $this->ammo--;
- for ($i = 0; $i < count($this->ships); $i++) {
- $result = $this->ships[$i]->compareShot($shot);
- $this->hitCount += $result;
- if ($result == 1) {
- array_pop($this->shotCoor);
- $this->hitCoor[] = $shot;
- }
- }
-
- if ($this->hitCount == $this->ships[0]->printLengte() + $this->ships[1]->printLengte()) {
-
- $this->gameResult = 1;
- $this->saveResult();
- } elseif ($this->ammo == 0) {
- $this->gameResult = 0;
- $this->saveResult();
- }
- }
-
- //send result from game to db
- public function saveResult()
- {
- global $dbase;
- if ($this->gameResult == 0 || $this->gameResult == 1) {
- $dbase->insertResult();
- }
- }
-
- public function __construct()
- {
- $this->setDifficulty();
- $this->createShips();
- $this->setAmmo();
- }
- }
-
- // outside of game class
- function gameStartup()
- {
- $_SESSION["game"] = new game;
- }
-
- //check current status game(select difficulty, play game, state after win/loss)
- function runGame()
- {
- if (isset($_POST["difficulty"])) {
- gameStartup();
- } elseif (isset($_POST["shot"])) {
- $shot = test_input($_POST["shot"]);
- $_SESSION["game"]->hitCheck($shot);
- } elseif (isset($_SESSION["game"]) && $_SESSION["game"]->gameResult !== null) {
- unset($_SESSION["game"]);
- }
- }
-
- //check if input is usable.
- function validateInput()
- {
- if ((isset($_SESSION["game"]) && $_SESSION['game']->gameResult === null) && ((int)$_POST["shot"] < 0 || (int)$_POST["shot"] > $_SESSION["game"]->ships[0]->printDokLengte() - 1)) {
- return "<span class=\"errormessage\">Verkeerde input. Cijfer moet tussen 0 en " . (string)($_SESSION["game"]->ships[0]->printDokLengte() - 1) . " zijn</span>";
- } else {
- runGame();
- }
- }
|