예제 #1
0
 public Ship(CoordPair loc, int length, bool horizontal)
 {
     if (length > 5 || length < 2)
     {
         throw new ArgumentException("Invalid Length");
     }
     if (horizontal)
     {
         if (length + loc.X - 1 > 9)
         {
             throw new ArgumentException("Invalid Coordinates");
         }
     }
     else
     {
         if (length + loc.Y - 1 > 9)
         {
             throw new ArgumentException("Invalid Coordinates");
         }
     }
     Location       = loc;
     Length         = length;
     Horizontal     = horizontal;
     UndamagedTiles = GetOccupiedArea(0);
     DamagedTiles   = new CoordSet();
 }
예제 #2
0
 public void DisplaySet(CoordSet cs)
 {
     tiles = new TrackerTile[10, 10];
     foreach (var item in cs.GetAllCoords())
     {
         this[item] = TrackerTile.Hit;
     }
 }
예제 #3
0
        public bool Overlaps(IEnumerable <CoordPair> other)
        {
            var otherSet = other as CoordSet;

            if (otherSet == null)
            {
                otherSet = new CoordSet(other);
            }

            return(BitVector.And(fields, otherSet.fields).PopulationCount() > 0);
        }
예제 #4
0
        public void IntersectWith(IEnumerable <CoordPair> other)
        {
            var otherSet = other as CoordSet;

            if (otherSet == null)
            {
                otherSet = new CoordSet(other);
            }

            fields.And(otherSet.fields);
            Count = fields.PopulationCount();
        }
예제 #5
0
        public void ExceptWith(IEnumerable <CoordPair> other)
        {
            var otherSet = other as CoordSet;

            if (otherSet == null)
            {
                otherSet = new CoordSet(other);
            }

            fields.XOr(BitVector.And(fields, otherSet.fields));
            Count = fields.PopulationCount();
        }
예제 #6
0
        public void SymmetricExceptWith(IEnumerable <CoordPair> other)
        {
            var otherSet = other as CoordSet;

            if (otherSet == null)
            {
                otherSet = new CoordSet(other);
            }

            fields.XOr(otherSet.fields);
            Count = fields.PopulationCount();
        }
예제 #7
0
        public void GenRNGHitCandids()
        {
            CoordSet cs = new CoordSet();

            switch (ShipsRemaining.Min())
            {
            case 2:
                for (int i = 0; i < 100; i += 2)
                {
                    cs.Add(new CoordPair(i));
                }
                break;

            case 3:
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        cs.Add(new CoordPair(9 - i, j));
                    }
                }
                break;

            case 4:
                for (int i = 0; i < 4; i++)
                {
                    cs.Add(new CoordPair(i, i + 6));
                    cs.Add(new CoordPair(i + 6, i));
                }
                for (int i = 0; i < 8; i++)
                {
                    cs.Add(new CoordPair(i, i + 2));
                    cs.Add(new CoordPair(i + 2, i));
                }
                break;

            case 5:
                for (int i = 0; i < 10; i++)
                {
                    cs.Add(new CoordPair(i, i));
                }
                for (int i = 0; i < 5; i++)
                {
                    cs.Add(new CoordPair(i, i + 5));
                    cs.Add(new CoordPair(i + 5, i));
                }
                break;
            }

            cs.ExceptWith(AllMoves);
            RandomHitCandidates = cs;
        }
예제 #8
0
        public bool SetEquals(IEnumerable <CoordPair> other)
        {
            var otherSet = other as CoordSet;

            if (otherSet == null)
            {
                otherSet = new CoordSet(other);
            }

            if (otherSet.Count != Count)
            {
                return(false);
            }
            return(otherSet.fields.Equals(fields));
        }
예제 #9
0
        public bool IsSubsetOf(IEnumerable <CoordPair> other)
        {
            if (Count == 0)
            {
                return(true);
            }
            var otherSet = other as CoordSet;

            if (otherSet == null)
            {
                otherSet = new CoordSet(other);
            }

            return(BitVector.Or(otherSet.fields, fields).Equals(otherSet.fields));
        }
예제 #10
0
        public CoordSet GetOccupiedArea(int range)
        {
            CoordSet cs     = new CoordSet();
            int      firstX = Location.X - range;
            int      firstY = Location.Y - range;
            int      lastX  = Location.X + range + (Horizontal ? Length - 1 : 0);
            int      lastY  = Location.Y + range + (Horizontal ? 0 : Length - 1);

            for (int x = firstX; x <= lastX; x++)
            {
                for (int y = firstY; y <= lastY; y++)
                {
                    if (CoordPair.IsValidCoord(x, y))
                    {
                        cs.Add(new CoordPair(x, y));
                    }
                }
            }

            return(cs);
        }
예제 #11
0
        public bool IsProperSupersetOf(IEnumerable <CoordPair> other)
        {
            var otherSet = other as CoordSet;

            if (otherSet == null)
            {
                var otherCollection = other as ICollection <CoordPair>;

                if (otherCollection != null && otherCollection.Count == 0 && Count > 0)
                {
                    return(true);
                }
                otherSet = new CoordSet(other);
            }

            if (otherSet.Count == 0 && Count > 0)
            {
                return(true);
            }
            return(BitVector.Or(fields, otherSet.fields).Equals(fields) && Count > otherSet.Count);
        }
예제 #12
0
 public void Add(CoordSet items)
 {
     fields.Or(items.fields);
     Count = fields.PopulationCount();
 }
예제 #13
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());
        }