Esempio n. 1
0
        public void RandomFill(int seed, Difficulty diff)
        {
            if (Populated)
            {
                tiles = new PrimaryTile[10, 10];
            }
            Random   r = new Random(seed);
            CoordSet filled;
            int      i = 0;

            switch (diff)
            {
            case Difficulty.Easy:

                while (i < 5)
                {
                    Ship s = Ship.CreateRandom(r, ClassicShipLengths[i]);
                    if (TryAddShip(s))
                    {
                        i++;
                    }
                }
                break;

            case Difficulty.Medium:

                filled = new CoordSet();
                while (i < 5)
                {
                    Ship s = Ship.CreateRandom(r, ClassicShipLengths[i]);
                    if (filled.Overlaps(s.GetOccupiedArea(0)))
                    {
                        continue;
                    }

                    filled.Add(s.GetOccupiedArea(1));

                    TryAddShip(s);
                    i++;
                }
                break;

            case Difficulty.Hard:

                filled = new CoordSet();
                var border = new CoordSet();

                for (int j = 0; j < 10; j++)
                {
                    border.Add(new CoordPair(0, j));
                    border.Add(new CoordPair(j, 0));
                    border.Add(new CoordPair(9, j));
                    border.Add(new CoordPair(j, 9));
                }

                while (i < 5)
                {
                    Ship s        = Ship.CreateRandom(r, ClassicShipLengths[i]);
                    var  shipArea = s.GetOccupiedArea(0);

                    if (shipArea.Overlaps(filled))
                    {
                        continue;
                    }

                    var weakArea = new CoordSet(border);
                    weakArea.IntersectWith(shipArea);
                    if (r.NextDouble() < weakArea.Count * 0.4)
                    {
                        continue;
                    }

                    border.Add(s.GetOccupiedArea(1));
                    filled.Add(shipArea);

                    TryAddShip(s);
                    i++;
                }
                break;
            }
            Populated = true;
            BoardChanged?.Invoke(this, new EventArgs());
        }