123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
-
- // Na verloop van vorige code liep ik op een dood einde. Na lessen van opnieuw te bekijken ivm klasses heb ik nu dit.
-
- require_once 'ship.php';
-
- class Board
- {
-
-
- protected $currentShips;
- protected $posArr;
-
- // We constructen de array die het spelbord zal vullen
-
- function __construct($difficulty)
- {
- $this->initShips($difficulty);
- }
-
- // We zetten een difficulty
-
- protected function initShips($difficulty)
- {
-
-
-
- if ($difficulty == 'Easy') {
- $this->currentShips[0] = new torpedojager();
- $this->currentShips[1] = new duikboot();
-
-
- $_SESSION['ammo'] = 30;
- } else if ($difficulty == 'Normal') {
- $this->currentShips[0] = new slagSchip();
- $this->currentShips[1] = new kruiser();
- $_SESSION['ammo'] = 24;
- } else if ($difficulty == 'Hard') {
- $this->currentShips[0] = new vliegdekSchip();
- $this->currentShips[1] = new slagSchip();
- $_SESSION['ammo'] = 16;
- }
-
-
-
- $this->posArr[0][0] = $this->currentShips[0]->returnPosBegin();
- $this->posArr[0][1] = $this->currentShips[0]->returnPosEind();
- $this->posArr[1][0] = $this->currentShips[1]->returnPosBegin();
- $this->posArr[1][1] = $this->currentShips[1]->returnPosEind();
-
-
- while ($this->legalShipPosition($this->currentShips[1]->posArr(), $this->posArr[0][0], $this->posArr[0][1])){
- $this->initShips($difficulty);
- }
-
- }
-
- //Overlappingen tegengaan
-
- protected function legalShipPosition($arr, $firstPos, $lastPos)
- {
- foreach ($arr as $value) {
- if ($value >= $firstPos && $value <= $lastPos) {
- return true;
- }
-
- }
-
- return false;
-
- }
-
-
- // Actieve ships returnen
-
- public function returnCurrentShips()
- {
- return $this->currentShips;
- }
-
-
- public function printBoard($shots)
- {
- // Hier uw code maken voor uw bord te printen:
- $html = "<pre>";
- for ($y = 0; $y < 3; $y++) {
- for ($x = 0; $x < 52; $x++) {
- if ($y !== 1) {
- if ($x == 0 || $x == 51) {
- $html .= "+";
- } else {
- $html .= "-";
- }
- } elseif ($x == 0 || $x == 51) {
- $html .= "|";
- } else {
-
-
- // Liep wat vast bij het verwijderen van het huidige shot, heb hiervoor de raad van klasgenoten gevraagd.
-
- if (!empty($shots)) {
- if (
- in_array($x, $shots)
- // || in_array($x, $_SESSION['currentShips']->returnCurrentShips()[1]->posArr())
- ) {
- if (
- in_array($x, $_SESSION['currentShips']->returnCurrentShips()[0]->posArr()) ||
- in_array($x, $_SESSION['currentShips']->returnCurrentShips()[1]->posArr())
- ) {
-
-
- // if (
- // $x == $_SESSION['currentShips']->returnCurrentShips()[0]->returnPosEind() ||
- // $x == $_SESSION['currentShips']->returnCurrentShips()[1]->returnPosEind()
- // ) {
- // $html .= ">";
- // } else {
- $html .= "x";
- // }
- } else {
- $html .= "o";
- }
- } else {
- $html .= " ";
- }
- } else {
- $html .= " ";
- }
- }
- }
-
- $html .= "\n";
- }
-
-
- $html .= "</pre>";
-
- return $html;
- }
- }
|