Example #1
0
        internal static GridSpotModel AskForShot(PlayerInfoModel activePlayer, PlayerInfoModel opponent)
        {
            bool isValidShot = false;

            UIDisplay.DisplayGrid(activePlayer.ShotsGrid);
            Console.WriteLine();
            Console.Write($"{activePlayer.PlayerName}: Where would you like to fire: ");
            string gridSpotSelection = Console.ReadLine();

            GridSpotModel output = UILogic.ParseStringToGridSpot(gridSpotSelection);

            isValidShot = GameLogic.ValidateShot(output, opponent);

            while (isValidShot == false)
            {
                Console.Clear();
                Console.WriteLine($"Oops!  \"{gridSpotSelection}\" was not a valid shot.  Please try again.");

                UIDisplay.DisplayGrid(activePlayer.ShotsGrid);
                Console.WriteLine();
                Console.Write($"{activePlayer.PlayerName}: Where would you like to fire: ");
                gridSpotSelection = Console.ReadLine();

                output = UILogic.ParseStringToGridSpot(gridSpotSelection);

                isValidShot = GameLogic.ValidateShot(output, opponent);
            }
            return(output);
        }
Example #2
0
        static void Main(string[] args)
        {
            UIDisplay.ApplicationStartMessage();

            PlayerInfoModel activePlayer = UILogic.CreatePlayer("Player 1");
            PlayerInfoModel opponent     = UILogic.CreatePlayer("Player 2");

            PlayerInfoModel winner = null;

            do
            {
                UILogic.TakeTurn(activePlayer, opponent);

                //Determine if the game is over
                if (GameLogic.GameIsOver(opponent) == true)
                {
                    UIDisplay.EndGameMessage(activePlayer);

                    winner = activePlayer;
                }

                else
                {
                    //Switch player spots for next turn
                    (activePlayer, opponent) = (opponent, activePlayer);
                }
            } while (winner == null);
        }
Example #3
0
        internal static PlayerInfoModel CreatePlayer(string playerTag)
        {
            Console.WriteLine($"{playerTag}");

            PlayerInfoModel newPlayer = new PlayerInfoModel();

            UIDisplay.GetPlayerName(newPlayer);
            UIDisplay.GetShipPlacements(newPlayer);

            Console.Clear();

            //Creates player's shot grid
            newPlayer.ShotsGrid = GameLogic.InitializeGrid();

            //Sets player's number of remaining ships property
            newPlayer.RemainingShips = GameLogic.QuantifyRemainingShips(newPlayer);

            //Sets player's number of turns to zero
            newPlayer.TotalTurns = 0;

            return(newPlayer);
        }
Example #4
0
        internal static void TakeTurn(PlayerInfoModel activePlayer, PlayerInfoModel opponent)
        {
            UIDisplay.DisplayTurnHeader(activePlayer, opponent);

            GridSpotModel shotRequest = UIDisplay.AskForShot(activePlayer, opponent);

            //Validates the shot and loops until the shot is valid
            bool isValidShot = GameLogic.ValidateShot(shotRequest, opponent);

            while (isValidShot == false)
            {
                UIDisplay.DisplayGrid(activePlayer.ShotsGrid);

                shotRequest = UIDisplay.AskForShot(activePlayer, opponent);
                isValidShot = GameLogic.ValidateShot(shotRequest, opponent);
            }

            //Once the player calls a valid shot, the program determins if it's a hit or a miss,
            //records the shot, and changes the opponent's number of remianing ships property
            if (GameLogic.IsAHit(shotRequest, opponent) == true)
            {
                GameLogic.RecordShot(shotRequest, activePlayer, opponent);

                opponent.RemainingShips = GameLogic.QuantifyRemainingShips(opponent);

                UIDisplay.DisplayHitMessage(opponent);
            }

            else
            {
                GameLogic.RecordShot(shotRequest, activePlayer, opponent);

                opponent.RemainingShips = GameLogic.QuantifyRemainingShips(opponent);

                UIDisplay.DisplayMissMessage();
            }
        }