private static bool TryPlaceShipInDirection(Direction direction, GameBoard board, Cell fromCell, IShip ship) { var emptyCells = new List <Cell> { fromCell }; Cell neighbourCell = null; for (int shipSize = 1; shipSize < ship.Size; shipSize++) { var neighbourCoordinate = CoordinateService.GetNeighbour(direction, neighbourCell ?? fromCell); if (neighbourCoordinate == null) { break; } neighbourCell = board.Cells[neighbourCoordinate.Row][neighbourCoordinate.Column]; if (neighbourCell.IsOccupied) { break; } emptyCells.Add(board.Cells[neighbourCoordinate.Row][neighbourCoordinate.Column]); } if (emptyCells.Count != ship.Size) { return(false); } PlaceShipAt(emptyCells, ship); return(true); }
public static GameBoard GenerateRandomBoard() { var board = GenerateEmptyBoard(); var ships = new Queue <IShip>(); foreach (var ship in Constants.Ships) { ships.Enqueue(ship); } while (ships.Any()) { var randomCoordinate = CoordinateService.GetRandomCoordinate(); var randomCell = board.Cells[randomCoordinate.Row][randomCoordinate.Column]; if (randomCell.IsOccupied) { continue; } var ship = ships.Dequeue(); var direction = Directions.GetRandom(); if (TryPlaceShipInDirection(direction, board, randomCell, ship)) { board.Ships.Add(ship); continue; } ships.Enqueue(ship); } return(board); }