Beispiel #1
0
        public bool ValidateMap()
        {
            if (Map.Length != MapSize)
            {
                return(false);
            }
            for (int i = 0; i < Map.Length; i++)
            {
                if (Map[i].Length != MapSize)
                {
                    return(false);
                }
            }

            SearchedCells = new bool[MapSize][];
            for (int i = 0; i < MapSize; i++)
            {
                SearchedCells[i] = new bool[MapSize];
                for (int j = 0; j < MapSize; j++)
                {
                    SearchedCells[i][j] = false;
                }
            }


            ShipPartsLeft = new List <int[]>();

            //ShipsToFound = ShipList;
            ShipsToFound = new short[4];
            ShipList.CopyTo(ShipsToFound, 0);


            for (int i = 0; i < MapSize; i++)
            {
                for (int j = 0; j < MapSize; j++)
                {
                    if (SearchedCells[i][j])
                    {
                        continue;
                    }
                    SearchedCells[i][j] = true;
                    if (Map[i][j] == 0)
                    {
                        continue;
                    }
                    else if (Map[i][j] == 1)
                    {
                        if (!CheckSlants(i, j))
                        {
                            return(false);
                        }

                        int sxNeg = FindInDirection(i - 1, j, -1, 0);
                        int sxPos = FindInDirection(i + 1, j, 1, 0);

                        int syNeg = FindInDirection(i, j - 1, 0, -1);
                        int syPos = FindInDirection(i, j + 1, 0, 1);

                        if (sxNeg + sxPos + syNeg + syPos > ShipsToFound.Length - 1)
                        {
                            return(false);
                        }

                        ShipsToFound[sxNeg + sxPos + syNeg + syPos] -= 1;

                        if (sxNeg + sxPos > 0)
                        {
                            int x = i + sxPos;
                            while (x >= i - sxNeg)
                            {
                                if (!CheckSlants(x, j))
                                {
                                    return(false);
                                }
                                ShipPartsLeft.Add(new int[] { x, j });
                                x--;
                            }
                        }
                        else if (syNeg + syPos > 0)
                        {
                            int y = j + syPos;
                            while (y >= j - syNeg)
                            {
                                if (!CheckSlants(i, y))
                                {
                                    return(false);
                                }
                                ShipPartsLeft.Add(new int[] { i, y });
                                y--;
                            }
                        }
                        else
                        {
                            if (!CheckSlants(i, j))
                            {
                                return(false);
                            }
                            ShipPartsLeft.Add(new int[] { i, j });
                        }
                    }
                }
            }

            for (int i = 0; i < ShipsToFound.Length; i++)
            {
                if (ShipsToFound[i] != 0)
                {
                    return(false);
                }
            }


            return(true);
        }