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'; } } }
public function testAllShipsSunk() { $board = new Board(10); $this->assertEmpty($board->getShips()); $board->addShip(new Ship('Submarine', 3)); $this->assertFalse($board->allShipsSunk()); }