Пример #1
0
        public void RunProg()
        {
            ConsoleOutput.DisplayTitle();
            Console.Clear();
            ConsoleKeyInfo keyPressed;


            do
            {
                mySetup = new SetupWorkflow();
                //myManager = new GameManager();
                SetupWorkflow.SetupPlayers();
                SetupWorkflow.SetupBoard();
                int firstTurn = myManager.RandomTurn();
                //if(firstTurn==0)

                keyPressed = ConsoleInput.ContinueOrQuit();

                Console.Clear();
            } while (!(keyPressed.Key == ConsoleKey.Q));
        }
Пример #2
0
        public static void PlayGame()
        {
            Player p1 = new Player();
            Player p2 = new Player();
            string player1;
            string player2;


            ConsoleOutput.DisplayTitle();
            player1 = ConsoleInput.GetPlayerOneName();
            p1.SetName(player1);
            player2 = ConsoleInput.GetPlayerTwoName();
            p2.SetName(player2);

            Console.Clear();
            p1.PlaceShipOnBoard();
            Console.Clear();
            p2.PlaceShipOnBoard();

            int whoseTurn = _rng.Next(1, 3);

            do
            {
                if (whoseTurn == 1)
                {
                    Console.Clear();
                    p2.DisplayBoard(p1);
                    p2.FireShotAtBoard(p1);
                    whoseTurn = 2;
                }
                else
                {
                    Console.Clear();
                    p1.DisplayBoard(p2);
                    p1.FireShotAtBoard(p2);
                    whoseTurn = 1;
                }
                Console.ReadLine();
            } while (!p1.GetWonGame() && !p2.GetWonGame());
        }
Пример #3
0
        public void RunGame()
        {
            Setup gameSetup = new Setup();

            Player PlayerOne = gameSetup.Player1;
            Player PlayerTwo = gameSetup.Player2;

            Board playerOneBoard = PlayerOne.PlayerBoard;
            Board playerTwoBoard = PlayerTwo.PlayerBoard;

            bool determineFirst = gameSetup.Player1Turn;
            bool IsVictory      = false;

            while (!IsVictory)
            {
                if (determineFirst)
                {
                    ConsoleOutput.GetBoard(playerTwoBoard); //playerone board parameter
                    var fireShotResponse = playerTwoBoard.FireShot(ConsoleInput.GetCoordinate(PlayerOne.PlayerName));
                    Console.WriteLine("This is the shot status : {0}", fireShotResponse.ShotStatus);
                    Console.WriteLine("This is the ship impacted status : {0}", fireShotResponse.ShipImpacted);


                    IsVictory = fireShotResponse.ShotStatus == ShotStatus.Victory;
                }
                else
                {
                    ConsoleOutput.GetBoard(playerOneBoard);
                    var fireShotResponse = playerOneBoard.FireShot(ConsoleInput.GetCoordinate(PlayerTwo.PlayerName));
                    Console.WriteLine("This is the shot status : {0}", fireShotResponse.ShotStatus);
                    Console.WriteLine("This is the ship impacted status : {0}", fireShotResponse.ShipImpacted);
                    IsVictory = fireShotResponse.ShotStatus == ShotStatus.Victory;
                }
                determineFirst = !determineFirst;
            }


            // GameState thisGame = new GameState(PlayerOne, PlayerTwo, determineFirst);
        }
Пример #4
0
        public static void SetUpBoard(User player)
        {
            ShipPlacement response;

            var ships = new Dictionary <ShipType, int>()
            {
                { ShipType.Destroyer, 2 },
                { ShipType.Cruiser, 3 },
                { ShipType.Submarine, 3 },
                { ShipType.Battleship, 4 },
                { ShipType.Carrier, 5 }
            };

            foreach (KeyValuePair <ShipType, int> vessel in ships)
            {
                do
                {
                    Console.Clear();
                    ConsoleOutput.DisplayBoard(player, false);
                    ConsoleOutput.DisplayPlacementMessage(player, vessel.Key, vessel.Value);
                    var request = new PlaceShipRequest()
                    {
                        Coordinate = ConsoleInput.GetCoordinate(),
                        Direction  = ConsoleInput.GetShipDirection(),
                        ShipType   = vessel.Key
                    };

                    response = player.Board.PlaceShip(request);
                    Console.Clear();
                    ConsoleOutput.DisplayBoard(player, false);
                    ConsoleOutput.DisplayShipResponse(request, response);
                    ConsoleInput.Continue();
                } while (response != ShipPlacement.Ok);
            }
            Console.Clear();
            ConsoleOutput.DisplayBoard(player, false);
            ClearScreenPrompt();
        }
