private function anchorShip(Ship $ship)
 {
     while (!$ship->isAnchored()) {
         $direction = rand() % 2 ? BattlefieldCreator::DIRECTION_VERTICAL : BattlefieldCreator::DIRECTION_HORIZONTAL;
         $startColumn = rand(BattlefieldCreator::MIN_COLUMNS, BattlefieldCreator::MAX_COLUMNS);
         $startRow = rand(BattlefieldCreator::MIN_ROWS, BattlefieldCreator::MAX_ROWS);
         $startPoint = new Coordinate($startRow, $startColumn);
         $anchorPoints = $this->generateAnchorPoints($startPoint, $ship->getSize(), $direction);
         if (!$this->hasCollision($anchorPoints)) {
             $ship->setAnchorPoints($anchorPoints);
         }
     }
 }
예제 #2
0
 /**
  * @param Ship $ship
  * @return bool
  */
 public function isShipSunk(Ship $ship)
 {
     foreach ($ship->getAnchorPoints() as $point) {
         if (!in_array($point, $this->hits)) {
             return false;
         }
     }
     return true;
 }