private void TryToRandomlyPlaceShips() { var randomX = random.Next(0, 10); var randomY = random.Next(0, 10); var randomCoordinate = new Coordinate(randomX, randomY, new HitState()); if (random.Next(0, 2) == 0) { CurrentShipToPlace.Direction = Ship.ShipDirection.Horizontal; } else { CurrentShipToPlace.Direction = Ship.ShipDirection.Vertical; } CurrentShipToPlace.BowCoordinate = randomCoordinate; List <Coordinate> coordslist; bool isValidPlacement; ValidateShipPlacement(out coordslist, out isValidPlacement); if (!isValidPlacement) { TryToRandomlyPlaceShips(); } else { Console.WriteLine($"{Environment.NewLine}Placing ship {CurrentShipToPlace.Type} for {CurrentPlayer.PlayerName} facing {CurrentShipToPlace.Direction} starting at {CurrentShipToPlace.BowCoordinate.X}, {CurrentShipToPlace.BowCoordinate.Y}"); CurrentShipToPlace.InitializeCoordinateList(CurrentShipToPlace, coordslist); CurrentPlayer.PlayerOccupiedCoordinates.AddRange(coordslist); var toRemove = PlacementState.Where(x => x.Type == CurrentShipToPlace.Type).First(); PlacementState.Remove(toRemove); if (PlacementState.Any()) { CurrentShipToPlace = PlacementState[0]; TryToRandomlyPlaceShips(); } else { CurrentGameMode = GameModes.Battle; CurrentPlayer = Player1; Console.WriteLine("Starting battle!!"); } } }
private void PlaceShipOnGameBoard(List <Coordinate> coordslist) { Console.WriteLine($"{Environment.NewLine}Placing ship {CurrentShipToPlace.Type} for {CurrentPlayer.PlayerName} facing {CurrentShipToPlace.Direction} starting at {CurrentShipToPlace.BowCoordinate.X}, {CurrentShipToPlace.BowCoordinate.Y}"); CurrentShipToPlace.InitializeCoordinateList(CurrentShipToPlace, coordslist); CurrentPlayer.PlayerOccupiedCoordinates.AddRange(coordslist); var toRemove = PlacementState.Where(x => x.Type == CurrentShipToPlace.Type).First(); PlacementState.Remove(toRemove); if (PlacementState.Any()) { CurrentShipToPlace = PlacementState[0]; } else { CurrentGameMode = GameModes.Battle; } }