Пример #5
0
        public GameState Start()
        {
            ConsoleOutput.DisplayTitle();


            //Get player names
            string p1 = ConsoleInput.GetUserName(1);
            string p2 = ConsoleInput.GetUserName(2);

            Board Player1Board = BuildBoard(p1);
            Board Player2Board = BuildBoard(p2);

            Player Player1 = new Player(p1, Player1Board);
            Player Player2 = new Player(p2, Player2Board);


            //decide who goes first

            bool      IsPlayerAsTurn = DecideWhoGoesFirst();
            GameState state          = new GameState(Player1, Player2, IsPlayerAsTurn);

            return(state);
        }
Пример #6
0
        public static bool Game(Board enemyPlayerBoard, Player player, Player otherPlayer)
        {
            bool checkVictory;

            ConsoleOutput.ShowBoard(player.GuessBoard.DisplayBoard);
            Console.WriteLine($"{player.Name}, enter a coordinate to fire a shot at enemy ships: (Ex. A2) ");
            Coordinate       shot             = ConsoleInput.GetCoordinate();
            FireShotResponse fireShotResponse = enemyPlayerBoard.FireShot(shot);

            HitOrMiss(shot, fireShotResponse, player);
            if (fireShotResponse.ShotStatus == ShotStatus.Invalid || fireShotResponse.ShotStatus == ShotStatus.Duplicate)
            {
                Console.WriteLine($"{fireShotResponse.ShotStatus} entry! Press enter to clear the screen and try again.");
                Console.ReadKey();
                player.WhoseTurn      = false;
                otherPlayer.WhoseTurn = true;
            }
            else if (fireShotResponse.ShotStatus == ShotStatus.HitAndSunk)
            {
                Console.WriteLine($"{fireShotResponse.ShotStatus} {fireShotResponse.ShipImpacted}! Press enter to end your turn.");
                Console.ReadKey();
            }
            else if (fireShotResponse.ShotStatus == ShotStatus.Victory)
            {
                Console.WriteLine($"{fireShotResponse.ShotStatus}! {player.Name} sunk all enemy ships!  Press enter to end the game.");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine($"{fireShotResponse.ShotStatus}! Press enter to end your turn.");
                Console.ReadKey();
            }
            checkVictory = Win(fireShotResponse);
            Console.Clear();
            return(checkVictory);
        }
Пример #7
0
        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();
        }
Пример #8
0
        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();
        }
Пример #9
0
        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;
                }
            }
        }
Пример #10
0
        public void StartFire()
        {
            int x = 0;
            int y = 0;

            while (true)
            {
                string strCoordinate = "";

                if (_start == 1)
                {
                    Coordinate Coordinate = new Coordinate(x, y);

                    Console.Clear();

                    DisplayBoard(_p1board);

                    Console.WriteLine("\n {0},", _p1);


                    //Asks player 1 where to fire

                    Console.WriteLine("Where would you like to fire?");

                    strCoordinate = ConsoleInput.GetCoor(Coordinate);

                    ConsoleInput.ProcessCoor(Coordinate, x, y, strCoordinate);
                    var shotResponse = _p1board.FireShot(Coordinate);

                    ResponseToShot(shotResponse);

                    if (shotResponse.ShotStatus == ShotStatus.Victory)
                    {
                        Console.WriteLine("{0}, you won!", _p1);
                        _start = -1;
                    }
                    else
                    {
                        Console.WriteLine("Press any key for {0}'s turn", _p2);

                        Console.ReadKey();
                    }
                    _start++;
                }

                else if (_start == 2)
                {
                    Coordinate Coordinate = new Coordinate(x, y);

                    Console.Clear();

                    DisplayBoard(_p2board);

                    Console.WriteLine("\n {0},", _p2);


                    //Asks player 2 where to fire

                    Console.WriteLine("Where would you like to fire?");

                    strCoordinate = ConsoleInput.GetCoor(Coordinate);

                    ConsoleInput.ProcessCoor(Coordinate, x, y, strCoordinate);

                    var shotResponse = _p2board.FireShot(Coordinate);

                    ResponseToShot(shotResponse);

                    if (shotResponse.ShotStatus == ShotStatus.Victory)
                    {
                        Console.WriteLine("{0}, you won!", _p2);
                        _start = 1;
                    }
                    else
                    {
                        Console.WriteLine("Press any key for {0}'s turn", _p1);

                        Console.ReadKey();
                    }

                    _start--;
                }
                else
                {
                    break;
                }
            }
            Console.WriteLine("Do you want to play again (Y/N)?");

            string again = Console.ReadLine();

            if (again == "Y" || again == "y")
            {
                Start();
            }
        }
