コード例 #1
0
ファイル: Solver.cs プロジェクト: MappyTheKat/sudoku
        //create candidates for multithreading.
        private List<Board> createCandidates()
        {
            List<Board> candidates = new List<Board>();

            for(int i = 0; i < gridSize; i++)
            {
                for(int j = 0; j < gridSize; j++)
                {
                    if(originalBoard.getBoard(i, j) == 0)
                    {
                        for(int k = 1; k <= gridSize; k++)
                        {
                            Board candy = originalBoard.Copy();
                            candy.setBoard(i, j, k);
                            if (candy.isValid())
                            {
                                candidates.Add(candy);
                            }
                        }
                        i = gridSize;
                        break;
                    }
                }
            }

            return candidates;
        }
コード例 #2
0
ファイル: SudokuForm.cs プロジェクト: zfme/Sudoku
        private BoardCozum CozVeOrtalamaDondur(Board board, ISolver solver, int denemeSayisi, string metrik, bool ilkiniAl = true)
        {
            long         total          = 0;
            int          deneme         = 0;
            List <Board> SolutionBoards = new List <Board>();
            string       detail         = "";

            for (int i = 0; i < denemeSayisi; i++)
            {
                Board     copy      = board.Copy();
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                Board aSolutionBoard = solver.Solve(copy);
                SolutionBoards.Add(aSolutionBoard);
                stopwatch.Stop();
                solver.Temizle();
                long gecen = metrik.Equals("ms") ? stopwatch.ElapsedMilliseconds : stopwatch.ElapsedTicks;
                detail += gecen + ",";
                if (!ilkiniAl && i == 0)
                {
                    continue;
                }
                deneme++;
                total += gecen;
            }
            double avg = ((double)total) / deneme;

            return(new BoardCozum
            {
                Total = total,
                Detail = detail.Remove(detail.Length - 1),
                CozumBoards = SolutionBoards,
                Sure = avg
            });
        }
コード例 #3
0
ファイル: Solver.cs プロジェクト: JurjenBiewenga/SudokuSolver
        /// <summary>
        /// Tries to solve the board
        /// </summary>
        /// <returns>A new instance of Board if solved otherwise null</returns>
        public Board Solve()
        {
            List <int>[,] possibleValues = FindValidValues(_currentBoard);

            Board copiedBoard = _currentBoard.Copy();

            _currentBoard = copiedBoard;
            if (Solve(possibleValues))
            {
                return(copiedBoard);
            }

            return(null);
        }
コード例 #4
0
ファイル: Generator.cs プロジェクト: MappyTheKat/sudoku
        public Generator(int size)
        {
            string init = "";

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    init += "0 ";
                }
            }
            initBoard = new Sudoku.Board(init);
            newBoard  = initBoard.Copy();
            gridSize  = size;
        }
コード例 #5
0
ファイル: Solver.cs プロジェクト: MappyTheKat/sudoku
 public Solver(Board b)
 {
     originalBoard = b.Copy();
     solution = b.Copy();
     gridSize = originalBoard.gridSize;
 }
コード例 #6
0
ファイル: Generator.cs プロジェクト: MappyTheKat/sudoku
        public string generateString(int holeNumber)
        // generate n boards...
        {
            int NUM_MAX = gridSize * 3 - 6; // number of random generated numbers

            status = 0;
            List <int> selected = new List <int>();
            Random     r        = new Random();
            bool       solved   = false;

            //lasvegas algorithms.
            do // while bm has a solution
            {
                sv = null;
                do // while newBoard got valid puzzle
                {
                    int numberSelect = NUM_MAX;
                    selected.Clear();
                    newBoard = initBoard.Copy();
                    while (numberSelect > 0)
                    {
                        int inputPosition = r.Next(1, gridSize * gridSize);
                        if (!selected.Contains(inputPosition))
                        {
                            selected.Add(inputPosition);
                            newBoard.setBoard(inputPosition / gridSize, inputPosition % gridSize, r.Next(1, gridSize));
                            numberSelect--;
                        }
                    }
                }while (!newBoard.isValid());
                sv = new Solver(newBoard);
                sv.PresentBoard += PresentBoard;
                bool DONE = false;
                var  t0   = new System.Threading.Thread(() => {
                    threeseconds();
                    if (DONE == false)
                    {
                        sv.killSolver();
                        DONE = true;
                    }
                });
                var t1 = new System.Threading.Thread(() =>
                {
                    solved = sv.solve(2, true);
                    Console.WriteLine("solved in three seconds...");
                    DONE = true;
                });
                t0.Start();
                t1.Start();
                while (!DONE)
                {
                    System.Threading.Thread.Sleep(200);
                }
                t0.Abort();
                t1.Abort();
            }while (!solved);

            Console.WriteLine("digging hole initiated");
            status = 1; // DO NOT PRESENT WHILE THIS REGION

            Board Digged = sv.solution.Copy();

            bool[,] DONOTDIG = new bool[gridSize, gridSize];
            //holeNumber = r.Next(54, 73); // 9x9 Normal difficulty (temporal), we should implement difficulty later
            sv = null;
            int[] shuffledArr = new int[gridSize * gridSize];

            for (int i = 0; i < gridSize * gridSize; i++)
            {
                DONOTDIG[i / gridSize, i % gridSize] = false;
                shuffledArr[i] = i;
            }

            // shuffling size array...
            int n = shuffledArr.Length;

            while (n > 1)
            {
                int k    = r.Next(n--);
                int temp = shuffledArr[n];
                shuffledArr[n] = shuffledArr[k];
                shuffledArr[k] = temp;
            }

            //lets dig holes!
            int cnt = gridSize * gridSize - 1;

            while (holeNumber > 0)
            {
                if (cnt == 0)
                {
                    break;
                }
                int digRow       = shuffledArr[cnt] / gridSize;
                int digCol       = shuffledArr[cnt--] % gridSize;
                int diggedNumber = Digged.getBoard(digRow, digCol);

                if (DONOTDIG[digRow, digCol])
                {
                    continue;
                }

                bool nope = false;
                holeNumber--;
                for (int i = 1; i <= gridSize; i++)
                {
                    if (i == diggedNumber)
                    {
                        continue;
                    }
                    Digged.setBoard(digRow, digCol, i);
                    sv = new Solver(Digged);
                    if (sv.solve(2, true))
                    {
                        DONOTDIG[digRow, digCol] = true;
                        Digged.setBoard(digRow, digCol, diggedNumber);
                        nope = true;
                        holeNumber++;
                        break;
                    }
                }
                if (!nope)
                {
                    DONOTDIG[digRow, digCol] = true;
                    //Console.WriteLine("digging #{0}: {1}, {2}", holeNumber, digRow, digCol);
                    Digged.setBoard(digRow, digCol, 0);
                    newBoard = Digged.Copy();
                }
            }
            SolBoard = Digged.Copy();
            Console.WriteLine("generating complete");
            Console.WriteLine(Digged.ToString());
            PresentBoard(this, new PresentBoardArgs(Digged.ToString()));
            return(Digged.ToString());
        }