Exemple #1
0
        public void PlacePlayerShips(List <Ship> playerShips, IPositionParser positionParser, IPositionValidator positionValidator, IShipManager shipManager, Battlefield battlefield)
        {
            foreach (var ship in playerShips)
            {
                Console.Clear();
                Console.WriteLine();
                //display battle field
                Console.WriteLine(GraphicManager.DisplayBattlefield(battlefield.ColumnSize, battlefield.RowSize, playerShips));
                //display place table
                Console.WriteLine(GraphicManager.DisplayPlaceTable(playerShips, ship.ShipType));

                //get the StartPosition
                Position position;
                do
                {
                    Console.Write(" Enter position for {0} > ", ship.ShipType);
                    position = positionParser.Parse(Console.ReadLine());
                } while (position == null);

                //get the direction
                Direction direction;
                do
                {
                    direction = GetDirection();
                } while (direction == Direction.None);

                //get whole ship positions
                var shipPositions = shipManager.WholeShipPositions(position, ship.Size, direction);

                //validate player position
                shipPositions = ValidatePlayerPositions(playerShips, positionParser, positionValidator, shipManager, shipPositions, ship, battlefield);

                ship.Positions.AddRange(shipPositions);
                SoundEffects.SetShipSoundPlayer();
            }
        }
 protected override void Establish()
 {
     _positionText   = "    \t   \r\n   ";
     _positionParser = PositionParserBuilder.Build();
     _position       = null;
 }
 protected override void Establish()
 {
     _positionText   = "5 z N";
     _positionParser = PositionParserBuilder.Build();
 }
Exemple #4
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
        }
Exemple #5
0
 protected override void Establish()
 {
     _positionText   = String.Empty;
     _positionParser = PositionParserBuilder.Build();
     _position       = null;
 }
Exemple #6
0
        public static Position PlayerShoot(Player player, Player computer, Battlefield battlefield, IPositionParser positionParser, IPositionValidator positionValidator)
        {
            GraphicManager.DisplayBattleView(player, computer, battlefield);

            // isShootOk
            Position iShootPosition;

            do
            {
                Console.WriteLine("                                               \" I Shoot \" ");
                Console.WriteLine();
                Console.Write("                                                POSITION >");

                //convert to "Position"
                iShootPosition = positionParser.Parse(Console.ReadLine());
            } while (positionValidator.IsShootOk(iShootPosition, battlefield.ColumnSize, battlefield.RowSize, computer) == false);
            return(iShootPosition);
        }
Exemple #7
0
        public static Position ComputerShoot(Player player, Player computer, Battlefield battlefield, IRandomManager randomManager, IPositionParser positionParser)
        {
            Console.Clear();
            GraphicManager.DisplayBattleView(player, computer, battlefield);
            // [!!!] should be better
            Position pcShootPosition = randomManager.RandomPosition(battlefield.ColumnSize, battlefield.RowSize);

            Console.WriteLine("                                                \" PC Shoot \" ");
            Console.WriteLine();
            Console.Write("                                                  THINKING");
            // imitate Thinking
            Thread.Sleep(1000);
            Console.Write("... ");
            Thread.Sleep(500);
            Console.Write("[ {0} ]", positionParser.BackParser(pcShootPosition));
            Thread.Sleep(500);
            Console.Write(" ...");
            Thread.Sleep(500);
            return(pcShootPosition);
        }
Exemple #8
0
 public void HitWater(Player player, Player computer, Position iShootPosition, Battlefield battlefield, IPositionParser positionParser)
 {
     // Display Graphic
     GraphicManager.DisplayBattleView(player, computer, battlefield);
     //Sound Effects
     SoundEffects.HitWaterSoundPlayer();
     //information
     Console.ForegroundColor = ConsoleColor.DarkCyan;
     Console.WriteLine("                                               \" Shoot [ {0} ]\" ",
                       positionParser.BackParser(iShootPosition));
     Console.WriteLine();
     Console.Write("                                                     LOST ");
     //Thread.Sleep(500);
     //Console.Write("...");
     //Thread.Sleep(1000);
     Console.ReadKey();
 }
Exemple #9
0
        private static List <Position> ValidatePlayerPositions(List <Ship> playerShips, IPositionParser positionParser, IPositionValidator positionValidator, IShipManager shipManager, List <Position> shipPositions, Ship ship, Battlefield battlefield)
        {
            Position  position;
            Direction direction;

            for (int i = 0; i < shipPositions.Count; i++)
            {
                while (positionValidator.IsValidPosition(shipPositions[i], battlefield.ColumnSize, battlefield.RowSize, playerShips) == false)
                {
                    Console.WriteLine();
                    do
                    {
                        Console.Write(" Position is not validated, try again >");
                        position = positionParser.Parse(Console.ReadLine());
                    } while (position == null);

                    do
                    {
                        direction = PlaceManager.GetDirection();
                    } while (direction == Direction.None);

                    shipPositions = shipManager.WholeShipPositions(position, ship.Size, direction);

                    i = -1;
                    break;
                }
            }
            return(shipPositions);
        }
Exemple #10
0
 public RobotParser(IPositionParser positionParser, IRouteParser routeParser)
 {
     _positionParser = positionParser;
     _routeParser    = routeParser;
 }