예제 #1
0
        public void Test54SeatsError()
        {
            string source = @"7 9 5 1 4 2 3 6 8
1 2 4 8 6 3 7 9 5
8 3 6 7 _ _ 2 1 4
_ 4 1 _ _ _ 5 8 7
_ 5 9 _ _ 8 6 4 1
_ 8 _ _ _ _ 9 2 3
9 _ 8 5 _ _ 4 3 6
4 _ _ _ _ _ 8 5 2
5 _ _ _ 8 _ 1 7 9";

            Definition.Sudoku sudoku;
            Assert.IsTrue(SudokuHelper.TryParse(source, out sudoku));
            Assert.IsTrue(sudoku.Validate(), "Invalid sudoku");

            var sourceSudoku  = sudoku.Copy();
            var guiController = new GUI.SudokuPlayerController(sourceSudoku, sudoku);

            using (var solver = new Core.SudokuSolver(sudoku))
            {
                Assert.IsTrue(solver.TrySolve());
            }

            guiController.ShowAndWaitForClose();
        }
예제 #2
0
        public void solve_puzzle_test_true(string puzzle, string test_message)
        {
            SudokuGrid result = SudokuHelper.solve_puzzle(puzzle);

            Assert.True(result.is_grid_solved(), $"{test_message} grid not solved. {System.Environment.NewLine}{result.ToStringFormatted()}");
            Assert.True(result.is_grid_valid(), $"{test_message} grid is invalid. {System.Environment.NewLine}{result.ToStringFormatted()}");
        }
예제 #3
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);
        }
예제 #4
0
        public void get_values_for_coordinates_test(string puzzle, CoordinateList coords, List <int> expected_values, string message)
        {
            SudokuGrid grid = new SudokuGrid(puzzle);

            //List<int> values_found = SudokuGrid.get_values_for_coordinates(coords, grid._grid_cells);

            //var firstNotSecond = values_found.Except(expected_values).ToList();
            //var secondNotFirst = expected_values.Except(values_found).ToList();

            Assert.True(SudokuHelper.are_int_lists_equal(SudokuGrid.get_values_for_coordinates(coords, grid._grid_cells), expected_values), message);
        }
예제 #5
0
        public void get_possible_values_for_cell_test(string puzzle, int x, int y, List <int> expected_values, string message)
        {
            SudokuGrid grid = new SudokuGrid(puzzle);

            //List<int> values_found = SudokuGrid.calculate_possible_values_for_cell(x, y, grid._grid_cells);

            //var firstNotSecond = values_found.Except(expected_values).ToList();
            //var secondNotFirst = expected_values.Except(values_found).ToList();

            //Assert.True(values_found.Count == expected_values.Count && !firstNotSecond.Any() && !secondNotFirst.Any(), message);
            Assert.True(SudokuHelper.are_int_lists_equal(SudokuGrid.calculate_possible_values_for_cell(x, y, grid._grid_cells), expected_values), message);
        }
예제 #6
0
        public void set_possible_values_of_all_cells_test(string puzzle, int x, int y, List <int> expected_values, string message)
        {
            SudokuGrid grid = new SudokuGrid(puzzle);

            grid.set_possible_values_of_all_cells();

            //List<int> target_possibles = grid._grid_cells[x, y]._possible_values;

            //var firstNotSecond = target_possibles.Except(expected_values).ToList();
            //var secondNotFirst = expected_values.Except(target_possibles).ToList();

            //Assert.True(target_possibles.Count == expected_values.Count && !firstNotSecond.Any() && !secondNotFirst.Any(), message);
            Assert.True(SudokuHelper.are_int_lists_equal(grid._grid_cells[x, y]._possible_values, expected_values), message);
        }
예제 #7
0
        public void find_pointing_pair_test()
        {
            SudokuGrid grid = new SudokuGrid("306000000|000070000|405000000||000000000|000000000|000000000||000000000|000000000|000000000");

            grid.set_possible_values_of_all_cells();
            HashSet <(int, int, int)> results          = SudokuHelper.find_pointing_pairs(grid._grid_cells);
            HashSet <(int, int, int)> expected_results = new HashSet <(int, int, int)> {
                (1, 3, 7), (1, 4, 7), (1, 5, 7), (1, 6, 7), (1, 7, 7), (1, 8, 7)
            };

            int a_not_b = results.Except(expected_results).Count();
            int b_not_a = expected_results.Except(results).Count();

            Assert.True(results.Count == expected_results.Count && a_not_b == 0 && b_not_a == 0);
        }
예제 #8
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            // The model properties are already checked. We can cast without any problem "value" param.
            var sudokuGrid = (SudokuGrid)value;
            var helper     = new SudokuHelper(sudokuGrid.Values);

            if (helper.ValidateGridValues())
            {
                return(ValidationResult.Success);
            }
            else
            {
                return(new ValidationResult("The grid is not valid. Some values filled in are not correct."));
            }
        }
