예제 #1
0
        public ThreadContext solveSudoku(int[,] sudoku, int threadID)
        {
            tableStack = new ArrayList();

            Console.WriteLine("Thread #" + threadID + " is entering");

            this.sudoku   = sudoku;
            ctx           = new ThreadContext(sudoku);
            this.threadID = threadID;

            Stopwatch sw = new Stopwatch();

            sw.Start();
            tableStack.Add(new SudokuStatus(sudoku, -1, -1));

            while (calculateLastDigit())
            {
                populateCells();
            }

            if (isDone())
            {
                ctx.sudoku   = sudoku;
                ctx.time     = sw.Elapsed;
                ctx.threadID = threadID;
                return(ctx);
            }

            if (solve())
            {
                ctx.sudoku = ((SudokuStatus)tableStack[tableStack.Count - 1]).getTable();
            }
            sw.Stop();

            ctx.setTableStack(tableStack);
            ctx.sudoku   = ((SudokuStatus)tableStack[tableStack.Count - 1]).getTable();
            ctx.time     = sw.Elapsed;
            ctx.threadID = threadID;

            Console.WriteLine("Thread #" + threadID + " is exiting");
            return(ctx);
        }
예제 #2
0
        public Threads(int[,] sudoku, int THREAD_COUNT)
        {
            this.sudoku       = sudoku;
            this.THREAD_COUNT = THREAD_COUNT;
            threads           = new Thread[THREAD_COUNT];
            tasks             = new Sudoku[THREAD_COUNT];
            objects           = new Sudoku[THREAD_COUNT];
            ctxs = new ThreadContext[THREAD_COUNT];
            Sudoku sudokuSolver = new Sudoku();

            for (int i = 0; i < THREAD_COUNT; i++)
            {
                int copy = i;
                objects[copy] = new Sudoku();
                ctxs[i]       = new ThreadContext(sudoku);
                threads[i]    = new Thread(() => { ctxs[copy] = objects[copy].solveSudoku((int[, ])sudoku.Clone(), copy); });
            }

            for (int i = 0; i < THREAD_COUNT; i++)
            {
                threads[i].Start();
            }
        }
예제 #3
0
        public Form1(int[,] sudoku, int SUDOKU_SIZE, int THREAD_COUNT)
        {
            InitializeComponent();
            panel1.Location   = new Point(this.Width * 5 / 100, this.Height * 5 / 100);
            this.SUDOKU_SIZE  = SUDOKU_SIZE;
            this.sudoku       = sudoku;
            this.THREAD_COUNT = THREAD_COUNT;
            ctxs = new ThreadContext[THREAD_COUNT];
            for (int i = 0; i < THREAD_COUNT; i++)
            {
                ctxs[i]        = new ThreadContext();
                ctxs[i].sudoku = this.sudoku;
            }
            label1.Visible = false;
            label8.Visible = false;

            helper = new Helper();

            isRed = new int[9, 9];

            isRed                = helper.getColor(sudoku);
            this.Text            = "Sudoku Solver";
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
        }