ToString() public méthode

public ToString ( ) : string
Résultat string
Exemple #1
0
 public void killSolver()
 {
     foreach(Thread thread in threads)
     {
         thread.Abort();
     }
     PresentBoard?.Invoke(this, new PresentBoardArgs(solution.ToString()));
     SolveEnded?.Invoke(this, new SolveEndedArgs(solution.isComplete(), "중단되었습니다."));
 }
Exemple #2
0
 void SolveBacktrack()
 {
     //Console.WriteLine("backtraking initiated.");
     bm = new BacktrackingModule(originalBoard);
     var solved = bm.solve();
     string message = string.Empty;
     if (solved)
     {
         solution = bm.GetSolution().Copy();
         PresentBoard?.Invoke(this, new PresentBoardArgs(solution.ToString()));
     }
     //Console.WriteLine(solution.ToString());
     //Console.WriteLine("valid : " + solution.isValid());
     //Console.WriteLine("complete : " + solution.isComplete());
     return;
 }
Exemple #3
0
 //solve backtrack for specific board.
 bool solveBacktrack(Board b)
 {
     //Console.WriteLine("multithreaded-backtraking initiated.");
     BacktrackingModule BM = new BacktrackingModule(b);
     if (bm == null)
         bm = BM;
     var solved = BM.solve();
     string message = string.Empty;
     if (solved)
     {
         bm = BM;
         solution = BM.GetSolution().Copy();
         PresentBoard?.Invoke(this, new PresentBoardArgs(solution.ToString()));
     }
     //Console.WriteLine("valid : " + solution.isValid());
     //Console.WriteLine("complete : " + solution.isComplete());
     return solved;
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Reading file.");
            string absolutePath = Path.Combine(GetApplicationDirectory(), SudokuFilePath);

            if (File.Exists(absolutePath))
            {
                string fileContents = File.ReadAllText(absolutePath);
                Console.WriteLine("Generating board.");

                Board sudokuBoard = GetBoardFromString(fileContents);

                Console.WriteLine("Solving the board.");

                Stopwatch stopwatch   = Stopwatch.StartNew();
                Board     solvedBoard = sudokuBoard.Solve();

                stopwatch.Stop();

                if (solvedBoard != null)
                {
                    Console.WriteLine($"Solving succesfull, it took {stopwatch.ElapsedMilliseconds} ms.");
                    Console.Write(solvedBoard.ToString());
                }
                else
                {
                    Console.WriteLine($"Solving failed, it took {stopwatch.ElapsedMilliseconds} ms.");
                }
            }
            else
            {
                Console.WriteLine($"Failed to read file at path \"{absolutePath}\".");
            }

            Console.ReadKey();
        }
Exemple #5
0
        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());
        }