Example #1
0
 public function testAllShipsSunk()
 {
     $board = new Board(10);
     $this->assertEmpty($board->getShips());
     $board->addShip(new Ship('Submarine', 3));
     $this->assertFalse($board->allShipsSunk());
 }
Example #2
0
 private function makeShot()
 {
     try {
         $shotPoint = $this->processUserInput();
         $hit = false;
         foreach ($this->board->getShips() as $ship) {
             if ($ship->checkPoint($shotPoint)) {
                 $hit = true;
                 if (!in_array($shotPoint, $this->board->shots['hit'])) {
                     $ship->hit($shotPoint);
                     $this->shotResult = "Hit";
                 }
                 if ($ship->isSunk()) {
                     $this->shotResult = "Sunk";
                 }
                 break;
             }
         }
         $this->board->shots['counter'] += 1;
         if ($hit) {
             $this->board->shots['hit'][] = $shotPoint;
         } else {
             $this->board->shots['miss'][] = $shotPoint;
             $this->shotResult = "Miss";
         }
     } catch (\Exception $e) {
         if (trim($this->userInput) == 'show') {
             $this->cheatMode = true;
         } else {
             $this->shotResult = 'Error';
         }
     }
 }
 /**
  * Helper function to draw a single spot on the board.
  * @param  Point $point the point to draw
  *
  * @return string String containing the output for this spot (X, -, .)
  */
 private function drawSector(Point $point, $cheatMode)
 {
     if ($cheatMode) {
         if (!$this->board->freeSpot($point)) {
             return ' X ';
         }
         return '   ';
     }
     if (in_array($point, $this->board->shots['hit'])) {
         return " X ";
     }
     if (in_array($point, $this->board->shots['miss'])) {
         return " - ";
     }
     return " . ";
 }