Exemple #1
0
 static void Main(string[] args)
 {
     int[,] field = new int[10, 10]
     {
         { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
         { 1, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
         { 1, 0, 1, 0, 1, 1, 1, 0, 1, 0 },
         { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
         { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
         { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 },
         { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
         { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
         { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
     };
     BattleshipField.ValidateBattlefield(field);
 }
        public static bool ValidateBattlefield(int[,] field)
        {
            BattleshipField game = new BattleshipField(field);

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (game.battleField[i, j] == 0 || game.chekedСells[i, j] == cellType.ShipCell)
                    {
                        continue;
                    }
                    Ship newShip = game.GetShip(i, j);
                    if (newShip == null)
                    {
                        return(false);
                    }
                    switch (newShip.Name)
                    {
                    case "BattleShip":
                        if (++game.battleShipCount > 1)
                        {
                            return(false);
                        }
                        break;

                    case "Cruiser":
                        if (++game.cruiserCount > 2)
                        {
                            return(false);
                        }
                        break;

                    case "Destroyer":
                        if (++game.destroyerCount > 3)
                        {
                            return(false);
                        }
                        break;

                    case "Submarine":
                        if (++game.submarineCount > 4)
                        {
                            return(false);
                        }
                        break;
                    }
                    if (!game.ChekAroundCells(newShip))
                    {
                        return(false);
                    }
                }
            }
            if (game.battleShipCount != 1 || game.cruiserCount != 2 ||
                game.destroyerCount != 3 || game.submarineCount != 4)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }