You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gamefunction.inc 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. require_once "dbase.inc";
  3. require_once "login.inc";
  4. class game
  5. {
  6. //properties
  7. protected $shipList = ['duikboot', 'torpedojager', 'kruiser', 'slagschip', 'vliegdekship'];
  8. public $difficulty;
  9. public $ammo;
  10. public $shotCoor = [];
  11. public $hitCoor = [];
  12. protected $hitCount = 0;
  13. public $gameResult = null;
  14. public $ships;
  15. //methods
  16. protected function setDifficulty()
  17. {
  18. if (isset($_POST['difficulty']) && $_POST['difficulty'] != "") {
  19. $this->difficulty = $_POST['difficulty'];
  20. }
  21. }
  22. protected function createShips()
  23. {
  24. $tempShips = [];
  25. $usedCoord = [];
  26. for ($i = 0; $i < 2; $i++) {
  27. $tempShips[] = new $this->shipList[rand(0, 4)]();
  28. $usedCoord[] = $tempShips[$i]->printCoord();
  29. //kijk na of de schepen overlappen
  30. if ($i > 0) {
  31. if (
  32. $usedCoord[1][0] >= $usedCoord[0][0] &&
  33. $usedCoord[1][0] <= $usedCoord[0][1] ||
  34. $usedCoord[1][1] >= $usedCoord[0][0] &&
  35. $usedCoord[1][1] <= $usedCoord[0][1]
  36. ) {
  37. array_pop($tempShips);
  38. array_pop($usedCoord);
  39. $i--;
  40. }
  41. }
  42. }
  43. $this->ships = $tempShips;
  44. }
  45. //amount of ammo depends on difficulty
  46. protected function setAmmo()
  47. {
  48. if ($this->difficulty == 'easy') {
  49. $this->ammo = 35;
  50. } elseif ($this->difficulty == 'medium') {
  51. $this->ammo = 25;
  52. } elseif ($this->difficulty == 'hard') {
  53. $this->ammo = 15;
  54. }
  55. }
  56. //after shot, check if ship was hit
  57. public function hitCheck($shot)
  58. {
  59. $this->shotCoor[] = $shot;
  60. $this->ammo--;
  61. for ($i = 0; $i < count($this->ships); $i++) {
  62. $result = $this->ships[$i]->compareShot($shot);
  63. $this->hitCount += $result;
  64. if ($result == 1) {
  65. array_pop($this->shotCoor);
  66. $this->hitCoor[] = $shot;
  67. }
  68. }
  69. if ($this->hitCount == $this->ships[0]->printLengte() + $this->ships[1]->printLengte()) {
  70. $this->gameResult = 1;
  71. $this->saveResult();
  72. } elseif ($this->ammo == 0) {
  73. $this->gameResult = 0;
  74. $this->saveResult();
  75. }
  76. }
  77. //send result from game to db
  78. public function saveResult()
  79. {
  80. global $dbase;
  81. if ($this->gameResult == 0 || $this->gameResult == 1) {
  82. $dbase->insertResult();
  83. }
  84. }
  85. public function __construct()
  86. {
  87. $this->setDifficulty();
  88. $this->createShips();
  89. $this->setAmmo();
  90. }
  91. }
  92. // outside of game class
  93. function gameStartup()
  94. {
  95. $_SESSION["game"] = new game;
  96. }
  97. //check current status game(select difficulty, play game, state after win/loss)
  98. function runGame()
  99. {
  100. if (isset($_POST["difficulty"])) {
  101. gameStartup();
  102. } elseif (isset($_POST["shot"])) {
  103. $shot = test_input($_POST["shot"]);
  104. $_SESSION["game"]->hitCheck($shot);
  105. } elseif (isset($_SESSION["game"]) && $_SESSION["game"]->gameResult !== null) {
  106. unset($_SESSION["game"]);
  107. }
  108. }
  109. //check if input is usable.
  110. function validateInput()
  111. {
  112. if ((isset($_SESSION["game"]) && $_SESSION['game']->gameResult === null) && ((int)$_POST["shot"] < 0 || (int)$_POST["shot"] > $_SESSION["game"]->ships[0]->printDokLengte() - 1)) {
  113. return "<span class=\"errormessage\">Verkeerde input. Cijfer moet tussen 0 en " . (string)($_SESSION["game"]->ships[0]->printDokLengte() - 1) . " zijn</span>";
  114. } else {
  115. runGame();
  116. }
  117. }