Example #1
0
        public void Test_SolveBoard_Solved()
        {
            int[,] array = new int[9, 9]
            {
                { 0, 0, 9, 0, 0, 0, 0, 0, 0, },
                { 3, 0, 4, 0, 8, 1, 0, 2, 0, },
                { 0, 8, 6, 5, 0, 0, 0, 0, 0, },
                { 9, 0, 1, 3, 0, 0, 0, 0, 2, },
                { 0, 0, 0, 0, 4, 0, 0, 0, 0, },
                { 6, 0, 0, 0, 0, 2, 4, 0, 1 },
                { 0, 0, 0, 0, 0, 3, 2, 6, 0 },
                { 0, 3, 0, 4, 1, 0, 5, 0, 8 },
                { 0, 0, 0, 0, 0, 0, 1, 0, 0, },
            };

            SudokoBoard   board  = SudokoBoard.CreateBoard(array);
            ISudokoSolver solver = new SudokoSolver(board);

            solver.SolveBoard();
            board = solver.GetBoard();

            bool isSolved = SudokuHelper.IsBoardSolved(board);

            Assert.IsTrue(isSolved);
        }
Example #2
0
        public void Test_GetBoxValues()
        {
            int[,] array = new int[9, 9]
            {
                { 0, 0, 9, 0, 0, 0, 0, 0, 0, },
                { 3, 0, 4, 0, 8, 1, 0, 2, 0, },
                { 0, 8, 6, 5, 0, 0, 0, 0, 0, },
                { 9, 0, 1, 3, 0, 0, 0, 0, 2, },
                { 0, 0, 0, 0, 4, 0, 0, 0, 0, },
                { 6, 0, 0, 0, 0, 2, 4, 0, 1 },
                { 0, 0, 0, 0, 0, 3, 2, 6, 0 },
                { 0, 3, 0, 4, 1, 0, 5, 0, 8 },
                { 0, 0, 0, 0, 0, 0, 1, 0, 0, },
            };

            SudokoBoard board = SudokoBoard.CreateBoard(array);

            // 3   0    0
            // 0    4   4
            // 0    0   2

            List <int> box5         = board.GetBoxValues(3, 3);
            List <int> box5Expected = new List <int> {
                3, 0, 0, 0, 4, 0, 0, 0, 2
            };

            var differnce = box5.Except(box5Expected).ToList();

            Assert.IsTrue(differnce.Count == 0);
        }
Example #3
0
        public static void PrintColumns(SudokoBoard pBoard)
        {
            foreach (List <int> t in pBoard.Columns)
            {
                foreach (var x in t)
                {
                    Console.Write(x + "  ");
                }
                Console.WriteLine();
            }

            Console.WriteLine("******* ** ******* ** ******* ** *******");
            Console.WriteLine();
        }
Example #4
0
 public static void PrintPossibleValues(SudokoBoard pBoard, bool showCellValues)
 {
     for (int i = 0; i < 9; i++)
     {
         for (int j = 0; j < 9; j++)
         {
             if (pBoard.SudoArray[i, j].CellValue == 0)
             {
                 Console.Write(pBoard.SudoArray[i, j].PossibleValues.ToCSV() + "\t  ");
             }
             else
             {
                 Console.Write(showCellValues ? (pBoard.SudoArray[i, j].CellValue + "\t  ") : "\t  ");
             }
         }
         Console.WriteLine();
         Console.WriteLine();
     }
 }
Example #5
0
        public void Test_IsBoardValid_Invalid()
        {
            int[,] array = new int[9, 9]
            {
                { 5, 1, 1, 2, 3, 7, 8, 4, 6 },
                { 5, 7, 4, 6, 8, 1, 9, 2, 5 },
                { 2, 8, 6, 5, 9, 4, 3, 1, 7 },
                { 9, 4, 1, 3, 6, 8, 7, 5, 2 },
                { 8, 2, 7, 1, 4, 5, 6, 3, 9 },
                { 6, 5, 3, 9, 7, 2, 4, 8, 1 },
                { 1, 9, 8, 7, 5, 3, 2, 6, 4 },
                { 7, 3, 2, 4, 1, 6, 5, 9, 8 },
                { 4, 6, 5, 8, 2, 9, 1, 7, 3 }
            };

            SudokoBoard board = SudokoBoard.CreateBoard(array);

            bool isSolved = SudokuHelper.IsBoardValid(board);

            Assert.IsFalse(isSolved);
        }
Example #6
0
        public static void PrintBoard(SudokoBoard pBoard)
        {
            // print board
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (pBoard.SudoArray[i, j].CellValue == 0)
                    {
                        Console.Write("_" + "  ");
                    }
                    else
                    {
                        Console.Write(pBoard.SudoArray[i, j].CellValue + "  ");
                    }
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            Console.WriteLine();
        }
Example #7
0
        static void Main(string[] args)
        {
            //int[,] array = new int[9, 9]
            //{
            //    {6, 5, 0, 0, 0, 0, 0, 9, 7},
            //    {0, 0, 4, 0, 2, 0, 0, 0, 0},
            //    {0, 0, 0, 6, 5, 7, 4, 0, 0},
            //    {0, 9, 0, 8, 0, 0, 0, 2, 3},
            //    {0, 0, 6, 3, 0, 9, 1, 0, 0},
            //    {3, 1, 0, 0, 0, 2, 0, 7, 0},
            //    {0, 0, 3, 2, 8, 4, 0, 0, 0},
            //    {0, 0, 0, 0, 9, 0, 8, 0, 0},
            //    {9, 8, 0, 0, 0, 0, 0, 4, 1},
            //};

            int[,] array = new int[9, 9]
            {
                { 0, 0, 9, 0, 0, 0, 0, 0, 0, },
                { 3, 0, 4, 0, 8, 1, 0, 2, 0, },
                { 0, 8, 6, 5, 0, 0, 0, 0, 0, },
                { 9, 0, 1, 3, 0, 0, 0, 0, 2, },
                { 0, 0, 0, 0, 4, 0, 0, 0, 0, },
                { 6, 0, 0, 0, 0, 2, 4, 0, 1 },
                { 0, 0, 0, 0, 0, 3, 2, 6, 0 },
                { 0, 3, 0, 4, 1, 0, 5, 0, 8 },
                { 0, 0, 0, 0, 0, 0, 1, 0, 0, },
            };

            SudokoBoard board = SudokoBoard.CreateBoard(array);

            Console.WriteLine(" ************ Before solution ************ ");
            Console.WriteLine();
            PrintBoard(board);
            //PrintRows(board);
            //PrintColumns(board);

            if (SudokuHelper.IsBoardValid(board))
            {
                ISudokoSolver solver = new SudokoSolver(board);
                solver.SolveBoard();

                board = solver.GetBoard();

                Console.WriteLine();

                Console.WriteLine(" ************ After solution ************ ");
                Console.WriteLine();
                PrintBoard(board);

                //board.PrintPossibleValues(false);

                Console.WriteLine();
            }
            else
            {
                //throw new InvalidOperationException("The sudoku board has some invalid cells");
                Console.WriteLine("Error: The sudoku board has some invalid cells");
            }


            List <int> lstA = new List <int> {
                1, 2, 3, 4,
            };
            List <int> lstB = new List <int> {
                4, 3, 2, 1,
            };


            var dups     = lstA.Intersect(lstB).ToList();
            var distinct = lstA.Except(lstB).ToList();

            Console.Read();
        }