Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SnakeGame.GridSystem.Grid"/> class.
        /// </summary>
        /// <param name="width">The grid's width.</param>
        /// <param name="height">The grid's height.</param>
        public Grid(int width, int height)
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width", width, "Width must be a positive number.");
            }
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", height, "Height must be a positive number.");
            }
            _rng   = new Random();
            _cells = new List <List <Cell> >(height);
            List <Cell> row;
            int         x;

            for (int y = 0; y < height; y++)
            {
                row = new List <Cell>(width);
                for (x = 0; x < width; x++)
                {
                    row.Add(new Cell(this, x, y));
                }
                _cells.Add(row);
            }
            Width  = width;
            Height = height;
            RangeX = new RangeRestriction <int>(0, width, false, true);
            RangeY = new RangeRestriction <int>(0, height, false, true);
        }
Ejemplo n.º 2
0
        public void CtorOverloads()
        {
            RangeRestriction <short> r = new RangeRestriction <short>(0, 3);

            Assert.AreEqual(0, r.LowerBound, "Two-T overload must set the first parameter as it is set in the main contsructor.");
            Assert.AreEqual(3, r.UpperBound, "Two-T overload must set the second parameter as it is set in the main contsructor.");
            Assert.IsFalse(r.LowerExclusive, "Two-T overload must set LowerExclusive to false.");
            Assert.IsFalse(r.UpperExclusive, "Two-T overload must set UpperExclusive to false.");
        }
Ejemplo n.º 3
0
        private void TestCorrectVals <T>(T low, T high, bool lowEx, bool highEx) where T : IComparable <T>
        {
            RangeRestriction <T> r = new RangeRestriction <T>(low, high, lowEx, highEx);

            if (low.CompareTo(high) > 0)
            {
                T temp = low;
                low  = high;
                high = temp;
            }
            Assert.AreEqual(low, r.LowerBound, "LowerBound must correspond to the lowest passed value out of the 1st and 2nd constructor parameters.");
            Assert.AreEqual(high, r.UpperBound, "Upper must correspond to the lowest passed value out of the 1st and 2nd constructor parameters.");
            Assert.AreEqual(lowEx, r.LowerExclusive, "LowerExclusive must correspond to the 3rd constructor parameter.");
            Assert.AreEqual(highEx, r.UpperExclusive, "UpperExclusive must correspond to the 4rd constructor parameter.");
            if (lowEx)
            {
                Assert.IsFalse(r.InRange(low), "If LowerExclusive is true, the point defined by LowerBound should not itself be in range.");
            }
            else
            {
                Assert.IsTrue(r.InRange(low), "If LowerExclusive is false, the point defined by LowerBound should itself be in range.");
            }
            if (highEx)
            {
                Assert.IsFalse(r.InRange(high), "If UpperExclusive is true, the point defined by UpperBound should not itself be in range.");
            }
            else
            {
                Assert.IsTrue(r.InRange(high), "If UpperExclusive is false, the point defined by UpperBound should itself be in range.");
            }
            RangeRestriction <T> r2 = new RangeRestriction <T>(low, high, lowEx, highEx);

            Assert.AreEqual(r, r2, "RangeRestrictin<T> structural equality must be defined (AssertEquals).");
            Assert.IsTrue(r.Equals(r2), "RangeRestrictin<T> structural equality must be defined (IEquatable Equals).");
            Assert.IsTrue(r == r2, "RangeRestrictin<T> structural equality must be defined (== operator).");
            Assert.IsFalse(r != r2, "RangeRestrictin<T> structural inequality must be defined (!= operator).");
            Assert.IsInstanceOf <IEquatable <RangeRestriction <T> > >(r, "RangeRestriction<T> must implement IEquatable<RangeRestriction<T>>.");
            Assert.AreEqual((r.LowerExclusive ? "(" : "[") + r.LowerBound.ToString() + ", " + r.UpperBound.ToString() + (r.UpperExclusive ? ")" : "]"), r.ToString(), "ToString() must output the correct format.");
        }