Пример #11
0
        internal static void GamePlay(GameState state)
        {
            // Decide who is going to shoot first
            // Draw the board of the defender player

            Player attackinPlayer  = null;
            Player defendingPlayer = null;



            ShotStatus status = new ShotStatus();


            // Decide who goes first and alternate each shot.



            //ConsoleOut.DrawBoard(defendingPlayer.PlayerBoard);

            // var statuss = ShotStatus.Miss;
            //var gameStatus = ShotStatus.Victory;



            bool isVictory = false;

            while (!isVictory)
            {
                if (state.isPlayerOneTurn)
                {
                    attackinPlayer  = state.P1;
                    defendingPlayer = state.P2;
                }

                else
                {
                    defendingPlayer = state.P2;
                    attackinPlayer  = state.P1;
                }

                ConsoleOut.DrawBoard(defendingPlayer.PlayerBoard);

                var coord = ConsoleInput.GetCoordinate();
                //ConsoleOut.DrawBoard(attackinPlayer.PlayerBoard);

                //FireShotResponse response = new FireShotResponse();



                FireShotResponse response = defendingPlayer.PlayerBoard.FireShot(coord);



                switch (response.ShotStatus)
                {
                case ShotStatus.Hit:
                    Console.WriteLine("It is a hit!");
                    state.isPlayerOneTurn = !state.isPlayerOneTurn;
                    break;

                case ShotStatus.Duplicate:
                    Console.WriteLine("It is a duplicate!");
                    break;

                case ShotStatus.Invalid:
                    Console.WriteLine("It is invalid");
                    break;

                case ShotStatus.Miss:
                    Console.WriteLine("It is a miss");
                    state.isPlayerOneTurn = !state.isPlayerOneTurn;
                    break;

                case ShotStatus.Victory:
                    Console.WriteLine("Congratulation you win the game");
                    state.isPlayerOneTurn = !state.isPlayerOneTurn;
                    break;

                case ShotStatus.HitAndSunk:
                    Console.WriteLine("Congratulation you win the game");
                    break;
                }



                /* if (response.ShotStatus == ShotStatus.Victory)
                 * {
                 *   isVictory = true;
                 * }
                 * else
                 * {
                 *   isVictory = false;
                 *   Player temp = defendingPlayer;
                 *   defendingPlayer = attackinPlayer;
                 *   attackinPlayer = temp;
                 *
                 * }*/
            }
        }
Пример #12
0
        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();
            }
        }
