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.

board.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // Na verloop van vorige code liep ik op een dood einde. Na lessen van opnieuw te bekijken ivm klasses heb ik nu dit.
  3. require_once 'ship.php';
  4. class Board
  5. {
  6. protected $currentShips;
  7. protected $posArr;
  8. // We constructen de array die het spelbord zal vullen
  9. function __construct($difficulty)
  10. {
  11. $this->initShips($difficulty);
  12. }
  13. // We zetten een difficulty
  14. protected function initShips($difficulty)
  15. {
  16. if ($difficulty == 'Easy') {
  17. $this->currentShips[0] = new torpedojager();
  18. $this->currentShips[1] = new duikboot();
  19. $_SESSION['ammo'] = 30;
  20. } else if ($difficulty == 'Normal') {
  21. $this->currentShips[0] = new slagSchip();
  22. $this->currentShips[1] = new kruiser();
  23. $_SESSION['ammo'] = 24;
  24. } else if ($difficulty == 'Hard') {
  25. $this->currentShips[0] = new vliegdekSchip();
  26. $this->currentShips[1] = new slagSchip();
  27. $_SESSION['ammo'] = 16;
  28. }
  29. $this->posArr[0][0] = $this->currentShips[0]->returnPosBegin();
  30. $this->posArr[0][1] = $this->currentShips[0]->returnPosEind();
  31. $this->posArr[1][0] = $this->currentShips[1]->returnPosBegin();
  32. $this->posArr[1][1] = $this->currentShips[1]->returnPosEind();
  33. while ($this->legalShipPosition($this->currentShips[1]->posArr(), $this->posArr[0][0], $this->posArr[0][1])){
  34. $this->initShips($difficulty);
  35. }
  36. }
  37. //Overlappingen tegengaan
  38. protected function legalShipPosition($arr, $firstPos, $lastPos)
  39. {
  40. foreach ($arr as $value) {
  41. if ($value >= $firstPos && $value <= $lastPos) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. // Actieve ships returnen
  48. public function returnCurrentShips()
  49. {
  50. return $this->currentShips;
  51. }
  52. public function printBoard($shots)
  53. {
  54. // Hier uw code maken voor uw bord te printen:
  55. $html = "<pre>";
  56. for ($y = 0; $y < 3; $y++) {
  57. for ($x = 0; $x < 52; $x++) {
  58. if ($y !== 1) {
  59. if ($x == 0 || $x == 51) {
  60. $html .= "+";
  61. } else {
  62. $html .= "-";
  63. }
  64. } elseif ($x == 0 || $x == 51) {
  65. $html .= "|";
  66. } else {
  67. // Liep wat vast bij het verwijderen van het huidige shot, heb hiervoor de raad van klasgenoten gevraagd.
  68. if (!empty($shots)) {
  69. if (
  70. in_array($x, $shots)
  71. // || in_array($x, $_SESSION['currentShips']->returnCurrentShips()[1]->posArr())
  72. ) {
  73. if (
  74. in_array($x, $_SESSION['currentShips']->returnCurrentShips()[0]->posArr()) ||
  75. in_array($x, $_SESSION['currentShips']->returnCurrentShips()[1]->posArr())
  76. ) {
  77. // if (
  78. // $x == $_SESSION['currentShips']->returnCurrentShips()[0]->returnPosEind() ||
  79. // $x == $_SESSION['currentShips']->returnCurrentShips()[1]->returnPosEind()
  80. // ) {
  81. // $html .= ">";
  82. // } else {
  83. $html .= "x";
  84. // }
  85. } else {
  86. $html .= "o";
  87. }
  88. } else {
  89. $html .= " ";
  90. }
  91. } else {
  92. $html .= " ";
  93. }
  94. }
  95. }
  96. $html .= "\n";
  97. }
  98. $html .= "</pre>";
  99. return $html;
  100. }
  101. }