protected override void Execute(Node puzzle)
        {
            _queue.Enqueue(puzzle);
            while (_queue.Count != 0)
            {
                Node node = _queue.Dequeue();
                if (!base._visitedNodes.HasNode(node))
                {
                    node.State = ENodeState.Gray;
                    base._visitedNodes.Add(node);
                }

                if (base.IsEqualToSolution(node))
                {
                    base.PrintList();
                    break;
                }

                List<Node> nodeList = ExpandedNodesFinder.ExpandNode(node);
                foreach (Node expandedNode in nodeList)
                {
                    if (!base._visitedNodes.HasNode(expandedNode))
                    {
                        _queue.Enqueue(expandedNode);
                        expandedNode.State = ENodeState.Gray;
                        base._visitedNodes.Add(expandedNode);
                    }
                }

                node.State = ENodeState.Black;

            }
        }
        protected override void Execute(Node puzzle)
        {
            if (!base._visitedNodes.HasNode(puzzle))
            {
                puzzle.State = ENodeState.Gray;
                base._visitedNodes.Add(puzzle);
            }

            if (base.IsEqualToSolution(puzzle))
            {
                base.PrintList();
                _solved = true;
                return;
            }

            if (_solved)
            {
                return;
            }

            List<Node> expandedNodes = ExpandedNodesFinder.ExpandNode(puzzle);
            foreach (Node node in expandedNodes)
            {
                if (!base._visitedNodes.HasNode(node))
                {
                    Execute(node);
                }
            }

            puzzle.State = ENodeState.Black;
        }
 /* testa o 0 movido da posicao {0,0} para {1,0} */
 public void SecondMoveTest() 
 {
     int[,] array = new int[3, 3] { { 0, 2, 3 }, { 4, 5, 6 }, { 7, 8, 1 } };
     Node aNode = new Node(array);
     List<Node> nodeList = ExpandedNodesFinder.ExpandNode(aNode);
     Node answerNode = nodeList.ToArray()[1];
     Assert.AreEqual(answerNode.ToString(), "4 2 3 0 5 6 7 8 1 ");
 }
        public void DifferenceTest()
        {
            BaseAlgorithm bfs = new BreadthFirstSearch();
            int[,] solution = new int[,] { { 1, 5, 3 }, { 8, 4, 6 }, { 7, 2, 0 } };
            Node aNode = new Node(solution);

            Assert.IsFalse(bfs.IsEqualToSolution(aNode));
        }
        public void EqualityTest()
        {
            BaseAlgorithm bfs = new BreadthFirstSearch();
            int[,] solution = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
            Node aNode = new Node(solution);

            Assert.IsTrue(bfs.IsEqualToSolution(aNode));
        }
 public void SeventhMoveTest()
 {
     int[,] array = new int[3, 3] { { 1, 2, 0 }, { 4, 5, 6 }, { 7, 8, 3 } };
     Node aNode = new Node(array);
     List<Node> nodeList = ExpandedNodesFinder.ExpandNode(aNode);
     Node answerNode = nodeList.ToArray()[1];
     Assert.AreEqual(answerNode.ToString(), "1 2 6 4 5 0 7 8 3 ");
 }
 public void FifthMoveTest()
 {
     int[,] array = new int[3, 3] { { 1, 0, 3 }, { 4, 2, 6 }, { 7, 8, 5 } };
     Node aNode = new Node(array);
     List<Node> nodeList = ExpandedNodesFinder.ExpandNode(aNode);
     Node answerNode = nodeList.ToArray()[2];
     Assert.AreEqual(answerNode.ToString(), "1 2 3 4 0 6 7 8 5 ");
 }
 static void Main(string[] args)
 {
     //int[,] array = new int[3, 3] { { 1, 3, 2 }, { 4, 0, 6 }, { 5, 7, 8 } };            
     int[,] array = new int[3, 3] { { 1, 2, 3 }, { 4, 0, 6 }, { 7, 5, 8 } };
     Node aNode = new Node(array);
     BaseAlgorithm bfs = new BreadthFirstSearch();
     bfs.Run(aNode);
     Console.ReadKey();           
 }        
 public void Run(Node puzzle)
 {
     Console.WriteLine("Começou\n");
     _stopwatch.Reset();
     _stopwatch.Start();            
     Execute(puzzle);
     _stopwatch.Stop();            
     Console.WriteLine("Terminou.\nTempo: {0} ms.", GetElapsedTime());
 }
        public void NodeListHasNode()
        {
            List<Node> list = new List<Node>();
            int[,] solution = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
            Node aNode = new Node(solution);
            list.Add(aNode);            
            Node anotherNode = new Node(solution);

            Assert.IsTrue(list.HasNode(anotherNode));

        }
        public static bool HasNode(this List<Node> list, Node aNode)
        {            
            bool result = false;

            foreach(Node node in list)
            {
                result = node.IsEqual(aNode);
                if (result)
                {
                    break;
                }
            }

            return result;            
        }
        public bool IsEqualToSolution(Node aNode)
        {
            bool equal = false;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    equal = aNode.Value[i, j] == _solution.Value[i, j];
                    if (!equal)
                    {
                        break;
                    }
                }

                if (!equal)
                {
                    break;
                }
            }

            return equal;
        }
 private static void FindZero(Node aNode, out int line, out int column)
 {
     line = 0;
     column = 0;
     for (int i = 0; i < 3; i++)
     {
         for (int j = 0; j < 3; j++)
         {
             if (aNode.Value[i, j] == 0)
             {
                 line = i;
                 column = j;
                 break;
             }
         }
     }
 }
 public BaseAlgorithm()
 {            
     _solution = new Node(InitializeArray());
     _stopwatch = new Stopwatch();
     _visitedNodes = new List<Node>();
 }
 public void TwelvethMoveTest()
 {
     int[,] array = new int[3, 3] { { 4, 2, 3 }, { 5, 0, 6 }, { 7, 8, 1 } };
     Node aNode = new Node(array);
     List<Node> nodeList = ExpandedNodesFinder.ExpandNode(aNode);
     Node answerNode = nodeList.ToArray()[1];
     Assert.AreEqual(answerNode.ToString(), "4 2 3 5 8 6 7 0 1 ");            
 }
        public static List<Node> ExpandNode(Node aNode)
        {
            List<Node> expandedNodes = new List<Node>();
            int line, column;
            FindZero(aNode, out line, out column);

            if (line == 0)
            {
                if (column == 0)
                {
                    //move 0 para a direita
                    int[,] firstMoveArray = (int[,])aNode.Value.Clone();
                    firstMoveArray[line, column] = firstMoveArray[line, column + 1];
                    firstMoveArray[line, column + 1] = 0;
                    Node firstNode = new Node(firstMoveArray);

                    //move 0 para baixo
                    int[,] secondMoveArray = (int[,])aNode.Value.Clone();
                    secondMoveArray[line, column] = secondMoveArray[line + 1, column];
                    secondMoveArray[line + 1, column] = 0;
                    Node secondNode = new Node(secondMoveArray);

                    firstNode.Level = aNode.Level + 1;
                    secondNode.Level = aNode.Level + 1;

                    expandedNodes.Add(firstNode);
                    expandedNodes.Add(secondNode);
                }

                if (column == 1)
                {
                    //move 0 para a esquerda
                    int[,] firstMoveArray = (int[,])aNode.Value.Clone();
                    firstMoveArray[line, column] = firstMoveArray[line, column - 1];
                    firstMoveArray[line, column - 1] = 0;
                    Node firstNode = new Node(firstMoveArray);

                    //move 0 para a direita
                    int[,] secondMoveArray = (int[,])aNode.Value.Clone();
                    secondMoveArray[line, column] = secondMoveArray[line, column + 1];
                    secondMoveArray[line, column + 1] = 0;
                    Node secondNode = new Node(secondMoveArray);

                    //move 0 para baixo
                    int[,] thirdMoveArray = (int[,])aNode.Value.Clone();
                    thirdMoveArray[line, column] = thirdMoveArray[line + 1, column];
                    thirdMoveArray[line + 1, column] = 0;
                    Node thirdNode = new Node(thirdMoveArray);

                    firstNode.Level = aNode.Level + 1;
                    secondNode.Level = aNode.Level + 1;
                    thirdNode.Level = aNode.Level + 1;

                    expandedNodes.Add(firstNode);
                    expandedNodes.Add(secondNode);
                    expandedNodes.Add(thirdNode);
                }

                if (column == 2)
                {
                    //move 0 para a esquerda
                    int[,] firstMoveArray = (int[,])aNode.Value.Clone();
                    firstMoveArray[line, column] = firstMoveArray[line, column - 1];
                    firstMoveArray[line, column - 1] = 0;
                    Node firstNode = new Node(firstMoveArray);

                    //move 0 para baixo
                    int[,] secondMoveArray = (int[,])aNode.Value.Clone();
                    secondMoveArray[line, column] = secondMoveArray[line + 1, column];
                    secondMoveArray[line + 1, column] = 0;
                    Node secondNode = new Node(secondMoveArray);

                    firstNode.Level = aNode.Level + 1;
                    secondNode.Level = aNode.Level + 1;

                    expandedNodes.Add(firstNode);
                    expandedNodes.Add(secondNode);
                }

            }

            if (line == 1)
            {
                if (column == 0)
                {
                    //move 0 para a direita
                    int[,] firstMoveArray = (int[,])aNode.Value.Clone();
                    firstMoveArray[line, column] = firstMoveArray[line, column + 1];
                    firstMoveArray[line, column + 1] = 0;
                    Node firstNode = new Node(firstMoveArray);

                    //move 0 para baixo
                    int[,] secondMoveArray = (int[,])aNode.Value.Clone();
                    secondMoveArray[line, column] = secondMoveArray[line + 1, column];
                    secondMoveArray[line + 1, column] = 0;
                    Node secondNode = new Node(secondMoveArray);

                    //move 0 para cima
                    int[,] thirdMoveArray = (int[,])aNode.Value.Clone();
                    thirdMoveArray[line, column] = thirdMoveArray[line - 1, column];
                    thirdMoveArray[line - 1, column] = 0;
                    Node thirdNode = new Node(thirdMoveArray);

                    firstNode.Level = aNode.Level + 1;
                    secondNode.Level = aNode.Level + 1;
                    thirdNode.Level = aNode.Level + 1;

                    expandedNodes.Add(firstNode);
                    expandedNodes.Add(secondNode);
                    expandedNodes.Add(thirdNode);
                }

                if (column == 1)
                {
                    //move 0 para a direita
                    int[,] firstMoveArray = (int[,])aNode.Value.Clone();
                    firstMoveArray[line, column] = firstMoveArray[line, column + 1];
                    firstMoveArray[line, column + 1] = 0;
                    Node firstNode = new Node(firstMoveArray);

                    //move 0 para baixo
                    int[,] secondMoveArray = (int[,])aNode.Value.Clone();
                    secondMoveArray[line, column] = secondMoveArray[line + 1, column];
                    secondMoveArray[line + 1, column] = 0;
                    Node secondNode = new Node(secondMoveArray);

                    //move 0 para cima
                    int[,] thirdMoveArray = (int[,])aNode.Value.Clone();
                    thirdMoveArray[line, column] = thirdMoveArray[line - 1, column];
                    thirdMoveArray[line - 1, column] = 0;
                    Node thirdNode = new Node(thirdMoveArray);

                    //move 0 para a esquerda
                    int[,] fourthMoveArray = (int[,])aNode.Value.Clone();
                    fourthMoveArray[line, column] = fourthMoveArray[line, column - 1];
                    fourthMoveArray[line, column - 1] = 0;
                    Node fourthNode = new Node(fourthMoveArray);

                    firstNode.Level = aNode.Level + 1;
                    secondNode.Level = aNode.Level + 1;
                    thirdNode.Level = aNode.Level + 1;
                    fourthNode.Level = aNode.Level + 1;

                    expandedNodes.Add(firstNode);
                    expandedNodes.Add(secondNode);
                    expandedNodes.Add(thirdNode);
                    expandedNodes.Add(fourthNode);
                }

                if (column == 2)
                {
                    //move 0 para a esquerda
                    int[,] firstMoveArray = (int[,])aNode.Value.Clone();
                    firstMoveArray[line, column] = firstMoveArray[line, column - 1];
                    firstMoveArray[line, column - 1] = 0;
                    Node firstNode = new Node(firstMoveArray);

                    //move 0 para baixo
                    int[,] secondMoveArray = (int[,])aNode.Value.Clone();
                    secondMoveArray[line, column] = secondMoveArray[line + 1, column];
                    secondMoveArray[line + 1, column] = 0;
                    Node secondNode = new Node(secondMoveArray);


                    //move 0 para cima
                    int[,] thirdMoveArray = (int[,])aNode.Value.Clone();
                    thirdMoveArray[line, column] = thirdMoveArray[line - 1, column];
                    thirdMoveArray[line - 1, column] = 0;
                    Node thirdNode = new Node(thirdMoveArray);

                    firstNode.Level = aNode.Level + 1;
                    secondNode.Level = aNode.Level + 1;
                    thirdNode.Level = aNode.Level + 1;

                    expandedNodes.Add(firstNode);
                    expandedNodes.Add(secondNode);
                    expandedNodes.Add(thirdNode);
                }
            }

            if (line == 2)
            {
                if (column == 0)
                {
                    //move 0 para a direita
                    int[,] firstMoveArray = (int[,])aNode.Value.Clone();
                    firstMoveArray[line, column] = firstMoveArray[line, column + 1];
                    firstMoveArray[line, column + 1] = 0;
                    Node firstNode = new Node(firstMoveArray);

                    //move 0 para cima
                    int[,] secondMoveArray = (int[,])aNode.Value.Clone();
                    secondMoveArray[line, column] = secondMoveArray[line - 1, column];
                    secondMoveArray[line - 1, column] = 0;
                    Node secondNode = new Node(secondMoveArray);

                    firstNode.Level = aNode.Level + 1;
                    secondNode.Level = aNode.Level + 1;

                    expandedNodes.Add(firstNode);
                    expandedNodes.Add(secondNode);
                }

                if (column == 1)
                {
                    //move 0 para a esquerda
                    int[,] firstMoveArray = (int[,])aNode.Value.Clone();
                    firstMoveArray[line, column] = firstMoveArray[line, column - 1];
                    firstMoveArray[line, column - 1] = 0;
                    Node firstNode = new Node(firstMoveArray);

                    //move 0 para a direita
                    int[,] secondMoveArray = (int[,])aNode.Value.Clone();
                    secondMoveArray[line, column] = secondMoveArray[line, column + 1];
                    secondMoveArray[line, column + 1] = 0;
                    Node secondNode = new Node(secondMoveArray);

                    //move 0 para cima
                    int[,] thirdMoveArray = (int[,])aNode.Value.Clone();
                    thirdMoveArray[line, column] = thirdMoveArray[line - 1, column];
                    thirdMoveArray[line - 1, column] = 0;
                    Node thirdNode = new Node(thirdMoveArray);

                    firstNode.Level = aNode.Level + 1;
                    secondNode.Level = aNode.Level + 1;
                    thirdNode.Level = aNode.Level + 1;

                    expandedNodes.Add(firstNode);
                    expandedNodes.Add(secondNode);
                    expandedNodes.Add(thirdNode);
                }

                if (column == 2)
                {
                    //move 0 para a esquerda
                    int[,] firstMoveArray = (int[,])aNode.Value.Clone();
                    firstMoveArray[line, column] = firstMoveArray[line, column - 1];
                    firstMoveArray[line, column - 1] = 0;
                    Node firstNode = new Node(firstMoveArray);

                    //move 0 para cima
                    int[,] secondMoveArray = (int[,])aNode.Value.Clone();
                    secondMoveArray[line, column] = secondMoveArray[line - 1, column];
                    secondMoveArray[line - 1, column] = 0;
                    Node secondNode = new Node(secondMoveArray);

                    firstNode.Level = aNode.Level + 1;
                    secondNode.Level = aNode.Level + 1;

                    expandedNodes.Add(firstNode);
                    expandedNodes.Add(secondNode);
                }
            }

            return expandedNodes;
        }
 public void EighteenthMoveTest()
 {
     int[,] array = new int[3, 3] { { 4, 2, 3 }, { 5, 6, 0 }, { 7, 8, 1 } };
     Node aNode = new Node(array);
     List<Node> nodeList = ExpandedNodesFinder.ExpandNode(aNode);
     Node answerNode = nodeList.ToArray()[2];
     Assert.AreEqual(answerNode.ToString(), "4 2 0 5 6 3 7 8 1 ");
 }
 protected abstract void Execute(Node puzzle);
 public void NinethMoveTest()
 {
     int[,] array = new int[3, 3] { { 4, 2, 3 }, { 0, 5, 6 }, { 7, 8, 1 } };
     Node aNode = new Node(array);
     List<Node> nodeList = ExpandedNodesFinder.ExpandNode(aNode);
     Node answerNode = nodeList.ToArray()[2];
     Assert.AreEqual(answerNode.ToString(), "0 2 3 4 5 6 7 8 1 ");
 }
 public void TwentyThirdMoveTest()
 {
     int[,] array = new int[3, 3] { { 7, 2, 3 }, { 4, 8, 6 }, { 5, 0, 1 } };
     Node aNode = new Node(array);
     List<Node> nodeList = ExpandedNodesFinder.ExpandNode(aNode);
     Node answerNode = nodeList.ToArray()[2];
     Assert.AreEqual(answerNode.ToString(), "7 2 3 4 0 6 5 8 1 ");
 }
 protected override void Execute(Node puzzle)
 {            
     throw new NotImplementedException();
 }