Пример #13
0
        public static void Flow()
        {
            Output.WelcomeMessage();
            P1_Name = Output.P1_name();
            P2_Name = Output.P2_name();

            //p1 Build Board

            Make.Blank(p1_Board);
            Output.PlayerTurn(P1_Name);
            Console.WriteLine("");
            GameFlow.PlaceShipTest2(p1_Board);
            Make.Drawboard(p1_Board);
            Console.ReadKey();
            Console.Clear();

            //p2 Build Board
            Make.Blank(p2_Board);
            Output.PlayerTurn(P2_Name);
            Console.WriteLine("");
            GameFlow.PlaceShipTest2(p2_Board);
            Make.Drawboard(p2_Board);
            Console.ReadKey();
            Console.Clear();

            Random _Rnd = new Random();
            int    turn = _Rnd.Next(1, 2);

            ShotStatus Victorious = new ShotStatus();

            Console.WriteLine("Let the Games begin!");

            do
            {
                if (turn % 2 == 0)
                {
                    Console.Clear();
                    bool repeat = false;
                    Console.WriteLine($"It's {P2_Name}'s turn");
                    //player 2 goes first
                    Console.WriteLine($"{P2_Name}'s Board");
                    Make.Drawboard(p2_Board);
                    Console.WriteLine($"{P1_Name}'s Board");
                    Make.DrawEnemyBoard(p1_Board);

                    do
                    {
                        Console.WriteLine("Pick Somewhere to Fire");


                        Coordinate       FireCord            = (ConsoleInput.InputtoCoordinate());
                        FireShotResponse p2_ResponseOfChoice = p1_Board.FireShot(FireCord);

                        if (p2_ResponseOfChoice.ShotStatus == ShotStatus.Duplicate)
                        {
                            Console.WriteLine("You already tried that silly!, try again!");
                            repeat = true;
                        }
                        else if (p2_ResponseOfChoice.ShotStatus == ShotStatus.Hit)
                        {
                            Console.WriteLine("It's a hit!");
                            Make.DrawEnemyBoardFire(p1_Board, p2_ResponseOfChoice, FireCord);
                            repeat = false;
                        }
                        else if (p2_ResponseOfChoice.ShotStatus == ShotStatus.HitAndSunk)
                        {
                            Console.WriteLine($"Massive hit!, You sunk the {p2_ResponseOfChoice.ShipImpacted}");
                            Make.DrawEnemyBoardFire(p1_Board, p2_ResponseOfChoice, FireCord);
                            repeat = false;
                        }
                        else if (p2_ResponseOfChoice.ShotStatus == ShotStatus.Invalid)
                        {
                            Console.WriteLine("Invalid input, try again");
                            repeat = true;
                        }
                        else if (p2_ResponseOfChoice.ShotStatus == ShotStatus.Miss)
                        {
                            Console.WriteLine("Oops, looks like you missed");
                            Make.DrawEnemyBoardFire(p1_Board, p2_ResponseOfChoice, FireCord);
                            repeat = false;
                        }
                        else if (p2_ResponseOfChoice.ShotStatus == ShotStatus.Victory)
                        {
                            Console.WriteLine($"{P2_Name} Wins!");
                            Victorious = ShotStatus.Victory;
                            repeat     = false;
                        }
                    } while (repeat == true);

                    Console.ReadKey();
                    turn++;
                }
                else
                {
                    Console.Clear();
                    bool repeat = false;
                    //player 1 goes first
                    Console.WriteLine($"{P1_Name}'s turn");
                    Console.WriteLine($"{P1_Name}'s Board");
                    Make.Drawboard(p1_Board);
                    Console.WriteLine($"{P2_Name}'s Board");
                    Make.DrawEnemyBoard(p2_Board);

                    //player 1 goes first
                    do
                    {
                        Console.WriteLine("Pick Somewhere to Fire");



                        Coordinate       FireCord            = (ConsoleInput.InputtoCoordinate());
                        FireShotResponse p1_ResponseOfChoice = p2_Board.FireShot(FireCord);

                        if (p1_ResponseOfChoice.ShotStatus == ShotStatus.Duplicate)
                        {
                            Console.WriteLine("You already tried that silly!, try again!");
                            repeat = true;
                        }
                        else if (p1_ResponseOfChoice.ShotStatus == ShotStatus.Hit)
                        {
                            Console.WriteLine("It's a hit!");
                            Make.DrawEnemyBoardFire(p2_Board, p1_ResponseOfChoice, FireCord);
                            repeat = false;
                        }
                        else if (p1_ResponseOfChoice.ShotStatus == ShotStatus.HitAndSunk)
                        {
                            Console.WriteLine($"Massive hit!, You sunk the {p1_ResponseOfChoice.ShipImpacted}");
                            Make.DrawEnemyBoardFire(p2_Board, p1_ResponseOfChoice, FireCord);
                            repeat = false;
                        }
                        else if (p1_ResponseOfChoice.ShotStatus == ShotStatus.Invalid)
                        {
                            Console.WriteLine("Invalid input, try again");
                            repeat = true;
                        }
                        else if (p1_ResponseOfChoice.ShotStatus == ShotStatus.Miss)
                        {
                            Console.WriteLine("Oops, looks like you missed");
                            Make.DrawEnemyBoardFire(p2_Board, p1_ResponseOfChoice, FireCord);
                            repeat = false;
                        }
                        else if (p1_ResponseOfChoice.ShotStatus == ShotStatus.Victory)
                        {
                            Console.WriteLine($"{P1_Name} Wins!");
                            Victorious = ShotStatus.Victory;
                            repeat     = false;
                        }
                    }while (repeat == true);
                    Console.ReadKey();
                    turn++;
                }
            } while (Victorious != ShotStatus.Victory);

            Output.replay();


            Console.ReadKey();
            Console.Clear();
        }
