ericr2427's Profile User Rating: -----

Reputation: 38 Craftsman
Group:
Contributors
Active Posts:
373 (0.23 per day)
Joined:
01-December 08
Profile Views:
5,498
Last Active:
User is offline Jun 03 2012 11:35 AM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Linux
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Who Cares
Dream Kudos:
150

Latest Visitors

Icon   ericr2427 has not set their status

Posts I've Made

  1. In Topic: RIP Dick Clark

    Posted 18 Apr 2012

    My only memory of him is the Far Side comic with the caption, "Suddenly, on national television, Dick Clark ages 200 years in 30 seconds."
  2. In Topic: TextBox Text Field Isn't Updating

    Posted 27 Jun 2011

    I'd already written the rest of the code as a console version and I just sort of wrote the GUI on top of it by hand. Add that to my complete inexperience with C# (~2 days) and you get this kind of code I guess.
  3. In Topic: TextBox Text Field Isn't Updating

    Posted 27 Jun 2011

    Thanks a bunch, I figured the code was probably horribly wrong in some way :). Anyhow, it's working great now, I appreciate your help.
  4. In Topic: TextBox Text Field Isn't Updating

    Posted 27 Jun 2011

    Nope, still not showing up. I still have no idea why this isn't working.
  5. In Topic: TextBox Text Field Isn't Updating

    Posted 27 Jun 2011

    Here's the entire code.
    using System;
    using System.Drawing;
    using System.Collections;
    using System.Windows.Forms;
    class node
    {
        public node right;
        public node left;
        public node up;
        public node down;
        public node colHead;
        public int numNodes;
        public int nodeID;
    }
    class sudokuSolver : Form
    {
        static int[,,] puzzles = new int[3,9,9] {{
            {0,0,0,3,9,0,5,0,0},
            {8,2,7,0,0,0,0,0,9},
            {0,3,0,7,0,0,1,4,0},
            {1,0,2,5,7,0,0,0,3},
            {0,0,8,2,0,4,9,0,0},
            {7,0,0,0,6,1,4,0,5},
            {0,6,4,0,0,8,0,3,0},
            {9,0,0,0,0,0,8,5,6},
            {0,0,5,0,1,7,0,0,0}
        },{
            {7,9,0,0,0,0,3,0,0},
            {0,0,0,0,0,6,9,0,0},
            {8,0,0,0,3,0,0,7,6},
            {0,0,0,0,0,5,0,0,2},
            {0,0,5,4,1,8,7,0,0},
            {4,0,0,7,0,0,0,0,0},
            {6,1,0,0,9,0,0,0,8},
            {0,0,2,3,0,0,0,0,0},
            {0,0,9,0,0,0,0,5,4}
        },{
            {0,0,5,0,9,0,0,0,2},
            {0,0,0,0,0,4,0,0,7},
            {0,0,6,0,0,0,1,9,0},
            {0,0,2,0,4,8,0,0,0},
            {8,0,0,3,0,6,0,0,5},
            {0,0,0,1,2,0,3,0,0},
            {0,5,7,0,0,0,6,0,0},
            {9,0,0,7,0,0,0,0,0},
            {1,0,0,0,8,0,2,0,0}
        }};
        static int[,] puzzle = new int[9, 9];
        public static node root = new node();
        public static node[] headers = new node[324];
        public static node[] nodes;
        public static TextBox[,] fields = new TextBox[9,9];
        public static Button solveButton = new Button();
        public sudokuSolver()
        {
            this.Text = "Sudoku Solver";
            this.Width = 254;
            this.Height = 300;
            this.MinimumSize = new Size(254, 300);
            this.MaximumSize = new Size(254, 300);
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    fields[i,j] = new TextBox();
                }
            }
            int xPos = 8;
            int yPos = 8;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    fields[i, j].Height = 20;
                    fields[i, j].Width = 20;
                    fields[i, j].MaxLength = 1;
                    fields[i, j].Left = xPos;
                    fields[i, j].Top = yPos;
                    fields[i, j].Text = "";
                    xPos += 25;
                }
                xPos = 8;
                yPos += 25;
            }
            solveButton.Text = "Solve";
            solveButton.Width = 100;
            solveButton.Left = 75;
            solveButton.Top = 235;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Controls.Add(fields[i,j]);
                }
            }
            Controls.Add(solveButton);
            solveButton.Click += new System.EventHandler(solveButton_Click);
        }
        public static void Main()
        {
            sudokuSolver solver = new sudokuSolver();
            Application.Run(solver);
        }
        public static void solve()
        {
            addHeaders();
            constructMatrix();
            ArrayList path = new ArrayList();
            sudokuSolver solver = new sudokuSolver();
            solver.recursiveSolve(path);
        }
        public static void addHeaders()
        {
            for (int i = 0; i < 324; i++)
            {
                headers[i] = new node();
                if (i == 0)
                {
                    headers[i].left = root;
                    root.right = headers[i];
                }
                else
                {
                    headers[i - 1].right = headers[i];
                    headers[i].left = headers[i-1];
                }
                headers[i].up = headers[i];
                headers[i].down = headers[i];
                headers[i].numNodes = 0;
                headers[i].nodeID = i;
            }
            headers[323].right = root;
            root.left = headers[323];
        }
        public static void constructMatrix()
        {
            int numNodes = 0;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (puzzle[i,j] == 0)
                    {
                        numNodes += 9;
                    }
                    else
                    {
                        numNodes++;
                    }
                }
            }
            numNodes *= 4;
            nodes = new node[numNodes];
            for (int i = 0; i < numNodes; i++)
            {
                nodes[i] = new node();
            }
            int currentNode = 0, start, end;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (puzzle[i,j] == 0)
                    {
                        start = 1;
                        end = 9;
                    }
                    else
                    {
                        start = puzzle[i,j];
                        end = start;
                    }
                    for (int x = start; x <= end; x++)
                    {
                        int[] values = {j+(9*i), x+(9*i)+80, x+(9*j)+161, x+(9*boxNum(i,j))+242};
                        for (int c = 0; c < 4; c++, currentNode++)
                        {
                            node current = nodes[currentNode];
                            if (c != 0)
                            {
                                current.left = nodes[currentNode-1];
                            }
                            else
                            {
                                current.left = nodes[currentNode+3];
                            }
                            if (c != 3)
                            {
                                current.right = nodes[currentNode+1];
                            }
                            else
                            {
                                current.right = nodes[currentNode-3];
                            }
                            current.up = headers[values[c]].up;
                            headers[values[c]].up.down = current;
                            headers[values[c]].up = current;
                            current.down = headers[values[c]];
                            current.colHead = headers[values[c]];
                            current.colHead.numNodes++;
                            current.nodeID = currentNode;
                        }
                    }
                }
            }
        }
        public static int boxNum (int row, int col)
        {
            int add = 0;
            if (row/3 == 1)
                add = 3;
            if (row/3 == 2)
                add = 6;
            if (col/3 == 0)
                return add + 0;
            if (col/3 == 1)
                return add + 1;
            else
                return add + 2;
        }
        public bool recursiveSolve(ArrayList path)
        {
            if (root.right.Equals(root))
            {
                printSolution(path);
                return true;
            }
            int columnNo = chooseColumn();
            coverColumn(headers[columnNo]);
            for (node i = headers[columnNo].down; !i.Equals(headers[columnNo]); i = i.down)
            {
                ArrayList newPath = path;
                newPath.Add(i);
                for (node j = i.right; !j.Equals(i); j = j.right)
                {
                    coverColumn(j.colHead);
                }
                if (recursiveSolve(newPath)) {
                    return true;
                }
                for (node j = i.left; !j.Equals(i); j = j.left)
                {
                    uncoverColumn(j.colHead);
                }
            }
            uncoverColumn(headers[columnNo]);
            return false;
        }
        public static void printSolution(ArrayList path)
        {
            int[,] solution = new int[9,9];
            foreach (node i in path)
            {
                node start = i;
                while (start.left.nodeID < start.nodeID)
                {
                    start = start.left;
                }
                int rc = start.colHead.nodeID;
                int y = rc / 9;
                int x = rc - (y * 9);
                int rv = start.right.colHead.nodeID;
                int v = rv - (y * 9) - 80;
                solution[y,x] = v;
            }
            printPuzzle(solution);
        }
        public static int chooseColumn()
        {
            int lowest = 729;
            int lowestCol = 0;
            for (node i = root.right; !i.Equals(root); i = i.right)
            {
                if (i.numNodes < lowest)
                {
                    lowest = i.numNodes;
                    lowestCol = i.nodeID;
                }
            }
            return lowestCol;
        }
        public static void coverColumn (node header)
        {
            header.left.right = header.right;
            header.right.left = header.left;
            for (node i = header.down; !i.Equals(header); i = i.down)
            {
                for (node j = i.right; !j.Equals(i); j = j.right)
                {
                    j.down.up = j.up;
                    j.up.down = j.down;
                    j.colHead.numNodes--;
                }
            }
        }
        public static void uncoverColumn (node header)
        {
            for (node i = header.up; !i.Equals(header); i = i.up)
            {
                for (node j = i.left; !j.Equals(i); j = j.left)
                {
                    j.colHead.numNodes++;
                    j.down.up = j;
                    j.up.down = j;
                }
            }
            header.left.right = header.right;
            header.right.left = header.left;
        }
        public static void populateArray() {
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    //if (fields[i, j].Text == " " || fields[i, j].Text == "")
                    //{
                    //    puzzle[i, j] = 0;
                    //}
                    //else
                    //{
                    //    puzzle[i, j] = int.Parse(fields[i,j].Text);
                    //}
                    puzzle[i, j] = puzzles[0, i, j];
                }
            }
        }
        public static void printPuzzle(int[,] board)
        {
            for (int a = 0; a < 9; a++) {
                for (int b = 0; b < 9; b++) {
                    fields[a, b].Text = board[a, b].ToString();
                    Console.WriteLine(fields[a, b].Text);
                }
            }
        }
        private void solveButton_Click(object sender, System.EventArgs e)
        {
            populateArray();
            solve();
        }
    }
    
    

My Information

Member Title:
D.I.C Regular
Age:
17 years old
Birthday:
August 10, 1995
Gender:
Location:
Here
Years Programming:
3
Programming Languages:
HTML, C++, PHP, Ruby

Contact Information

E-mail:
Click here to e-mail me
Website URL:
Website URL  http://

Friends

Comments

ericr2427 has no profile comments yet. Why not say hello?