Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                IBattleField      battlefield = new Battlefield(10);
                List <Battleship> allShips    = new List <Battleship>();
                List <Battleship> battleships = new List <Battleship>();
                List <Battleship> destroyers  = new List <Battleship>();

                (battlefield, battleships) = CreateAndLoadBattleshipsToBattlefield(battlefield);
                (battlefield, destroyers)  = CreateAndLoadDestroyersToBattlefield(battlefield);

                allShips.AddRange(battleships);
                allShips.AddRange(destroyers);

                do
                {
                    Console.Clear();
                    Console.WriteLine(battlefield.ReturnBattlefieldView());

                    (int x, int y) = GetCoorinatesFromUser(battlefield);

                    int?idOfTheShip = battlefield.Shoot(x, y);
                    if (idOfTheShip != null)
                    {
                        var ship = allShips.Where(a => a.Id == idOfTheShip).FirstOrDefault();
                        Battleship.ShipResponse response = ship.DestroyMast(x, y);
                        switch (response)
                        {
                        case Battleship.ShipResponse.Hit:
                            Console.WriteLine("Hit");
                            battlefield.GetArray()[x, y].State = FieldState.ShipShooted;
                            break;

                        case Battleship.ShipResponse.Miss:
                            Console.WriteLine("Miss");
                            battlefield.GetArray()[x, y].State = FieldState.EmptyShooted;
                            break;

                        case Battleship.ShipResponse.Sunk:
                            Console.WriteLine("Sunk");
                            battlefield.GetArray()[x, y].State = FieldState.ShipShooted;
                            break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Miss");
                        battlefield.GetArray()[x, y].State = FieldState.EmptyShooted;
                    }

                    Thread.Sleep(700);
                } while (allShips.Any(a => a.IsTheShipAlive()));

                Console.WriteLine("You win !");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine("An unexpected problem has occurred.");
                Console.ReadKey();
            }
        }