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.

gamevisual.inc 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. require_once "dbase.inc";
  3. require_once "gamefunction.inc";
  4. //tables
  5. //display number for each table cell
  6. function theadVisualTable()
  7. {
  8. if (isset($_SESSION['game'])) {
  9. for ($i = 0; $i < $_SESSION["game"]->ships[0]->printDokLengte(); $i++) {
  10. ?>
  11. <th>
  12. <?= $i; ?>
  13. </th>
  14. <?php }
  15. }
  16. }
  17. //show if there was hit, miss or endresult in table.
  18. function tbodyVisualTable()
  19. {
  20. if (isset($_SESSION['game'])) {
  21. for ($i = 0; $i < $_SESSION["game"]->ships[0]->printDokLengte(); $i++) { ?>
  22. <td>
  23. <?php
  24. if (in_array($i, $_SESSION["game"]->hitCoor)) {
  25. echo 'X';
  26. } elseif (isset($_SESSION["game"]) && $_SESSION["game"]->ammo <= 0 || $_SESSION['game']->gameResult == 1) {
  27. $shipArray = [$_SESSION["game"]->ships[0]->printCoord(), $_SESSION["game"]->ships[1]->printCoord()];
  28. if (($i >= $shipArray[0][0] && $i <= $shipArray[0][1]) ||
  29. ($i >= $shipArray[1][0] && $i <= $shipArray[1][1])
  30. ) {
  31. echo '=';
  32. } elseif ($i == $shipArray[0][0] - 1 && $i == $shipArray[1][1] + 1 || $i == $shipArray[1][0] - 1 && $i == $shipArray[0][1] + 1) {
  33. echo '><';
  34. } elseif ($i == $shipArray[0][0] - 1 || $i == $shipArray[1][0] - 1) {
  35. echo '<';
  36. } elseif ($i == $shipArray[0][1] + 1 || $i == $shipArray[1][1] + 1) {
  37. echo '>';
  38. } elseif (in_array($i, $_SESSION["game"]->shotCoor)) {
  39. echo 'O';
  40. }
  41. } elseif (in_array($i, $_SESSION["game"]->shotCoor)) {
  42. echo 'O';
  43. }
  44. ?>
  45. </td><?php
  46. }
  47. }
  48. }
  49. //show highscores of all users
  50. function tbodyHighScores()
  51. {
  52. global $dbase;
  53. $queryHighScore = $dbase->highScoreQuery();
  54. while ($result = $queryHighScore->fetchArray(SQLITE3_NUM)) {
  55. ?>
  56. <tr>
  57. <td>
  58. <?= $result[0]; ?>
  59. </td>
  60. <td>
  61. <?= $result[1]; ?>
  62. </td>
  63. </tr>
  64. <?php
  65. }
  66. }
  67. //show higscores of current users
  68. function tbodyUserScores()
  69. {
  70. global $dbase;
  71. $queryUserScore = $dbase->userScoreQuery($_SESSION['spelerId']);
  72. while ($result = $queryUserScore->fetchArray(SQLITE3_NUM)) {
  73. ?>
  74. <tr>
  75. <td>
  76. <?= $result[0]; ?>
  77. </td>
  78. <td>
  79. <?php
  80. if ($result[1] == 1) {
  81. echo "win";
  82. } else {
  83. echo "loss";
  84. }
  85. ?>
  86. </td>
  87. <td>
  88. <?= $result[2]; ?>
  89. </td>
  90. </tr>
  91. <?php
  92. }
  93. }
  94. // forms
  95. //select difficulty at start game
  96. function difficultyForm()
  97. {
  98. if (!(isset($_SESSION['game']))) {
  99. ?>
  100. <label for="difficulty-select">Choose difficulty:</label>
  101. <select name="difficulty" id="difficulty-select" class="difficulty__selection">
  102. <option value="">--please choose the difficulty--</option>
  103. <option value="easy">easy</option>
  104. <option value="medium">medium</option>
  105. <option value="hard">hard</option>
  106. </select>
  107. <?php
  108. }
  109. }
  110. //input shot during game
  111. function shotForm()
  112. {
  113. if (isset($_SESSION['game']) && $_SESSION['game']->gameResult === null) {
  114. ?>
  115. <label for="shot">Shot: </label><br>
  116. <input type="number" id="shot" name="shot" class="game__selection" required autofocus>
  117. <?php
  118. }
  119. }
  120. //change name submit button at end game.
  121. function submitButtonValue()
  122. {
  123. if (isset($_SESSION['game']) && $_SESSION['game']->gameResult !== null) {
  124. ?> value="try again"
  125. <?php } else { ?>
  126. value="submit"
  127. <?php
  128. }
  129. }
  130. //show if player has won or lost at end game.
  131. function showGameResult()
  132. {
  133. if (isset($_SESSION['game']) && $_SESSION['game']->gameResult === 0) {
  134. ?>
  135. <h3> <?= "lost"; ?> </h3>
  136. <?php
  137. } elseif (isset($_SESSION['game']) && $_SESSION['game']->gameResult === 1) {
  138. ?>
  139. <h3> <?= "win"; ?> </h3>
  140. <?php
  141. }
  142. }
  143. //show shots left during game, or at end game if player has won.
  144. function showShotsLeft()
  145. {
  146. if (isset($_POST['shot']) && $_SESSION['game']->ammo > 0 || isset($_POST['difficulty'])) {
  147. ?>
  148. shots left:
  149. <?php echo $_SESSION['game']->ammo;
  150. }
  151. }