예제 #9
0
        public void find_hidden_singles_test()
        {
            SudokuGrid grid = new SudokuGrid("000000000|000200000|000000000||000060000|000000000|000080000||000000000|000000000|000002000");

            grid.set_possible_values_of_all_cells();

            HashSet <(int, int, int)> results          = SudokuHelper.find_hidden_singles(grid._grid_cells);
            HashSet <(int, int, int)> expected_results = new HashSet <(int, int, int)> {
                (4, 4, 2)
            };

            int a_not_b = results.Except(expected_results).Count();
            int b_not_a = expected_results.Except(results).Count();

            Assert.True(results.Count == expected_results.Count && a_not_b == 0 && b_not_a == 0);
        }
예제 #10
0
        public void ParseFromString()
        {
            string source = @"7 9 5 1 2 4 3 6 8
1 2 4 8 6 3 7 9 5
3 6 8 7 5 9 2 1 4
6 4 1 _ _ _ _ 8 7
2 5 9 _ 7 8 6 4 _
_ 8 7 _ _ _ _ 2 _
_ 1 _ 5 _ 7 _ 3 6
_ 7 _ _ _ _ _ 5 _
5 3 _ _ 8 _ 1 7 _";

            Definition.Sudoku sudoku;
            Assert.IsTrue(SudokuHelper.TryParse(source, out sudoku));
            Assert.AreEqual(source, sudoku.ToString());
        }
예제 #11
0
        static void Main(string[] args)
        {
            //var map = new int[9, 9]
            //{
            //    { 0,8,0,0,0,0,6,0,0},
            //    { 0,0,0,4,0,0,0,0,9},
            //    { 0,7,0,0,0,0,8,0,5},
            //    { 4,0,0,0,0,0,0,0,0},
            //    { 0,3,0,0,6,0,0,9,0},
            //    { 0,0,0,7,2,0,1,0,0},
            //    { 0,9,3,2,0,0,0,6,4},
            //    { 8,1,0,3,0,0,0,0,0},
            //    { 0,0,0,0,0,5,0,0,0}
            //};
            var map = new string[]
            {
                "800074000",
                "050600003",
                "000200000",
                "900500040",
                "000140060",
                "000006800",
                "000902056",
                "062000900",
                "070001200"
            };
            var numMap = new int[9, 9];

            for (int i = 0; i < 9; i++)
            {
                for (int k = 0; k < 9; k++)
                {
                    numMap[i, k] = Convert.ToInt32(map[i][k].ToString());
                }
            }
            var sudoku = new SudokuHelper(numMap);

            sudoku.Run();
            for (int i = 0; i < 9; i++)
            {
                for (int k = 0; k < 9; k++)
                {
                    Console.Write($"{sudoku.Map[i, k]}\t");
                }
                Console.WriteLine();
            }
        }
예제 #12
0
        static void Main(string[] args)
        {
            var services = new ServiceCollection();

            ConfigureServices(services);

            using (ServiceProvider serviceProvider = services.BuildServiceProvider())
            {
                SudokuHelper helper = serviceProvider.GetService <SudokuHelper>();

                //MyApplication app = serviceProvider.GetService<MyApplication>();
                //app.Run();


                SudokuGrid grid = new SudokuGrid("010809300000506028007003001300601804004200500070090060700048000620130000081000950");
                Console.WriteLine(grid.ToStringFormatted());
                SudokuHelper.solve_grid(grid);
                Console.WriteLine(grid.ToStringFormatted());
            }
        }
예제 #13
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);
        }
예제 #14
0
        private static void Main()
        {
            List <SudokuWithLegalValues> results = SudokuHelper.Solve(originalSudoku16, 2, null, out _);

            int counter = 1;

            if (results == null)
            {
                return;
            }

            foreach (var sudokuSolution in results)
            {
                Console.WriteLine($"Solution {counter}/{results.Count}:");
                SudokuHelper.LogSudoku(sudokuSolution, true);
                Console.WriteLine();
                Console.WriteLine();

                string compact = SudokuHelper.GenerateDisplayStringCompact(sudokuSolution);
                Console.WriteLine(compact);
                counter++;
            }
        }
예제 #15
0
 public void are_lists_equal_false(List <int> a, List <int> b, string message)
 {
     Assert.False(SudokuHelper.are_int_lists_equal(a, b), message);
 }
예제 #16
0
        public IActionResult GenerateGrid()
        {
            var helper = new SudokuHelper();

            return(Ok(helper.GenerateValidGrid()));
        }
예제 #17
0
 private void Btn_Click(object sender, EventArgs e)
 {
     sudokuHelper           = new SudokuHelper(sudokuTable.DataSource);
     sudokuTable.DataSource = sudokuHelper.Run().Map;
 }
예제 #18
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();
        }