Esempio n. 1
0
        public static void Main()
        {
            #region [on] 0. Instance
            var battlefield = new Battlefield(10, 10);
            positionParser    = new PositionParser();
            positionValidator = new PositionValidator();
            randomManager     = new RandomManager();
            shipManager       = new ShipManager();
            shootManager      = new ShootManager();
            placeManager      = new PlaceManager();
            var battleBgm = new WindowsMediaPlayer();
            var winnerBgm = new WindowsMediaPlayer();

            #endregion


            #region [off] 1. Welcome Screen
            //GraphicManager.WelcomeScreen();
            #endregion


            #region [on] 1.1. BGM

            SoundEffects.BattleBgmPlayer(battleBgm);

            #endregion


            #region [on] 2. Ships Topf

            // instance two players
            var player = new Player();
            player.Positions = new List <Position>();
            player.Hits      = new List <Hit>();
            player.Ships     = new List <Ship>();

            var computer = new Player();
            computer.Positions = new List <Position>();
            computer.Hits      = new List <Hit>();
            computer.Ships     = new List <Ship>();

            #endregion


            #region [on] 3. Place PlayerShips and ComputerShips

            // create player Ships
            player.Ships = InitializeShips();
            // place player ships
            // [!!]  placeManager.PlacePlayerShips(player.Ships, positionParser, positionValidator, shipManager,battlefield);
            placeManager.PlaceComputerShips(player.Ships, randomManager, shipManager, positionValidator, battlefield);

            // create ships for pc
            computer.Ships = InitializeShips();
            // place pc ships
            placeManager.PlaceComputerShips(computer.Ships, randomManager, shipManager, positionValidator, battlefield);

            // Add positions into Instances
            foreach (var playerShip in player.Ships)
            {
                player.Positions.AddRange(playerShip.Positions);
            }
            var allPlayerShipsPositions = player.Positions;

            foreach (var computerShip in computer.Ships)
            {
                computer.Positions.AddRange(computerShip.Positions);
            }
            var allComputerShipsPositions = computer.Positions;

            #endregion


            #region + [on] display pcShips for Testing
            Console.Clear();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("        God Mode");
            Console.WriteLine("        PC Ships:");
            Console.WriteLine();
            Console.WriteLine(GraphicManager.DisplayBattlefield(battlefield.ColumnSize, battlefield.RowSize, computer.Ships).PadRight(20));
            Console.ReadKey();
            SoundEffects.SetShipSoundPlayer();
            #endregion


            #region  + [off] display Win view for Testing

            //battleBgm.close();
            //EndGameManager.WhoWin(player, computer, winnerBgm, shootManager);
            //EndGameManager.RestartGame(winnerBgm);

            #endregion


            #region [on] 4. Game begin view

            GraphicManager.GameStartView(player, computer, battlefield);

            #endregion


            #region [on] 5. Player || PC shots
            do     // End the Game
            {
                do // Player == * ==> PC
                {
                    var iShootPosition = ShootManager.PlayerShoot(player, computer, battlefield, positionParser, positionValidator);

                    if (allComputerShipsPositions.Contains(iShootPosition)) // Hit Ship
                    {
                        computer.Hits.Add(new Hit(HitType.Ship, iShootPosition));
                        shootManager.HitShip(player, computer, iShootPosition, computer.Ships, battlefield);
                    }
                    else // Hit Water
                    {
                        computer.Hits.Add(new Hit(HitType.Water, iShootPosition));
                        shootManager.HitWater(player, computer, iShootPosition, battlefield, positionParser);
                    }

                    // Is all Player Ships Sunken
                    if (shootManager.IsAllShipsSunken(computer.Ships))
                    {
                        break;
                    }
                } while (computer.Hits.Last().HitType == HitType.Ship); // one more time shoot


                do // PC == * ==> Player
                {
                    // if All player ships have Sunked skip all those code
                    if (shootManager.IsAllShipsSunken(computer.Ships))
                    {
                        break;
                    }

                    var pcShootPosition = ShootManager.ComputerShoot(player, computer, battlefield, randomManager, positionParser);

                    if (allPlayerShipsPositions.Contains(pcShootPosition)) // Hit Ship
                    {
                        player.Hits.Add(new Hit(HitType.Ship, pcShootPosition));
                        shootManager.HitShip(player, computer, pcShootPosition, player.Ships, battlefield);
                    }
                    else //Hit Water
                    {
                        player.Hits.Add(new Hit(HitType.Water, pcShootPosition));
                        shootManager.HitWater(player, computer, pcShootPosition, battlefield, positionParser);
                    }

                    // Check if all Player Ship Sunken
                    if (shootManager.IsAllShipsSunken(player.Ships))
                    {
                        break;
                    }
                } while (player.Hits.Last().HitType == HitType.Ship);                                                                 // one more time shoot
            } while (shootManager.IsAllShipsSunken(computer.Ships) == false && shootManager.IsAllShipsSunken(player.Ships) == false); // End the game

            #endregion


            #region [on] 6. Game End View

            battleBgm.close();
            EndGameManager.WhoWin(player, computer, winnerBgm, shootManager);
            EndGameManager.RestartGame(winnerBgm);

            #endregion
        }