Ejemplo n.º 4
0
        private void TestValidGrid(int width, int height)
        {
            Grid g = new Grid(width, height);

            Assert.AreEqual(width, g.Width, "Width must correspond to the first constructor argument.");
            Assert.AreEqual(height, g.Height, "Height must correspond to the second constructor argument.");
            RangeRestriction <int> xRestrict = new RangeRestriction <int>(0, width, false, true);
            RangeRestriction <int> yRestrict = new RangeRestriction <int>(0, height, false, true);

            Assert.AreEqual(xRestrict, g.RangeX, "RangeX must be [0, width).");
            Assert.AreEqual(yRestrict, g.RangeY, "RangeY must be [0, height).");
            Assert.AreEqual(g.RangeX, g.GetAxisRange(Axis2D.X), "GetAxisRange(Axis2D.X) must return the same value as RangeX.");
            Assert.AreEqual(g.RangeY, g.GetAxisRange(Axis2D.Y), "GetAxisRange(Axis2D.Y) must return the same value as RangeY.");
            Assert.AreEqual(width * height, g.CellCount, "Total number of cells must be width * height.");
            int x;

            for (int y = 0; y < height; y++)
            {
                for (x = 0; x < width; x++)
                {
                    Assert.IsTrue(g.IsDefined(x, y), "All points in range must be defined.");
                    Assert.AreNotEqual(Cell.INVALID_CELL, g[x, y], "A point in range must be a valid cell.");
                }
            }
            Assert.IsFalse(g.IsDefined(-1, 0), "Out-of-range points must be undefined (-1, 0).");
            Assert.IsFalse(g[-1, 0].IsValid, "Out-of-range cells must be invalid cells (-1, 0).");
            Assert.IsFalse(g.IsDefined(0, -1), "Out-of-range points must be undefined (0, -1).");
            Assert.IsFalse(g[0, -1].IsValid, "Out-of-range cells must be invalid cells (0, -1).");
            Assert.IsFalse(g.IsDefined(-1, -1), "Out-of-range points must be undefined (-1, -1).");
            Assert.IsFalse(g[-1, -1].IsValid, "Out-of-range cells must be invalid cells (-1, -1).");
            Assert.IsFalse(g.IsDefined(width, 0), "Out-of-range points must be undefined (width, 0).");
            Assert.IsFalse(g[width, 0].IsValid, "Out-of-range cells must be invalid cells (width, 0).");
            Assert.IsFalse(g.IsDefined(0, height), "Out-of-range points must be undefined (0, height).");
            Assert.IsFalse(g[0, height].IsValid, "Out-of-range cells must be invalid cells (0, height).");
            Assert.IsFalse(g.IsDefined(width, height), "Out-of-range points must be undefined (width, height).");
            Assert.IsFalse(g[width, height].IsValid, "Out-of-range cells must be invalid cells (width, height).");
            Assert.IsInstanceOf <IEnumerable <Cell> >(g, "Grid must implement IEnumerable<Cell>.");
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Determines whether the specified <see cref="SnakeGame.GridSystem.RangeRestriction{T}"/> is equal to the current <see cref="SnakeGame.GridSystem.RangeRestriction{T}"/>.
 /// </summary>
 /// <param name="other">The <see cref="SnakeGame.GridSystem.RangeRestriction{T}"/> to compare with the current <see cref="SnakeGame.GridSystem.RangeRestriction{T}"/>.</param>
 /// <returns><c>true</c> if the specified <see cref="SnakeGame.GridSystem.RangeRestriction{T}"/> is equal to the current
 /// <see cref="SnakeGame.GridSystem.RangeRestriction{T}"/>; otherwise, <c>false</c>.</returns>
 public bool Equals(RangeRestriction <T> other)
 {
     return(LowerBound.CompareTo(other.LowerBound) == 0 && UpperBound.CompareTo(other.UpperBound) == 0 && LowerExclusive == other.LowerExclusive && UpperExclusive == other.UpperExclusive);
 }