public static void Main() { InitializeGame(); int ShipChosen = 0; bool isInitialPositionChosen = false; bool isShipPlacedOnBoard = false; bool isGameActive = false; comPlayer = (player2.GetType() == player1.GetType()) ? false : true; Player playerVariable = player1; while (playerVariable.areShipsEmpty() == false) { Console.Clear(); Board.DisplayBoard(playerVariable.board); if (playerVariable.areShipsFull()) { Console.WriteLine("Player {0}, you must choose where to put your ships", (playerVariable == player1) ? 1 : 2); } Console.WriteLine("Which ship would you like to place?"); if (playerVariable.areShipsEmpty() == false) { ShipChosen = 0; isInitialPositionChosen = false; isShipPlacedOnBoard = false; } while (ShipChosen == 0) { Errors.WriteErrorMessage(); playerVariable.listShips(); string shipChoice = Console.ReadLine().ToLower(); ShipChosen = SelectShip(shipChoice, playerVariable); } Console.WriteLine("Select starting coordinates in the following format: x,y"); int shipLength = playerVariable.realShips[ShipChosen - 1].Length; while (!isInitialPositionChosen) { Errors.WriteErrorMessage(); try { string playerStartCoords = Console.ReadLine().ToLower(); isInitialPositionChosen = PlaceShip(playerStartCoords, playerVariable); } catch (Exception) { Console.WriteLine("You can only enter numbers between 0 and 9, separated by a comma to set the start point of your ship."); Console.WriteLine("Try again."); } } Console.WriteLine("Excellent, now choose whether you want it facing N, E, S, or W."); while (!isShipPlacedOnBoard) { Errors.WriteErrorMessage(); try { string direction = Console.ReadLine().ToLower(); isShipPlacedOnBoard = playerVariable.board.SetShipDirection(xCoord, yCoord, direction, shipLength, playerVariable); } catch (FormatException) { Errors.ErrorMessage = "Please only write 'N', 'E', 'S', or 'W'"; } } if (playerVariable.areShipsEmpty() == true) { Console.Clear(); Board.DisplayBoard(playerVariable.board); Errors.WriteErrorMessage(); InfoMessages.WriteInfoMessage(); if (playerVariable == player1 && !comPlayer) { Console.WriteLine("Look at your board and press any key + Enter to let player 2 enter their ships"); } else { Console.WriteLine("Look at your board and press any key + Enter to start the game!"); } Console.ReadLine(); Console.Clear(); } if (player1.areShipsEmpty() && playerVariable == player1 && !comPlayer) { playerVariable = player2; } else if (player1.areShipsEmpty() && comPlayer) { ComputerPlayer.SetUpComputerShips((ComputerPlayer)player2); Errors.ErrorMessage = ""; } } isGameActive = true; Player activePlayer = player1; Player inactivePlayer = player2; while (isGameActive) { bool wasTurnSuccessful = false; InfoMessages.InfoMessage += "Player " + ((activePlayer == player1) ? "1" : "2") + ", it is your turn. Select coordinate (x,y) to attack."; ClearBoardAndShowMessages(activePlayer); try { wasTurnSuccessful = (PlayerAttack(activePlayer, inactivePlayer, Console.ReadLine())); } catch (Exception) { FormatErrorMessage(); } if (wasTurnSuccessful) { ClearBoardAndShowMessages(activePlayer); } if (DidAnyoneWin(inactivePlayer)) { Console.WriteLine("Congrats Player " + ((activePlayer == player1) ? "1" : "2") + ", you won!!!"); isGameActive = false; Console.ReadLine(); break; } if (wasTurnSuccessful) { PressEnterToContinue(); } if (!comPlayer && wasTurnSuccessful) { activePlayer = (activePlayer == player1) ? player2 : player1; inactivePlayer = (inactivePlayer == player2) ? player1 : player2; } else if (comPlayer && wasTurnSuccessful) { ComputerPlayer.Attack((ComputerPlayer)player2); } } }
private static void ClearBoard(Player player) { Console.Clear(); Board.DisplayBoard(player.enemyBoard); Board.DisplayBoard(player.board); }
public int[] AimAttack(Board mytargetboard) { int[] coordinates = new int[2]; int x = 1; int y = 1; bool KeepGoing = true; for (int i = 1; mytargetboard.layout[x, y] != "[ ]"; i++) { if (y == 21) { x++; y = 1; } else { y++; } } mytargetboard.layout[x, y] = "[+]"; do { mytargetboard.DisplayBoard($"Where do you want to attack?"); ConsoleKey buttonPress = Console.ReadKey().Key; if (buttonPress == ConsoleKey.RightArrow) { bool moved = MoveCursorRight(mytargetboard, x, y); if (moved) { y++; } } else if (buttonPress == ConsoleKey.LeftArrow) { bool moved = MoveCursorLeft(mytargetboard, x, y); if (moved) { y--; } } else if (buttonPress == ConsoleKey.UpArrow) { bool moved = MoveCursorUp(mytargetboard, x, y); if (moved) { x--; } } else if (buttonPress == ConsoleKey.DownArrow) { bool moved = MoveCursorDown(mytargetboard, x, y); if (moved) { x++; } } else if (buttonPress == ConsoleKey.Enter) { coordinates[0] = x; coordinates[1] = y; KeepGoing = false; } }while (KeepGoing); return(coordinates); }