Example #1
0
        private State swapSquares(State st, Square activeSquare, Square zeroBlock)
        {
            String tempContent = activeSquare.val;
            int[] tempCoord = { activeSquare.X, activeSquare.Y };

            Square temp = st.state[activeSquare.X][activeSquare.Y];
            st.state[activeSquare.X][activeSquare.Y] = st.state[zeroBlock.X][zeroBlock.Y];
            st.state[zeroBlock.X][zeroBlock.Y] = temp;

            activeSquare.X = zeroBlock.X;
            activeSquare.Y = zeroBlock.Y;

            zeroBlock.X = tempCoord[0];
            zeroBlock.Y = tempCoord[1];

            return st;
        }
        private void swapSquares(Square activeSquare, Square zeroBlock)
        {
            String tempContent = activeSquare.val;
            int[] tempCoord = { activeSquare.X, activeSquare.Y };

            Square temp = currentNode.state.state[activeSquare.X][activeSquare.Y];
            currentNode.state.state[activeSquare.X][activeSquare.Y] = currentNode.state.state[zeroBlock.X][zeroBlock.Y];
            currentNode.state.state[zeroBlock.X][zeroBlock.Y] = temp;

            activeSquare.X = zeroBlock.X;
            activeSquare.Y = zeroBlock.Y;

            zeroBlock.X = tempCoord[0];
            zeroBlock.Y = tempCoord[1];

            currentNode.hValue = Heuristics.manhattanDistanceHeuristicPlusReversalPenalty(currentNode);
            currentNode.setAsRootNode();
        }
        private void initializePuzzleState()
        {
            List<List<Square>> values = new List<List<Square>>();
            int count = 0;
            for(int i = 0; i < 3; i++)
            {
                List<Square> temp = new List<Square>();
                for (int j = 0; j < 3; j++)
                {
                    if (count > 7)
                    {
                        Square tempSquare = new Square(i, j, (0).ToString());
                        temp.Add(tempSquare);
                    }
                    else
                    {
                        Square tempSquare = new Square(i, j, (count + 1).ToString());
                        temp.Add(tempSquare);
                        ((Button)blockContainer.Children[count++]).DataContext = tempSquare;
                    }

                }
                values.Add(temp);
            }
            currentNode = new Node(new State(values, ""));
            initializeStats();
        }
 private void moveIt(Square activeSquare)
 {
     Square zeroBlock = getBlankPosition();
     if (canBeMoved(activeSquare, zeroBlock))
     {
         swapSquares(activeSquare, zeroBlock);
         updateStats();
     }
 }
 private Boolean canBeMoved(Square activeSquare, Square zeroBlock)
 {
     if ((activeSquare.X == zeroBlock.X && ((Math.Abs(activeSquare.Y - zeroBlock.Y)) == 1))
         || (activeSquare.Y == zeroBlock.Y && ((Math.Abs(activeSquare.X - zeroBlock.X)) == 1)))
     {
         return true;
     }
     return false;
 }