コード例 #1
0
        static void Main(string[] args)
        {
            var battleShipLogic   = new BattleShipLogic();
            var gameFeedBackLogic = new GameFeedBackLogic();



            gameFeedBackLogic.WelcomeMessage();
            gameFeedBackLogic.GameExplanation();

            var userInput = ReadKey();



            if (userInput.Key == ConsoleKey.Enter)
            {
                Clear();
                bool IsGameInPlay = true;

                do
                {
                    battleShipLogic.StartGame(battleShipLogic, gameFeedBackLogic, IsGameInPlay);
                }while (IsGameInPlay && battleShipLogic.ShotsRemaining != 0 || battleShipLogic.HitCount != 5);
            }

            if (userInput.Key == ConsoleKey.Escape)
            {
                Environment.Exit(-1);
            }
        }
コード例 #2
0
        public void StartGame(BattleShipLogic battleShipLogic, GameFeedBackLogic gameFeedBackLogic, bool IsGameInPlay)
        {
            battleShipLogic.ShowGrid();
            var Xaxis = battleShipLogic.GetPlayerXAxis();
            var Yaxis = battleShipLogic.GetPlayerYAxis();

            var IsTheShipHit = battleShipLogic.FireShot(Xaxis, Yaxis);
            var IsGameOver   = battleShipLogic.GameIsOver();

            void ReStartOrEndGame()
            {
                var ReStartGame = ReadKey();
                var EndGame     = ReadKey();

                if (ReStartGame.Key == ConsoleKey.Enter)
                {
                    battleShipLogic.HitCount       = 0;
                    battleShipLogic.ShotsRemaining = 8;

                    battleShipLogic.SetPosition();
                    gameFeedBackLogic.GameRestarted(battleShipLogic.HitCount, battleShipLogic.ShotsRemaining);
                    StartGame(battleShipLogic, gameFeedBackLogic, IsGameInPlay);
                }

                if (EndGame.Key == ConsoleKey.Escape)
                {
                    gameFeedBackLogic.GameEnded();
                    Environment.Exit(-1);
                }
            }

            if (!IsTheShipHit)
            {
                if (IsGameOver)
                {
                    gameFeedBackLogic.NoMoreAmmo(5 - battleShipLogic.HitCount, battleShipLogic.ShotsRemaining);

                    ReStartOrEndGame();
                }
            }
            if (IsTheShipHit && battleShipLogic.ShipIsSunk() == false)
            {
                Clear();

                gameFeedBackLogic.YouHitTheShip(5 - battleShipLogic.HitCount, battleShipLogic.ShotsRemaining);
                battleShipLogic.SetPosition();
                StartGame(battleShipLogic, gameFeedBackLogic, IsGameInPlay);
            }
            if (IsTheShipHit == false && battleShipLogic.ShipIsSunk() == false)
            {
                Clear();

                gameFeedBackLogic.YouMissedTheShip(5 - battleShipLogic.HitCount, battleShipLogic.ShotsRemaining);
                WriteLine();
            }

            if (battleShipLogic.ShipIsSunk() == true && battleShipLogic.HitCount == 5)
            {
                Clear();

                gameFeedBackLogic.YouSunkTheShip(5 - battleShipLogic.HitCount, battleShipLogic.ShotsRemaining);

                ReStartOrEndGame();
            }
        }