Ejemplo n.º 1
0
        /// <summary>
        /// Calls the FindLegalDigits method on the board
        /// </summary>
        /// <param name="board">Board to check</param>
        public static void FindLegalDigits(SudokuBoard board)
        {
            int row, col;

GetRow:
            Console.WriteLine("Enter the row that you want to check (1-9)");
            int userInput;

            if (!int.TryParse(Console.ReadLine(), out userInput) || userInput < 1 || userInput > 9)
            {
                Console.WriteLine("You have entered an incorrect input. Please try again.");

                goto GetRow;
            }
            row = userInput;

GetCol:
            Console.WriteLine("Enter the col that you want to place (1-9)");
            if (!int.TryParse(Console.ReadLine(), out userInput) || userInput < 1 || userInput > 9)
            {
                Console.WriteLine("You have entered an incorrect input. Please try again.");

                goto GetCol;
            }
            col = userInput;

            Console.WriteLine();
            foreach (int validVal in board.FindLegalDigits(row - 1, col - 1))
            {
                Console.WriteLine(validVal + " is a legal digit for row " + row + " and column " + col);
            }
            Console.WriteLine();
        }
        /// <summary>
        /// Algorithmically finds a solution (if there is one) to this sudoku puzzle.
        /// Make sure to assign the solved board back to the board argument
        /// </summary>
        /// <param name="board">The board to solve</param>
        /// <returns>True if the board was solved, false otherwise.</returns>
        public static bool SolveBoardIterativelyWithQueue(ref SudokuBoard board)
        {
            Queue <SudokuBoard> boards = new Queue <SudokuBoard>();

            boards.Push(board);
            List <int> nums = new List <int>();

            while (boards.Count != 0)
            {
                SudokuBoard temp = boards.Pop();

                if (temp.VerifyBoard() == true)
                {
                    board = temp;
                    return(true);
                }

                int  index1 = 0;
                int  index2 = 0;
                bool hasrun = false;

                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (temp.Board[i, j] == 0)
                        {
                            index1 = i;
                            index2 = j;
                            nums   = temp.FindLegalDigits(i, j);
                            hasrun = true;
                            if (hasrun == true)
                            {
                                break;
                            }
                        }
                    }
                }

                for (int i = 0; i < nums.Count; i++)
                {
                    SudokuBoard newBoard = new SudokuBoard(temp);
                    newBoard.Board[index1, index2] = nums[i];
                    boards.Push(newBoard);
                }
            }

            return(false);
            //As long as there is a board in the queue, do the following:
            //dequeue from the queue and store the returned value
            //if the returned value is complete
            //apply board to our ref parameter and return true
            //Find the first blank space "0" on the board
            //FindLegalDigits() on that space
            //Enqueue a new board for each legal digit found(make sure to put that digit on the new board!)
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Algorithmically finds a solution (if there is one) to this sudoku puzzle.
        /// Make sure to assign the solved board back to the board argument
        /// </summary>
        /// <param name="board">The board to solve</param>
        /// <returns>True if the board was solved, false otherwise.</returns>
        public static bool SolveBoardIterativelyWithQueue(ref SudokuBoard board)
        {
            Queue <SudokuBoard> boards   = new Queue <SudokuBoard>();
            SudokuBoard         fatdaddy = new SudokuBoard();

            boards.Enqueue(board);



            //As long as there is a board in the queue, do the following:
            while (boards.Count > 0)
            {
                //dequeue from the queue and store the returned value

                fatdaddy = boards.Dequeue();
                //if the returned value is complete
                if (fatdaddy.VerifyBoard())
                {
                    //apply board to our ref parameter and return true
                    board = fatdaddy;
                    return(true);
                }
                bool isgood = false;
                //Find the first blank space "0" on the board
                for (int x = 0; x < 9; x++)
                {
                    for (int y = 0; y < 9; y++)
                    {
                        if (fatdaddy.Board[x, y] == 0)
                        {
                            //FindLegalDigits() on that space
                            List <int> legal_digits = fatdaddy.FindLegalDigits(x, y);

                            //Enqueue a new board for each legal digit found (make sure to put that digit on the new board!)
                            if (isgood == false)
                            {
                                for (int i = 0; i < legal_digits.Count; i++)
                                {
                                    SudokuBoard copy = new SudokuBoard(fatdaddy);
                                    copy.Board[x, y] = legal_digits[i];

                                    boards.Enqueue(copy);
                                }
                                isgood = true;
                            }
                        }
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Algorithmically finds a solution (if there is one) to this sudoku puzzle.
        /// Make sure to assign the solved board back to the board argument
        /// </summary>
        /// <param name="board">The board to solve</param>
        /// <returns>True if the board was solved, false otherwise.</returns>
        public static bool SolveBoardIterativelyWithQueue(ref SudokuBoard board)
        {
            // throw new NotImplementedException();

            Queue <SudokuBoard> boards = new Queue <SudokuBoard>();
            SudokuBoard         one    = new SudokuBoard();

            boards.Enqueue(board);

            while (boards.Count > 0)
            {
                one = boards.Dequeue();
                if (one.VerifyBoard())
                {
                    board = one;
                    return(true);
                }

                bool wee = false;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (one.Board[i, j] == 0)
                        {
                            List <int> digits = one.FindLegalDigits(i, j);
                            if (wee == false)
                            {
                                for (int x = 0; x < digits.Count; x++)
                                {
                                    SudokuBoard two = new SudokuBoard(one);
                                    two.Board[i, j] = digits[x];

                                    boards.Enqueue(two);
                                }
                                wee = true;
                            }
                        }
                    }
                }
            }

            //As long as there is a board in the queue, do the following:
            //dequeue from the queue and store the returned value
            //if the returned value is complete
            //apply board to our ref parameter and return true
            //Find the first blank space "0" on the board
            //FindLegalDigits() on that space
            //Enqueue a new board for each legal digit found (make sure to put that digit on the new board!)
            return(false);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Algorithmically finds a solution (if there is one) to this sudoku puzzle.
        /// Make sure to assign the solved board back to the board argument
        /// </summary>
        /// <param name="board">The board to solve</param>
        /// <returns>True if the board was solved, false otherwise.</returns>
        public static bool SolveBoardIterativelyWithQueue(ref SudokuBoard board)
        {
            Stack <SudokuBoard> boards = new Stack <SudokuBoard>();

            boards.Push(board);

            while (boards.Count != 0)
            {
                SudokuBoard CurrentBoard = boards.Pop();
                if (CurrentBoard.VerifyBoard() == true)
                {
                    //Apply Board to our Ref Parameter and return true
                    board = CurrentBoard;
                    return(true);
                }

                bool HasRun = false;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (HasRun != true && CurrentBoard.Board[i, j] == 0)
                        {
                            List <int> LegalBoard = CurrentBoard.FindLegalDigits(i, j);
                            foreach (int S in LegalBoard)
                            {
                                SudokuBoard B = new SudokuBoard(CurrentBoard);
                                B.Board[i, j] = S;
                                boards.Push(B);
                            }
                            HasRun = true;
                        }
                    }
                }
            }
            return(false);

            ////apply board to our ref parameter and return true
            ////Find the first blank space "0" on the board
            ////FindLegalDigits() on that space
            ////Enqueue a new board for each legal digit found (make sure to put that digit on the new board!)
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Algorithmically finds a solution (if there is one) to this sudoku puzzle.
        /// Make sure to assign the solved board back to the board argument
        /// </summary>
        /// <param name="board">The board to solve</param>
        /// <returns>True if the board was solved, false otherwise.</returns>

        public static bool SolveBoardIterativelyWithQueue(ref SudokuBoard board)
        {
            Stack <SudokuBoard> boards = new Stack <SudokuBoard>();

            boards.Push(board);


            while (boards.Count != 0)
            {
                SudokuBoard currentboard = boards.Pop();
                if (currentboard.VerifyBoard() == true)
                {
                    //apply board to our ref parameter
                    board = currentboard;
                    //and return true
                    return(true);
                }
                bool hasrun = false;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (hasrun != true && currentboard.Board[i, j] == 0)
                        {
                            List <int> legalboard = currentboard.FindLegalDigits(i, j);
                            foreach (int s in legalboard)
                            {
                                SudokuBoard b = new SudokuBoard(currentboard);
                                b.Board[i, j] = s;
                                boards.Push(b);
                            }
                            hasrun = true;
                        }
                    }
                }
            }

            return(false);
        }