Пример #14
0
        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();
        }
        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;
                    }
                }
            }
        }
Пример #16
0
        public static ShipType UserTypeShip(Board x, int Count)
        {
            ShipType ship = new ShipType();

            Console.WriteLine("Enter a Ship type");
            string typeofship = ConsoleInput.ReadLine();


            if (typeofship == "Destroyer" || typeofship == "destroyer")
            {
                ship = ShipType.Destroyer;
            }
            else if (typeofship == "Submarine" || typeofship == "submarine")
            {
                ship = ShipType.Submarine;
            }
            else if (typeofship == "Cruiser" || typeofship == "cruiser")
            {
                ship = ShipType.Cruiser;
            }
            else if (typeofship == "Battleship" || typeofship == "battleship")
            {
                ship = ShipType.Battleship;
            }
            else if (typeofship == "Carrier" || typeofship == "carrier")
            {
                ship = ShipType.Carrier;
            }
            else
            {
                Console.WriteLine("Invalid Ship type, try again");
                UserTypeShip(x, Count);
            }


            for (int i = Count; i < 5; i++)
            {
                if (Count == 1)
                {
                    if (x.Ships[i - 1].ShipType == ship)
                    {
                        Console.WriteLine("You've got a duplicate ship, try again!");
                        ship = UserTypeShip(x, Count);
                    }
                }
                else if (Count == 2)
                {
                    if ((x.Ships[i - 1].ShipType == ship) || (x.Ships[i - 2].ShipType == ship))
                    {
                        Console.WriteLine("You've got a duplicate ship, try again!");
                        ship = UserTypeShip(x, Count);
                    }
                }
                else if (Count == 3)
                {
                    if ((x.Ships[i - 1].ShipType == ship) || (x.Ships[i - 2].ShipType == ship) || (x.Ships[i - 3].ShipType == ship))
                    {
                        Console.WriteLine("You've got a duplicate ship, try again!");
                        ship = UserTypeShip(x, Count);
                    }
                }
                else if (Count == 4)
                {
                    if ((x.Ships[i - 1].ShipType == ship) || (x.Ships[i - 2].ShipType == ship) || (x.Ships[i - 3].ShipType == ship) || (x.Ships[i - 4].ShipType == ship))
                    {
                        Console.WriteLine("You've got a duplicate ship, try again!");
                        ship = UserTypeShip(x, Count);
                    }
                }
                break;
            }



            return(ship);
        }
