private static void PlacePlayerShipsOnBoard(BattleShips battleShips, Player player, Menu menu) { string userShipPlacementChoice = ""; do { Console.WriteLine("Press 'R' for random placement or press 'C' to place the ships yourself."); Console.Write(">"); userShipPlacementChoice = Console.ReadLine()?.Trim().ToUpper() ?? "R"; } while (userShipPlacementChoice != "R" && userShipPlacementChoice != "C"); if (userShipPlacementChoice == "R") { battleShips.PlaceShipsAutomatically(player); } else { int nrOfShipsLeft = player.Ships.Count; foreach (var playerShip in player.Ships) { BattleShipsUI.PrintBoard(battleShips, player); Console.WriteLine($"Nr. of ships left to place: {nrOfShipsLeft}"); Console.WriteLine($"Ship width: {playerShip.Width}"); var shipOrientation = ""; do { Console.WriteLine("Ship orientation: Choose 'V' for vertical and 'H' for horizontal"); Console.Write(">"); shipOrientation = Console.ReadLine()?.Trim().ToUpper() ?? "V"; } while (shipOrientation != "V" && shipOrientation != "H"); Console.WriteLine($"Orientation: {shipOrientation}"); do { int startRow; int startCol; (startRow, _, _) = AskForUserInput("Choose the starting row coordinate for your ship", battleShips.Height, 1, menu); (startCol, _, _) = AskForUserInput("Choose the starting column coordinate for your ship", battleShips.Width, 1, menu); startRow--; startCol--; int endRow = startRow; int endCol = startCol; if (shipOrientation == "H") { for (var i = 1; i < playerShip.Width; i++) { endCol++; } playerShip.Orientation = ShipOrientation.Horizontal; } else { for (var i = 1; i < playerShip.Width; i++) { endRow++; } playerShip.Orientation = ShipOrientation.Horizontal; } if (endRow > battleShips.Height - 1 || endCol > battleShips.Width - 1) { Console.WriteLine("Ship ending row/column coordinate is out of bounds! Please try again"); continue; } List <Panel> affectedPanels = new List <Panel>(); if (!battleShips.ShipsCanTouch) { var orientation = shipOrientation == "H" ? 0 : 1; battleShips.FindAffectedPanelsAroundTheShip(player, orientation, affectedPanels, startRow, startCol, endRow, endCol); } else { affectedPanels.AddRange(player.GameBoard.Range(startRow, startCol, endRow, endCol)); } var shipPlacementPanels = player.GameBoard.Range(startRow, startCol, endRow, endCol); if (affectedPanels.Any(x => x.IsOccupied)) { var message = "A ship has already been placed here!"; Console.WriteLine(!battleShips.ShipsCanTouch ? $"{message} Also the ships cannot touch! Please try again" : $"{message} Please try again"); continue; } playerShip.StartCol = startCol; playerShip.StartRow = startRow; playerShip.EndCol = endCol; playerShip.EndRow = endRow; foreach (var panel in shipPlacementPanels) { panel.PanelState = playerShip.PanelState; } nrOfShipsLeft--; break; } while (true); } } }