Esempio n. 1
0
        public void TestSnakePeekNodeAndValue()
        {
            var snake = new SnakeItem();

            var node = new Tuple <int, int>(1, 1);

            snake.AddNode(node, 10);

            node = new Tuple <int, int>(1, 2);
            snake.AddNode(node, 20);

            Assert.IsTrue(snake.PeekNode().Item1 == 1 && snake.PeekNode().Item2 == 2);
            Assert.IsTrue(snake.SnakeValue == 30);
        }
Esempio n. 2
0
        public bool FeedFrom(int i, int j)
        {
            // instantiate a new stack to serve to create incomplete snakes
            Stack <SnakeItem> myStack = new Stack <SnakeItem>();

            // first snake is created
            var snake = new SnakeItem();

            snake.AddNode(new Tuple <int, int>(i, j), meal[i][j]);
            myStack.Push(snake);

            int count = 1;
            var found = false;

            while (myStack.Count > 0 && !found)
            {
                // for each snake from stack, a new node is created based on its position
                snake = myStack.Pop();
                if (snake.NodeCount == NODEMAX)
                {
                    // once a snake is completed with 7 nodes, then born
                    // verify if existent snake has same value
                    found = SnakeBirth(snake);
                }
                else
                {
                    // here is the core
                    // for last position on its node is calculated new position
                    // taking into account element adjacents
                    var lastNodeFromSnake = snake.PeekNode();
                    List <Tuple <int, int> > nextPositions = Validator.Position(lastNodeFromSnake, MaxWidth, MaxHeigh);
                    foreach (var pos in nextPositions)
                    {
                        if (Validator.HasMinAdjacents(pos, snake.Nodes, 1, MaxWidth, MaxHeigh))
                        {
                            var newSnake = snake.DeepClone();
                            if (newSnake.CanAdd(pos))
                            {
                                // once element adjacent restriction is satisfied, a new node is added to our snake
                                newSnake.AddNode(pos, meal[pos.Item1][pos.Item2]);
                                myStack.Push(newSnake);
                            }
                        }
                    }
                }
                count++;
            }
            return(found);
        }