public static void Startup() { bool playAgain = true; while (playAgain) { ConsoleOutput.MainMenu(); Console.Clear(); Player player1 = new Player(); Player player2 = new Player(); Player currentPlayer = new Player(); player1.Name = ConsoleInput.GetPlayerNames("Player 1 - Please enter your name:"); Console.Clear(); player2.Name = ConsoleInput.GetPlayerNames("Player 2 - Please enter your name:"); Console.Clear(); Random random = new Random(); int[] playerList = { 0, 1 }; int playerSelection = random.Next(0, playerList.Length); if (playerSelection == 0) { currentPlayer.Name = player1.Name; currentPlayer.Board = player1.Board; } currentPlayer.Name = player2.Name; currentPlayer.Board = player2.Board; Console.Clear(); Console.WriteLine(currentPlayer.Name + " - You will be first to shoot!"); Console.WriteLine(); Console.WriteLine(player1.Name + " - Get ready to place your board"); Console.WriteLine("Press any key to continue..."); Console.WriteLine(); Console.ReadKey(); Console.Clear(); foreach (ShipType ship in Enum.GetValues(typeof(ShipType))) { bool shipPlaced = false; do { Coordinate coordinate = ConsoleInput.GetShipCoordinate(player1.Name + " - Please Enter Your " + ship + " Coordinates. Example B2"); ShipDirection direction = ConsoleInput.GetShipDirection(player1.Name + " - Please Enter Your " + ship + "'s Direction - U - Up, D - Down, L - Left, R - Right"); PlaceShipRequest placeShipRequest = new PlaceShipRequest() { Coordinate = coordinate, Direction = direction, ShipType = ship }; var response = player1.Board.PlaceShip(placeShipRequest); switch (response) { case BLL.Responses.ShipPlacement.NotEnoughSpace: Console.WriteLine("Not enough space"); break; case BLL.Responses.ShipPlacement.Overlap: Console.WriteLine("Ship Overlaps"); break; default: shipPlaced = true; break; } }while (!shipPlaced); } Console.Clear(); foreach (ShipType ship in Enum.GetValues(typeof(ShipType))) { bool shipPlaced = false; do { Coordinate coordinate = ConsoleInput.GetShipCoordinate(player2.Name + " - Please Enter Your " + ship + " Coordinates. Example B2"); ShipDirection direction = ConsoleInput.GetShipDirection(player2.Name + " - Please Enter Your " + ship + "'s Direction - U - Up, D - Down, L - Left, R - Right"); PlaceShipRequest placeShipRequest = new PlaceShipRequest() { Coordinate = coordinate, Direction = direction, ShipType = ship }; var response = player2.Board.PlaceShip(placeShipRequest); switch (response) { case BLL.Responses.ShipPlacement.NotEnoughSpace: Console.WriteLine("Not Enough Space"); break; case BLL.Responses.ShipPlacement.Overlap: Console.WriteLine("Ship Overlaps"); break; default: shipPlaced = true; break; } }while (!shipPlaced); } Console.Clear(); Console.WriteLine(); Console.WriteLine(currentPlayer.Name + " - Get ready to shoot!"); bool GameOver = false; while (!GameOver) { if (currentPlayer.Name == player1.Name) { Coordinate fireShotCoordinate = ConsoleInput.FireShotCoordinates(player1.Name + " - Please select coordinates to fire your shot:"); FireShotResponse fireShotResponse = new FireShotResponse(); fireShotResponse = player2.Board.FireShot(fireShotCoordinate); switch (fireShotResponse.ShotStatus) { case ShotStatus.Duplicate: break; case ShotStatus.Invalid: break; case ShotStatus.Hit: Console.WriteLine("Nice hit!"); break; case ShotStatus.Miss: Console.WriteLine("You missed!"); break; case ShotStatus.HitAndSunk: Console.WriteLine("Hit and Sink"); break; case ShotStatus.Victory: default: Console.WriteLine("Congrats! You Win!"); GameOver = true; break; } DisplayBoard.DisplayScoreBoard(player2.Board); Console.WriteLine(); } while (!GameOver) { Coordinate fireShotCoordinate2 = ConsoleInput.FireShotCoordinates(player2.Name + " - Please select coordinates to fire your shot:"); FireShotResponse fireShotResponse2 = new FireShotResponse(); fireShotResponse2 = player1.Board.FireShot(fireShotCoordinate2); switch (fireShotResponse2.ShotStatus) { case ShotStatus.Duplicate: break; case ShotStatus.Invalid: break; case ShotStatus.Hit: Console.WriteLine("Nice hit!"); break; case ShotStatus.Miss: Console.WriteLine("You missed!"); break; case ShotStatus.HitAndSunk: Console.WriteLine("Hit and Sink"); break; case ShotStatus.Victory: default: Console.WriteLine("Congrats! You Win!"); GameOver = true; break; } DisplayBoard.DisplayScoreBoard(player1.Board); Console.WriteLine(); } } while (!GameOver) { if (currentPlayer.Name == player2.Name) { Coordinate fireShotCoordinate3 = ConsoleInput.FireShotCoordinates(player2.Name + " - Please select coordinates to fire your shot:"); FireShotResponse fireShotResponse3 = new FireShotResponse(); fireShotResponse3 = player1.Board.FireShot(fireShotCoordinate3); switch (fireShotResponse3.ShotStatus) { case ShotStatus.Duplicate: Console.WriteLine("Duplicate Shot!"); break; case ShotStatus.Invalid: Console.WriteLine("Invalid Shot!"); break; case ShotStatus.Hit: Console.WriteLine("Nice hit!"); break; case ShotStatus.Miss: Console.WriteLine("You missed!"); break; case ShotStatus.HitAndSunk: Console.WriteLine("Hit and Sink"); break; case ShotStatus.Victory: default: Console.WriteLine("Congrats! You Win!"); GameOver = true; break; } DisplayBoard.DisplayScoreBoard(player1.Board); Console.WriteLine(); } while (!GameOver) { Coordinate fireShotCoordinate4 = ConsoleInput.FireShotCoordinates(player1.Name + " - Please select coordinates to fire your shot:"); FireShotResponse fireShotResponse4 = new FireShotResponse(); fireShotResponse4 = player2.Board.FireShot(fireShotCoordinate4); switch (fireShotResponse4.ShotStatus) { case ShotStatus.Duplicate: Console.WriteLine("Duplicate Shot!"); break; case ShotStatus.Invalid: Console.WriteLine("Invalid Shot!"); break; case ShotStatus.Hit: Console.WriteLine("Nice hit!"); break; case ShotStatus.Miss: Console.WriteLine("You missed!"); break; case ShotStatus.HitAndSunk: Console.WriteLine("Hit and Sink"); break; case ShotStatus.Victory: default: Console.WriteLine("Congrats! You Win!"); GameOver = true; break; } DisplayBoard.DisplayScoreBoard(player2.Board); Console.WriteLine(); } } Console.WriteLine("Do you want to play again? Y or N"); string startOver = Console.ReadLine().ToUpper(); if (startOver == "Y" || startOver == "YES") { playAgain = true; } if (startOver == "N" || startOver == "NO") { Console.WriteLine("Thanks for playing!"); playAgain = false; } } }
public void PlayGame() { bool gameOver = false; //welcome message ConsoleOutput.Welcome(); while (gameOver == false) { string[] playerlist = ConsoleInput.GetPlayerNames(); int startingPlayer = DetermineWhoStarts(); string currentPlayer = playerlist[startingPlayer]; //run game setup SetupWorkflow gameBoard = new SetupWorkflow(); Board player1Board; Board player2Board; if (currentPlayer == playerlist[0]) { ConsoleOutput.PlayerStartPrompt(playerlist[0]); player1Board = gameBoard.GameSetUp(); ConsoleOutput.PlayerStartPrompt(playerlist[1]); player2Board = gameBoard.GameSetUp(); } else { ConsoleOutput.PlayerStartPrompt(playerlist[1]); player2Board = gameBoard.GameSetUp(); ConsoleOutput.PlayerStartPrompt(playerlist[0]); player1Board = gameBoard.GameSetUp(); } bool winnerFound = false; while (winnerFound == false) { /*Show a grid with marks from the their board's shot history. Place a yellow M in a coordinate if a shot has been fired and missed at that location or a red H if a shot has been fired that has hit.*/ if (currentPlayer == playerlist[0]) { ShowGrid(player1Board.GetShotHistory()); } else { ShowGrid(player2Board.GetShotHistory()); } //Prompt the user for a coordinate entry (ex: B10). Validate the entry; if valid, create a coordinate object, convert the letter to a number, and call the opponent board's FireShot() method. Coordinate shot = GetShotCoordinates(currentPlayer); FireShotResponse response; if (currentPlayer == playerlist[0]) { response = player2Board.FireShot(shot); } else { response = player1Board.FireShot(shot); } //Retrieve the response from the FireShot method and display an appropriate message to the user. ConsoleOutput.DisplayShotResult(response); if (response.ShotStatus == ShotStatus.Victory) { winnerFound = true; } //if the shot is not invalid and not a duplicate switch players if (response.ShotStatus != ShotStatus.Invalid && response.ShotStatus != ShotStatus.Duplicate) { Console.Clear(); currentPlayer = SwitchPlayer(currentPlayer, playerlist); } } //ask if they would like to play again //reset boards if they do //exit if they dont bool playAgain = ConsoleInput.PlayAgain(currentPlayer); if (playAgain == false) { gameOver = true; } } //Ending message ConsoleOutput.GameOver(); }