static void Main(string[] args) { bool isGameOver = false; while (!isGameOver) { GameflowSetup infoAndBoardSetup = new GameflowSetup(); Gameflow game = new Gameflow(); GameState state = infoAndBoardSetup.SetupPlayersBoard(); game.PlayGame(state); isGameOver = ConsoleInput.PlayAgain(); } }
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 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(); } }