Пример #17
0
        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 static void ShipSetup(Player p1, Player p2)
        {
            int              x           = 0;
            int              y           = 0;
            Coordinate       xy          = new Coordinate(x, y);
            ShipDirection    direction   = ShipDirection.Down;
            bool             IsValid     = true;
            string           DirResponse = "";
            PlaceShipRequest psr         = new PlaceShipRequest();
            ShipPlacement    response;

            for (int k = 0; k < 2; k++)
            {
                if (k == 1)
                {
                    Console.Clear();
                    Console.WriteLine($"{p1.Name}, you're all set up!");
                    Console.WriteLine($"\nNow let's have {p2.Name} set up his ships.");
                    Console.ReadLine();
                }
                foreach (ShipType stype in Enum.GetValues(typeof(ShipType)))
                {
                    do
                    {
                        do
                        {
                            if (k == 0)
                            {
                                Console.Clear();
                                VisualBoard.HitBoard(p1);
                                Console.WriteLine($"{p1.Name}, choose a coordinate for your {stype}:");
                            }
                            else
                            {
                                Console.Clear();
                                VisualBoard.HitBoard(p1);
                                Console.WriteLine($"{p2.Name}, choose a coordinate for your {stype}:");
                            }

                            string cord = Console.ReadLine();

                            xy = ConsoleInput.ConvertToCord(cord);

                            IsValid = ConsoleInput.ValidateCord(xy);
                            Console.Clear();
                        } while (IsValid == false);

                        if (IsValid == true)
                        {
                            do
                            {
                                Console.WriteLine(
                                    $"Now choose a direction for your {stype}\n1 - Up, 2 - Down, 3 - Right, 4 - Left");
                                DirResponse = Console.ReadLine();
                                switch (DirResponse)
                                {
                                case "1":
                                    direction = ShipDirection.Up;
                                    IsValid   = true;
                                    break;

                                case "2":
                                    direction = ShipDirection.Down;
                                    IsValid   = true;
                                    break;

                                case "3":
                                    direction = ShipDirection.Right;
                                    IsValid   = true;
                                    break;

                                case "4":
                                    direction = ShipDirection.Left;
                                    IsValid   = true;
                                    break;

                                default:
                                    Console.Clear();
                                    Console.WriteLine("That was not a valid direction response. Try Again");
                                    Console.ReadLine();
                                    Console.Clear();
                                    IsValid = false;
                                    break;
                                }
                            } while (IsValid == false);
                            Console.Clear();

                            psr.Coordinate = xy;
                            psr.Direction  = direction;
                            psr.ShipType   = stype;
                        }

                        if (k == 0)
                        {
                            response = p1.pBoard.PlaceShip(psr);
                        }
                        else
                        {
                            response = p2.pBoard.PlaceShip(psr);
                        }
                        if (response == ShipPlacement.NotEnoughSpace)
                        {
                            ConsoleOutput.DisplayNotEnoughSpace();
                        }
                        else if (response == ShipPlacement.Overlap)
                        {
                            ConsoleOutput.DisplayOverlap();
                        }
                        else
                        {
                            Console.WriteLine("Good Job");


                            Console.ReadLine();
                        }
                    } while (response != ShipPlacement.Ok);
                }
            }
        }
Пример #19
0
        public static Coordinate InputtoCoordinate()
        {
            Console.WriteLine("Enter A Coordinate (A-J for row, 1-10 for column  EX: A1, B2, C3)");
            int    First_int = 0;
            string Input     = ConsoleInput.ReadLine();

            if (Input.Length <= 1)
            {
                Console.WriteLine("Invalid input, Try again");
                return(InputtoCoordinate());
            }
            string First_String = Input.Substring(0, 1);


            switch (First_String)
            {
            case "A":
                First_int = 1;
                break;

            case "a":
                First_int = 1;
                break;

            case "B":
                First_int = 2;
                break;

            case "b":
                First_int = 2;
                break;

            case "C":
                First_int = 3;
                break;

            case "c":
                First_int = 3;
                break;

            case "D":
                First_int = 4;
                break;

            case "d":
                First_int = 4;
                break;

            case "E":
                First_int = 5;
                break;

            case "e":
                First_int = 5;
                break;

            case "F":
                First_int = 6;
                break;

            case "f":
                First_int = 6;
                break;

            case "G":
                First_int = 7;
                break;

            case "g":
                First_int = 7;
                break;

            case "H":
                First_int = 8;
                break;

            case "h":
                First_int = 8;
                break;

            case "I":
                First_int = 9;
                break;

            case "i":
                First_int = 9;
                break;

            case "J":
                First_int = 10;
                break;

            case "j":
                First_int = 10;
                break;

            default:
                Console.WriteLine("Sorry, try again");
                return(InputtoCoordinate());

                break;
            }

            string Second_String = Input.Substring(Input.Length - 1, 1);
            string Third_String  = Input.Substring(Input.Length - 2, 2);
            int    Second_int    = 0;

            if ((Second_String == "1" || Second_String == "2" || Second_String == "3" || Second_String == "4" || Second_String == "5" || Second_String == "6" || Second_String == "7" || Second_String == "8" || Second_String == "9"))
            {
                Second_int = Convert.ToInt32(Second_String);
            }
            else if (Third_String == "10")
            {
                Second_int = 10;
            }
            else
            {
                Console.WriteLine("Invalid coordinates, try again");
                return(InputtoCoordinate());
            }

            Coordinate Answer = new Coordinate(First_int, Second_int);

            return(Answer);
        }
Пример #20
0
        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;
                }
            }
        }
Пример #21
0
        //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));
        }