Пример #1
0
        private void TestCtor(Cell c, Direction dir)
        {
            MovementNode m = new MovementNode(c, dir);

            Assert.AreEqual(c, m.Cell);
            Assert.AreEqual(dir, m.MovementDirection);
        }
Пример #2
0
        private void LengthChangeTest(Grid g, Cell c, int length, Direction dir)
        {
            Snake        s       = new Snake(g, c, length, dir);
            MovementNode oldHead = s.Head;
            MovementNode oldEnd  = s.End;

            s.Length = s.Length;
            Assert.AreEqual(oldHead, s.Head, "Settin Snake.Length to the same value as it currently has should change nothing (head check).");
            Assert.AreEqual(oldEnd, s.End, "Settin Snake.Length to the same value as it currently has should change nothing (end check).");
            int oldLength = s.Length;
            int newLength = oldLength + 5;

            s.Length = newLength;
            Assert.AreEqual(newLength, s.Length, "Setting the Snake.Length property should update the Snake.Length getter (increment).");
            Assert.AreEqual(oldHead, s.Head, "Setting the Snake.Length property should not change the snake's head (increment).");
            Assert.AreNotEqual(oldEnd, s.End, "Setting the Snake.Length property to any value except the value it already had should change Snake.End (increment).");
            List <MovementNode> nodes = new List <MovementNode>(s.Tail);

            Assert.AreEqual(newLength, nodes.Count, "The number of MovementNodes in Snake.Tail must match one-to-one with the snake's length at all times (length increase check).");
            int  offset    = 1;
            int  increment = oldEnd.MovementDirection.Invert().Sign();
            bool xAxis     = dir.GetAxis() == Axis2D.X;
            Cell check;

            for (int i = oldLength; i < newLength; i++, offset++)
            {
                Assert.AreEqual(oldEnd.MovementDirection, nodes[i].MovementDirection, "Increasing Snake.Length should create new MovementNodes in the same direction as Snake.End.MovementDirection.");
                check = xAxis ? g[oldEnd.Cell.X + offset * increment, oldEnd.Cell.Y] : g[oldEnd.Cell.X, oldEnd.Cell.Y + offset * increment];
                Assert.AreEqual(check, nodes[i].Cell, "Increasing Snake.Length should create new MovementNodes with a position off by one of the previous MovementNode, in accordance with MovementDirection.");
            }
            oldEnd     = s.End;
            newLength -= 2;
            s.Length   = newLength;
            Assert.AreEqual(newLength, s.Length, "Setting the Snake.Length property should update the Snake.Length getter (decrement).");
            Assert.AreEqual(oldHead, s.Head, "Setting the Snake.Length property should not change the snake's head (decrement).");
            Assert.AreNotEqual(oldEnd, s.End, "Setting the Snake.Length property to any value except the value it already had should change Snake.End (decrement).");
            List <MovementNode> newNodes = new List <MovementNode>(s.Tail);

            Assert.AreEqual(newLength, newNodes.Count, "The number of MovementNodes in Snake.Tail must match one-to-one with the snake's length at all times (length decrease check).");
            Assert.IsFalse(newNodes.Contains(nodes[nodes.Count - 1]), "Decreasing the snake's length by n should remove n nodes from the end of the tail (count - 1).");
            Assert.IsFalse(newNodes.Contains(nodes[nodes.Count - 2]), "Decreasing the snake's length by n should remove n nodes from the end of the tail (count - 2).");
        }