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(); }
public static void StartGame(Player p1, Player p2) { Random rng = new Random(); int whoseTurn = rng.Next(1, 3); bool isValid = true; Coordinate xy; bool Victory = false; FireShotResponse response = new FireShotResponse(); if (whoseTurn == 1) { Console.WriteLine("Ok, let's get started!"); Console.ReadLine(); Console.Clear(); Console.WriteLine($"{p1.Name}, you have been randomly chosen to go first."); Console.ReadLine(); Console.Clear(); } else { Console.Clear(); Console.WriteLine("Ok, let's get started!"); Console.ReadLine(); Console.Clear(); Console.WriteLine($"{p2.Name}, you have been randomly chose to go first."); } while (Victory == false) { if (whoseTurn == 1) { VisualBoard.HitBoard(p2); //Getting cords to fire Console.WriteLine($"{p1.Name} enter a coordinate to fire at"); string cord = Console.ReadLine(); xy = ConsoleInput.ConvertToCord(cord); //setting fireshot response response = p2.pBoard.FireShot(xy); if (response.ShotStatus == ShotStatus.Duplicate) { ConsoleOutput.DisplayDuplicate(); } else if (response.ShotStatus == ShotStatus.Hit) { ConsoleOutput.DisplayHit(); } else if (response.ShotStatus == ShotStatus.HitAndSunk) { ConsoleOutput.DisplayHitAndSunk(); } else if (response.ShotStatus == ShotStatus.Invalid) { ConsoleOutput.DisplayInvalid(); } else if (response.ShotStatus == ShotStatus.Miss) { ConsoleOutput.DisplayMiss(); } else { ConsoleOutput.DisplayVictory(); Victory = true; } if (response.ShotStatus == ShotStatus.Duplicate) { whoseTurn = 1; } else if (response.ShotStatus == ShotStatus.Invalid) { whoseTurn = 1; } else { whoseTurn = 2; } } else { VisualBoard.HitBoard(p1); Console.WriteLine($"{p2.Name} enter a coordinate to fire at"); string cord = Console.ReadLine(); xy = ConsoleInput.ConvertToCord(cord); response = p1.pBoard.FireShot(xy); if (response.ShotStatus == ShotStatus.Duplicate) { ConsoleOutput.DisplayDuplicate(); } else if (response.ShotStatus == ShotStatus.Hit) { ConsoleOutput.DisplayHit(); } else if (response.ShotStatus == ShotStatus.HitAndSunk) { ConsoleOutput.DisplayHitAndSunk(); } else if (response.ShotStatus == ShotStatus.Invalid) { ConsoleOutput.DisplayInvalid(); } else if (response.ShotStatus == ShotStatus.Miss) { ConsoleOutput.DisplayMiss(); } else { ConsoleOutput.DisplayVictory(); Victory = true; } if (response.ShotStatus == ShotStatus.Duplicate) { whoseTurn = 2; } else if (response.ShotStatus == ShotStatus.Invalid) { whoseTurn = 2; } else { whoseTurn = 1; } } } }
internal static void GoPlay(GameState state) { Player attackingPlayer = null; Player defendingPlayer = null; Player activePlayer = null; bool isVictory = false; while (!isVictory) { if (state.IsPlayerAsTurn) { attackingPlayer = state.Player1; defendingPlayer = state.Player2; } else { attackingPlayer = state.Player2; defendingPlayer = state.Player1; } if (state.IsPlayerAsTurn) { activePlayer = state.Player1; } else { activePlayer = state.Player2; } var ShotCoord = ConsoleInput.FireCoord(state); ConsoleOutput.DrawBoard(defendingPlayer.PlayerBoard); FireShotResponse response = defendingPlayer.PlayerBoard.FireShot(ShotCoord); switch (response.ShotStatus) { case ShotStatus.Hit: ConsoleOutput.ShipHit(state); state.IsPlayerAsTurn = !state.IsPlayerAsTurn; break; case ShotStatus.Miss: ConsoleOutput.ShotMiss(state); state.IsPlayerAsTurn = !state.IsPlayerAsTurn; break; case ShotStatus.Duplicate: ConsoleOutput.ShotDuplicate(state); state.IsPlayerAsTurn = true; break; case ShotStatus.Invalid: ConsoleOutput.InvalidShot(state); state.IsPlayerAsTurn = true; break; case ShotStatus.HitAndSunk: ConsoleOutput.HitandSunk(state); state.IsPlayerAsTurn = !state.IsPlayerAsTurn; break; case ShotStatus.Victory: ConsoleOutput.Victory(state); isVictory = true; break; } } }
public static void Start(GameState state) { Player attackingPlayer = null; Player defendingPlayer = null; bool isGameOver = false; while (!isGameOver) { if (state.IsPlayerOneTurn) { attackingPlayer = state.P1; defendingPlayer = state.P2; } else { attackingPlayer = state.P2; defendingPlayer = state.P1; } ConsoleOutput.DrawBoard(defendingPlayer.Board); Coordinate attack = ConsoleInput.GetCoord(attackingPlayer.Name); FireShotResponse ShotFireStatus = defendingPlayer.Board.FireShot(attack); switch (ShotFireStatus.ShotStatus) { case ShotStatus.Hit: Console.WriteLine("Hit confirmed! Next players turn."); Console.ReadLine(); Console.Clear(); state.IsPlayerOneTurn = !state.IsPlayerOneTurn; break; case ShotStatus.Miss: Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Welp! You tried but that shot was a miss. Next players turn."); Console.ReadLine(); Console.Clear(); state.IsPlayerOneTurn = !state.IsPlayerOneTurn; break; case ShotStatus.Invalid: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("...can't shoot there, coordinates must be inside of the grid, try again."); Console.ReadLine(); break; case ShotStatus.Duplicate: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Whoops! Didn't look at your board? Already shot there, try again."); Console.ReadLine(); break; case ShotStatus.HitAndSunk: Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Yahtzee! You sunk a ship."); Console.ReadLine(); Console.Clear(); state.IsPlayerOneTurn = !state.IsPlayerOneTurn; break; case ShotStatus.Victory: isGameOver = true; ConsoleOutput.GameOver(); Console.Clear(); break; } } ConsoleOutput.GameOver(); }
public void Start() { Player playerOne = new Player(); Player playerTwo = new Player(); //shows start menu & displays header ConsoleOutput.DisplayTitle(); //get players names playerOne.Name = ConsoleInput.GetPlayerName(1); playerTwo.Name = ConsoleInput.GetPlayerName(2); Console.WriteLine("Press enter to start..."); Console.ReadKey(); //the player's boards: their boards with ships & their guess board playerOne.PlayerBoard = ConsoleInput.PlayerBoard(); playerOne.GuessBoard = ConsoleInput.PlayerBoard(); playerTwo.PlayerBoard = ConsoleInput.PlayerBoard(); playerTwo.GuessBoard = ConsoleInput.PlayerBoard(); //for loop to place their 5 ships for player 1 for (int i = 0; i < 5; i++) { GameManager.PlaceShips(playerOne, i); } Console.Clear(); Console.WriteLine($"{playerTwo.Name}, it's your turn to fill your board. Press enter if you are ready..."); Console.ReadKey(); //for loop to place their 5 ships for player 2 for (int i = 0; i < 5; i++) { GameManager.PlaceShips(playerTwo, i); } //randomly chooses who goes first & playing the game //randomly find's out who's going first Console.Clear(); int StartingPlayer = GameManager.WhoGoesFirst(); if (StartingPlayer == 1) { playerOne.WhoseTurn = true; playerTwo.WhoseTurn = false; } else { playerOne.WhoseTurn = false; playerTwo.WhoseTurn = true; } //bool checkVictory; //playing the game bool isVictory = false; do { if (playerOne.WhoseTurn == true) { Console.WriteLine($"\n\n\n {playerOne.Name}, it is your turn."); Console.ReadKey(); isVictory = GameManager.Game(playerTwo.PlayerBoard, playerOne, playerTwo); GameManager.WhoseTurn(playerOne, playerTwo); } else { Console.WriteLine($"\n\n\n {playerTwo.Name}, it is your turn."); Console.ReadKey(); isVictory = GameManager.Game(playerOne.PlayerBoard, playerTwo, playerOne); GameManager.WhoseTurn(playerOne, playerTwo); } } while (isVictory == false); //do //{ // if (playerOne.WhoseTurn) // { // ConsoleOutput.ShowBoard(playerOne.GuessBoard.DisplayBoard); // Console.WriteLine($"{playerOne.Name}, enter a coordinate to fire a shot at enemy ships: (Ex. A2) "); // Coordinate shot = ConsoleInput.GetCoordinate(); // FireShotResponse fireShotResponse = playerTwo.PlayerBoard.FireShot(shot); // GameManager.HitOrMiss(shot, fireShotResponse, playerOne); // if (fireShotResponse.ShotStatus == ShotStatus.Invalid || fireShotResponse.ShotStatus == ShotStatus.Duplicate) // { // Console.WriteLine($"{fireShotResponse.ShotStatus} entry! Press enter to try again."); // Console.ReadKey(); // playerOne.WhoseTurn = false; // } // else // { // Console.WriteLine($"{fireShotResponse.ShotStatus} {fireShotResponse.ShipImpacted}! Press enter to end your turn."); // Console.ReadKey(); // playerOne.WhoseTurn = false; // } // //GameManager.Game(playerTwo.PlayerBoard, playerOne); // checkVictory = GameManager.Win(fireShotResponse); // Console.Clear(); // } // else // { // ConsoleOutput.ShowBoard(playerTwo.GuessBoard.DisplayBoard); // Console.WriteLine($"{playerTwo.Name}, enter a coordinate to fire a shot at enemy ships: (Ex. A2) "); // Coordinate shot = ConsoleInput.GetCoordinate(); // FireShotResponse fireShotResponse = playerOne.PlayerBoard.FireShot(shot); // GameManager.HitOrMiss(shot, fireShotResponse, playerTwo); // if (fireShotResponse.ShotStatus == ShotStatus.Invalid || fireShotResponse.ShotStatus == ShotStatus.Duplicate) // { // Console.WriteLine($"{fireShotResponse.ShotStatus} entry! Press enter to try again."); // Console.ReadKey(); // playerTwo.WhoseTurn = false; // } // else // { // Console.WriteLine($"{fireShotResponse.ShotStatus} {fireShotResponse.ShipImpacted}! Press enter to end your turn."); // Console.ReadKey(); // playerTwo.WhoseTurn = false; // } // checkVictory = GameManager.Win(fireShotResponse); // Console.Clear(); // } // GameManager.WhoseTurn(playerOne, playerTwo); //} while (checkVictory == false); //keep game going until someone wins //to end the game or restart a new one if (isVictory == true && playerOne.WhoseTurn == false) { ConsoleOutput.WonTitle(); Console.WriteLine($" {playerOne.Name} wins! \n\nWould you like to play again?\nPress Y then enter for Yes or any key to quit."); string playOrQuit = Console.ReadLine().ToUpper(); if (playOrQuit == "Y" || playOrQuit == "YES") { Console.Clear(); Start(); } } else if (isVictory == true && playerTwo.WhoseTurn == false) { ConsoleOutput.WonTitle(); Console.WriteLine($" {playerTwo.Name} wins! \n\nWould you like to play again?\nPress Y then enter for Yes or any key to quit."); string playOrQuit = Console.ReadLine().ToUpper(); if (playOrQuit == "Y" || playOrQuit == "YES") { Console.Clear(); Start(); } } }
public void Play() { bool stillPlaying = true; Console.WriteLine(); Console.WriteLine("Hello! Player 1 setup your board. Player 2 will follow. First turn will be randomly chosen."); Console.WriteLine("Press anything to begin!"); Console.ReadLine(); Player P1 = Setup.CreatePlayer(1); //one more step to set up board, put that at the top of loop new loop, //Console.Clear(); Player P2 = Setup.CreatePlayer(2); Console.Clear(); //start here and while (stillPlaying) { P1.PlayerBoard = Setup.SetUpBoard(P1.Name); Console.Clear(); P2.PlayerBoard = Setup.SetUpBoard(P2.Name); Console.Clear(); //need to start new game from here string firstTurn = Setup.DecideFirstTurn(P1.Name, P2.Name); //return bool, set it to turn bool isP1Turn = true; bool gameRunning = true; string winner = ""; string loser = ""; if (firstTurn == P1.Name) { isP1Turn = true; } else if (firstTurn == P2.Name) { isP1Turn = false; } //gameplay while (gameRunning) { if (isP1Turn) //could definetly make this one time //if decides who is attacking who { ConsoleOutput.DrawBoard(P2.PlayerBoard); Console.WriteLine($"{P1.Name} FIRE AWAY!"); Coordinate c = ConsoleInput.EnterCoords(); FireShotResponse r = P2.PlayerBoard.FireShot(c); Console.Clear(); while (r.ShotStatus == ShotStatus.Duplicate || r.ShotStatus == ShotStatus.Invalid) //added invalid { ConsoleOutput.DrawBoard(P2.PlayerBoard); Console.WriteLine("Bad shot! Quit wasting ammo. Try again!"); Console.WriteLine($"{P1.Name} FIRE AWAY!"); c = ConsoleInput.EnterCoords(); r = P2.PlayerBoard.FireShot(c); Console.Clear(); } Console.Clear(); //draw shot ConsoleOutput.DrawBoard(P2.PlayerBoard); ConsoleOutput.ShotMessage(r, P1.Name, P2.Name); if (r.ShotStatus == ShotStatus.Victory) { winner = P1.Name; loser = P2.Name; gameRunning = false; } isP1Turn = false; Console.Clear(); } else if (!isP1Turn) { Console.Clear(); ConsoleOutput.DrawBoard(P1.PlayerBoard); Console.WriteLine($"{P2.Name} FIRE AWAY!"); Coordinate c = ConsoleInput.EnterCoords(); FireShotResponse r = P1.PlayerBoard.FireShot(c); Console.Clear(); //invalid shot while (r.ShotStatus == ShotStatus.Duplicate || r.ShotStatus == ShotStatus.Invalid) //added { ConsoleOutput.DrawBoard(P1.PlayerBoard); Console.WriteLine("Bad shot! Quit wasting ammo. Try again!"); Console.WriteLine($"{P2.Name} FIRE AWAY!"); c = ConsoleInput.EnterCoords(); r = P1.PlayerBoard.FireShot(c); Console.Clear(); } Console.Clear(); //draw shot ConsoleOutput.DrawBoard(P1.PlayerBoard); ConsoleOutput.ShotMessage(r, P1.Name, P2.Name); if (r.ShotStatus == ShotStatus.Victory) { winner = P2.Name; loser = P1.Name; gameRunning = false; } isP1Turn = true; Console.Clear(); } } Console.Clear(); Console.WriteLine($"The game is over! {winner} is the winner! {loser} loses!"); stillPlaying = ConsoleInput.PlayAgain(); } }
public static ConsoleKeyInfo ContinueOrQuit() { Console.Clear(); ConsoleOutput.ContinueOrQuitMessage(); return(Console.ReadKey()); }
public static void PlayerTurns(Player P1, Player P2, int firstTurn) { bool player1Victory = false; bool player2Victory = false; bool victory = false; int whoseTurn = firstTurn; while (!victory) { if (whoseTurn == 1) { //player 1 turn //Display p2 board //Fire shot bool validShot = false; while (!validShot) { ConsoleOutput.DisplayGameBoard(P2.PlayerGameBoard); ConsoleOutput.WaveBorder(1); ConsoleOutput.Player1Turn(P1.Name); Coordinate c = ConsoleInput.GetPlayerCoordinate(); FireShotResponse response = P2.PlayerGameBoard.FireShot(c); Console.Clear(); ConsoleOutput.DisplayGameBoard(P2.PlayerGameBoard); ConsoleOutput.WaveBorder(1); if (response.ShotStatus != ShotStatus.Duplicate && response.ShotStatus != ShotStatus.Invalid) { validShot = true; whoseTurn = 2; } if (response.ShotStatus == ShotStatus.Victory) { validShot = true; victory = true; player1Victory = true; } ConsoleOutput.ShotMessage(response, P1.Name, P2.Name, P2, c); } } else { //player 2 turn //Display p1 board //Fire Shot bool validShot = false; while (!validShot) { ConsoleOutput.DisplayGameBoard(P1.PlayerGameBoard); ConsoleOutput.WaveBorder(2); ConsoleOutput.Player2Turn(P2.Name); Coordinate c = ConsoleInput.GetPlayerCoordinate(); FireShotResponse response = P1.PlayerGameBoard.FireShot(c); Console.Clear(); ConsoleOutput.DisplayGameBoard(P1.PlayerGameBoard); ConsoleOutput.WaveBorder(2); if (response.ShotStatus != ShotStatus.Duplicate && response.ShotStatus != ShotStatus.Invalid) { validShot = true; whoseTurn = 1; } if (response.ShotStatus == ShotStatus.Victory) { validShot = true; victory = true; player2Victory = true; } ConsoleOutput.ShotMessage(response, P2.Name, P1.Name, P1, c); } } } bool playAgain = false; playAgain = ConsoleOutput.EndGameMessage(player1Victory, player2Victory, P1.Name, P2.Name); if (playAgain) { //Create new game boards and go through the turns again Console.Clear(); Board blank = new Board(); ConsoleOutput.DisplayGameBoard(blank); ConsoleOutput.ShipKey(); P1.PlayerGameBoard = ConsoleInput.PlaceShipLoop(P1.Name); Console.Clear(); ConsoleOutput.DisplayGameBoard(blank); ConsoleOutput.ShipKey(); P2.PlayerGameBoard = ConsoleInput.PlaceShipLoop(P2.Name); Console.Clear(); GameWorkflow turn = new GameWorkflow(); int ft = turn.WhoIsGoingFirst(); PlayerTurns(P1, P2, ft); } ConsoleOutput.Credits(); }
//int _sunkCount; //ShotHistory p1currentShot; //ShotHistory p2currentShot; public void RunProg() { CreateGameManagerInstance(); ConsoleOutput.DisplayTitle(); Console.Clear(); ConsoleKeyInfo keyPressed; //string player1Name; //string player2Name; FireShotResponse fireShotResponseP1 = new FireShotResponse(); FireShotResponse fireShotResponseP2 = new FireShotResponse(); ConsoleInput input = new ConsoleInput(); ConsoleOutput output = new ConsoleOutput(); //ConsoleOutput.ContinueOrQuitMessage; CreateRandomTurn(); Coordinate coordinate; SetupWorkflow setup = new SetupWorkflow(); do { //SetupWorkflow.SetupPlayers(); //setup.SetupBoard(); Console.Clear(); _manager.player1.Name = input.GetPlayerName(); _manager.player2.Name = input.GetPlayerName(); Console.Clear(); ConsoleOutput.PlayerTurnMessage(_manager.player1.Name); _manager.player1.Board = setup.SetupBoard(_manager.player1.Name); ConsoleOutput.PlayerTurnMessage(_manager.player2.Name); _manager.player2.Board = setup.SetupBoard(_manager.player2.Name); _manager.player1.turnNumber = 0; _manager.player2.turnNumber = 1; //CreateRandomTurn(); bool victory = false; do { if (_manager.player1.turnNumber == _turn) { Console.Clear(); ConsoleOutput.PlayerTurnMessage(_manager.player1.Name); ConsoleOutput.CurrentPlayerTurn(_manager.player1.Name); Console.WriteLine(); output.DrawBoard(_manager.player2.Board); Console.WriteLine(); Console.WriteLine("Lets try to hit an opponent's ship."); Console.WriteLine(); do { coordinate = input.GetCoordinates(); fireShotResponseP1 = _manager.player2.Board.FireShot(coordinate); if (fireShotResponseP1.ShotStatus == ShotStatus.Duplicate) { output.DuplicateMessage(); } } while (fireShotResponseP1.ShotStatus == ShotStatus.Duplicate); if (fireShotResponseP1.ShotStatus == ShotStatus.HitAndSunk) { output.HitAndSunkMessage(fireShotResponseP1.ShipImpacted); Console.ReadKey(); } else { output.DisplayShotStatus(fireShotResponseP1.ShotStatus); } if (fireShotResponseP1.ShotStatus == ShotStatus.Victory) { victory = true; string currentPlayerName = _manager.player1.Name; ConsoleOutput.VictoryMessage(currentPlayerName); Console.WriteLine("Press any key to continue."); Console.ReadKey(); break; } //else //{ // ConsoleOutput.SwitchPlayerPrompt(); //} //Console.ReadKey(); _turn = 1; } else { Console.Clear(); ConsoleOutput.PlayerTurnMessage(_manager.player2.Name); ConsoleOutput.CurrentPlayerTurn(_manager.player2.Name); Console.WriteLine(); output.DrawBoard(_manager.player1.Board); Console.WriteLine(); Console.WriteLine("Lets try to hit an opponent's ship."); Console.WriteLine(); do { coordinate = input.GetCoordinates(); fireShotResponseP2 = _manager.player1.Board.FireShot(coordinate); if (fireShotResponseP2.ShotStatus == ShotStatus.Duplicate) { output.DuplicateMessage(); } } while (fireShotResponseP2.ShotStatus == ShotStatus.Duplicate); if (fireShotResponseP2.ShotStatus == ShotStatus.HitAndSunk) { output.HitAndSunkMessage(fireShotResponseP2.ShipImpacted); Console.ReadKey(); } else { output.DisplayShotStatus(fireShotResponseP2.ShotStatus); } if (fireShotResponseP2.ShotStatus == ShotStatus.Victory) { victory = true; string currentPlayerName = _manager.player2.Name; ConsoleOutput.VictoryMessage(currentPlayerName); Console.WriteLine("Press any key to continue."); Console.ReadKey(); } //else //{ // ConsoleOutput.SwitchPlayerPrompt(); //} _turn = 0; } } while (/*!(fireShotResponseP1.ShotStatus == ShotStatus.Victory) || (fireShotResponseP2.ShotStatus == ShotStatus.Victory)*/ !victory); keyPressed = ConsoleInput.ContinueOrQuit(); } while (!(keyPressed.Key == ConsoleKey.Q)); }