Esempio n. 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            NineSquareElement[] elements = new NineSquareElement[9];

            for (int i = 0; i < 9; i++)
            {
                int left = 0, right = 0, top = 0, bottom = 0;

                var textBox = FindControl("textBox" + (i + 1) + "l") as TextBox;
                int.TryParse(textBox.Text.Trim(), out left);
                textBox = FindControl("textBox" + (i + 1) + "t") as TextBox;
                int.TryParse(textBox.Text.Trim(), out top);
                textBox = FindControl("textBox" + (i + 1) + "r") as TextBox;
                int.TryParse(textBox.Text.Trim(), out right);
                textBox = FindControl("textBox" + (i + 1) + "b") as TextBox;
                int.TryParse(textBox.Text.Trim(), out bottom);

                NineSquareElement element = new NineSquareElement(left, top, right, bottom);
                elements[i] = element;
            }

            NineSquarePuzzle puzzle = new NineSquarePuzzle(elements);
            if (!NineSquarePuzzle.CheckSolvable(elements))
                MessageBox.Show("Invalid Puzzle..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            if (puzzle.Solve())
            {
                for (int i = 0; i < 9; i++)
                {
                    int left = 0, right = 0, top = 0, bottom = 0;

                    var textBox = FindControl("textBox" + (i + 1) + "l") as TextBox;
                    textBox.Text = puzzle.SolvedNineSquareElements[i].EdgeLeftWeight.ToString();
                    textBox = FindControl("textBox" + (i + 1) + "t") as TextBox;
                    textBox.Text = puzzle.SolvedNineSquareElements[i].EdgeTopWeight.ToString();
                    textBox = FindControl("textBox" + (i + 1) + "r") as TextBox;
                    textBox.Text = puzzle.SolvedNineSquareElements[i].EdgeRightWeight.ToString();
                    textBox = FindControl("textBox" + (i + 1) + "b") as TextBox;
                    textBox.Text = puzzle.SolvedNineSquareElements[i].EdgeBottomWeight.ToString();
                }
            }
        }
Esempio n. 2
0
        public bool BT(NineSquareElement start, ref NineSquareElement[] nineSquareElements)
        {
            int rotateCount = 0;
            NineSquareElement[] oldElements = null;
            // used here to clone current elements into oldElements
            Revert(ref nineSquareElements, ref oldElements);

            if (VerifySolved(nineSquareElements))
                return true;

            ++nestedCount;
            while (start != null)
            {
                var elements = GetElementsNear(ref start, ref nineSquareElements);

                if (rotateCount <= 3)
                {
                    if (VerifySolved(nineSquareElements))
                    {
                        --nestedCount;
                        return true;
                    }
                    else if (BT(start.ForwardElement, ref nineSquareElements))
                    {
                        --nestedCount;
                        // verify this particular node.

                        return true;
                    }

                    else if (rotateCount <= 3)
                    {
                        // Check & Rotate
                        StepsToSolve.Enqueue(new StepNode((NineSquareElement) start.Clone(),
                            IndexOf(start, nineSquareElements), true));

                        NineSquarePuzzle puzzle = new NineSquarePuzzle(nineSquareElements);
                        VisualPuzzleSolve.Enqueue(puzzle);

                        start.RotateCW();
                        rotateCount++;
                    }
                    else if (rotateCount > 3 && start != nineSquareElements[0])
                    {
                        // backtrack
                        //Console.WriteLine("BT...");
                        StepsToSolve.Enqueue(new StepNode((NineSquareElement) start.Clone(),
                            IndexOf(start, nineSquareElements), false, true));

                        Revert(ref oldElements, ref nineSquareElements);

                        NineSquarePuzzle puzzle = new NineSquarePuzzle(nineSquareElements);
                        VisualPuzzleSolve.Enqueue(puzzle);
                    }
                    else
                    {
                        --nestedCount;
                        return false;
                    }
                }
                else
                {
                    --nestedCount;
                    return false;
                }
            }
            --nestedCount;
            return false;
        }