PHP Zeeslag
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.

index.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <html>
  2. <!-- Hier start het php-script te lopen -->
  3. <?php
  4. session_start();
  5. class Sub
  6. {
  7. public $lengte;
  8. public $pos = rand(0,20);
  9. public $ammo;
  10. public $shot;
  11. protected $begin;
  12. private $eind;
  13. public $midden;
  14. public $schoten = [];
  15. function start()
  16. {
  17. $this->begin = $this->midden - floor($this->lengte / 2);
  18. }
  19. function end()
  20. {
  21. $this->einde = $this->begin + $this->lengte - 1;
  22. }
  23. function positie(){
  24. if (!isset($_SESSION['pos'])){
  25. $this ->begin = $_SESSION["begin"];
  26. $this ->einde = $_SESSION["einde"];
  27. $this ->pos = $_SESSION["pos"];
  28. $this ->schoten = $_SESSION["schoten"];
  29. $this ->lengte = $_SESSION["lengte"];
  30. }
  31. if (!isset($_SESSION['ammo'])) {
  32. $this ->ammo = $_SESSION["ammo"];
  33. }
  34. }
  35. function maxSubmarines($max,$lengte){
  36. }
  37. }
  38. ?>
  39. <body>
  40. <?php
  41. // Naar php-modus
  42. if (!isset($_SESSION['pos'])) {
  43. $lengte = 3;
  44. $pos = rand(0, 20);
  45. $_SESSION["begin"] = $pos - floor($lengte / 2);
  46. $_SESSION["einde"] = $pos + $lengte - 1;
  47. $_SESSION["pos"] = $pos;
  48. $_SESSION["schoten"] = [];
  49. $_SESSION["lengte"] = $lengte;
  50. }
  51. if (!isset($_SESSION['pos2'])) {
  52. $lengte2 = 4;
  53. $pos2 = rand(0, 20);
  54. $_SESSION["begin2"] = $pos2 - floor($lengte2 / 2);
  55. $_SESSION["einde2"] = $pos2 + $lengte2 - 1;
  56. $_SESSION["pos2"] = $pos2;
  57. $_SESSION["schoten2"] = [];
  58. $_SESSION["lengte2"] = $lengte2;
  59. }
  60. if (!isset($_SESSION['pos3'])) {
  61. $lengte3 = 5;
  62. $pos2 = rand(0, 20);
  63. $_SESSION["begin3"] = $pos3 - floor($lengte3 / 2);
  64. $_SESSION["einde3"] = $pos3 + $lengte3 - 1;
  65. $_SESSION["pos3"] = $pos3;
  66. $_SESSION["schoten3"] = [];
  67. $_SESSION["lengte3"] = $lengte3;
  68. }
  69. if (!isset($_SESSION['ammo'])) {
  70. $ammo = 5;
  71. $_SESSION["ammo"] = $ammo;
  72. }
  73. if (isset($_POST["shot"])) {
  74. $_SESSION["ammo"]--;
  75. //if ($_POST["shot"] == $_SESSION['pos']) {
  76. // if ($_POST["shot"] >= $_SESSION["begin"] && $_POST["shot"] <= $_SESSION["einde"]) {
  77. if ($_POST["shot"] >= $_SESSION["begin"] && $_POST["shot"] <= $_SESSION["einde"]) {
  78. // RAAK
  79. $schoten = $_SESSION["schoten"];
  80. if (!in_array($_POST["shot"], $schoten)) {
  81. $schoten[] = $_POST["shot"]; // Dit is de manier om items TOE TE VOEGEN aan een array
  82. $_SESSION["schoten"] = $schoten;
  83. }
  84. if (count($schoten) == $_SESSION["lengte"]) {
  85. //WON
  86. }
  87. } elseif ($_POST["shot"] >= $_SESSION["begin2"] && $_POST["shot"] <= $_SESSION["einde2"]) {
  88. //RAAK DB2
  89. $schoten = $_SESSION["schoten2"];
  90. if (!in_array($_POST["shot"], $schoten)) {
  91. $schoten[] = $_POST["shot"]; // Dit is de manier om items TOE TE VOEGEN aan een array
  92. $_SESSION["schoten2"] = $schoten;
  93. }
  94. if (count($schoten) == $_SESSION["lengte2"]) {
  95. //WON
  96. }
  97. } elseif ($_POST["shot"] >= $_SESSION["begin3"] && $_POST["shot"] <= $_SESSION["einde3"]) {
  98. //RAAK DB3
  99. $schoten = $_SESSION["schoten3"];
  100. if (!in_array($_POST["shot"], $schoten)) {
  101. $schoten[] = $_POST["shot"]; // Dit is de manier om items TOE TE VOEGEN aan een array
  102. $_SESSION["schoten3"] = $schoten;
  103. }
  104. if (count($schoten) == $_SESSION["lengte3"]) {
  105. //WON
  106. }
  107. } elseif ($_SESSION['ammo'] == 0) {
  108. // MIS
  109. }
  110. }
  111. // while ($ammo > 0 && !$win) {
  112. ?>
  113. <!-- Terug in HTML modus -->
  114. <form action="/zeeslag/index.php" method="POST">
  115. <label for="shot">Shot: </label><br>
  116. <input type="text" id="shot" name="shot" required>
  117. <input type="submit" value="submit">
  118. </form>
  119. <?php
  120. // In PHP
  121. //if ($_POST['submit']) {
  122. //$win = true;
  123. // }
  124. //}
  125. ?>
  126. <!-- HTML -->
  127. </body>
  128. <!-- Hier wordt de pagina met gerenderd formulier naar de browser gestuurd -->
  129. </html>