예제 #1
0
        public void equals_failure_possible()
        {
            string     puzzle = "004300209005009001070060043006002087190007400050083000600000105003508690042910300";
            SudokuGrid grid1  = new SudokuGrid(puzzle);
            SudokuGrid grid2  = new SudokuGrid(puzzle);

            grid1._grid_cells[1, 1]._possible_values = new List <int> {
                1, 2, 3
            };
            grid1._grid_cells[2, 2]._possible_values = new List <int> {
                2
            };
            grid1._grid_cells[3, 3]._possible_values = new List <int> {
                1, 3
            };
            grid1._grid_cells[4, 4]._possible_values = new List <int> {
                1, 2, 3
            };

            grid1._grid_cells[1, 1]._possible_values = new List <int> {
                1, 2, 3, 4
            };
            grid1._grid_cells[2, 2]._possible_values = new List <int> {
                2
            };
            grid1._grid_cells[3, 3]._possible_values = new List <int> {
                1, 3
            };
            grid1._grid_cells[4, 4]._possible_values = new List <int> {
                1, 2, 3
            };

            Assert.False(grid1.Equals(grid2), "Grids same puzzles, but different possible values should not be equal");
        }
예제 #2
0
        public void equals_success_possible()
        {
            string     puzzle = "004300209005009001070060043006002087190007400050083000600000105003508690042910300";
            SudokuGrid grid1  = new SudokuGrid(puzzle);
            SudokuGrid grid2  = new SudokuGrid(puzzle);

            grid1._grid_cells[1, 1]._possible_values = new List <int> {
                1, 2, 3
            };
            grid1._grid_cells[2, 2]._possible_values = new List <int> {
                2
            };
            grid1._grid_cells[3, 3]._possible_values = new List <int> {
                1, 3
            };
            grid1._grid_cells[4, 4]._possible_values = new List <int> {
                1, 2, 3
            };

            grid2._grid_cells[1, 1]._possible_values = new List <int> {
                1, 2, 3
            };
            grid2._grid_cells[2, 2]._possible_values = new List <int> {
                2
            };
            grid2._grid_cells[3, 3]._possible_values = new List <int> {
                1, 3
            };
            grid2._grid_cells[4, 4]._possible_values = new List <int> {
                1, 2, 3
            };

            Assert.True(grid1.Equals(grid2), "Grids created with the same puzzles should be equal");
        }
예제 #3
0
        public void GetUnsetNeighbors_VariableIsUnset_ScreensOutVariable()
        {
            // Arrange
            var grid     = new SudokuGrid(FullGrid());
            var variable = grid[0, 2];

            variable.Unset();

            var neighbors = new Variable[]
            {
                grid[0, 5],                 // same row
                grid[3, 2],                 // same column
                grid[1, 0],                 // same square
                grid[2, 2]                  // same column and square
            };

            foreach (var neighbor in neighbors)
            {
                neighbor.Unset();
            }

            // Act
            var unsetNeighbors = new List <Variable>(grid.GetUnsetNeighbors(variable));

            // Assert
            CollectionAssert.DoesNotContain(unsetNeighbors, variable);
        }
예제 #4
0
        public void get_intersecting_blocks_true(CoordinateList reference_cells, List <CoordinateList> expected_coords, string message)
        {
            List <CoordinateList> block_coords = SudokuGrid.get_intersecting_blocks(reference_cells);

            bool match_found         = false;
            bool match_found_for_all = true;

            foreach (CoordinateList expected_block in expected_coords)
            {
                match_found = false;
                foreach (CoordinateList found_block in block_coords)
                {
                    if (expected_block.Equals(found_block))
                    {
                        match_found = true;
                    }
                }

                if (!match_found)
                {
                    match_found_for_all = false;
                }
            }

            Assert.True(match_found_for_all && (expected_coords.Count() == block_coords.Count()), $"{message}; matches found={match_found_for_all}; expected count={expected_coords.Count()}; found count={block_coords.Count()}");
        }
예제 #5
0
        public void GetSetNeighbors_WithAnyVariable_ReturnsSetVariablesInRowColumnAndSquare()
        {
            // Arrange
            var grid     = new SudokuGrid(EmptyGrid());
            var variable = grid[8, 8];

            var expectedVariables = new Variable[]
            {
                grid[8, 3],                 // same row
                grid[1, 8],                 // same column
                grid[7, 7],                 // same square
                grid[8, 6]                  // same row and square
            };

            foreach (var neighbor in expectedVariables)
            {
                neighbor.Value = 2;
            }

            // Act
            var setNeighbors = new List <Variable>(grid.GetSetNeighbors(variable));

            // Assert
            CollectionAssert.AreEquivalent(expectedVariables, setNeighbors);
        }
예제 #6
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()}");
        }
예제 #7
0
        static void Main(string[] args)
        {
            SudokuWriter        sudokuWriter = new SudokuWriter();
            ImmutableSudokuGrid filledGrid   = sudokuWriter.CreateFilledGrid();

            filledGrid.PrintGrid();

            Console.WriteLine("\n");

            ImmutableSudokuGrid emptiedGrid = sudokuWriter.EmptyGridForHardSolve(filledGrid);

            emptiedGrid.PrintGrid();

            Console.WriteLine("\n");

            SudokuGrid   mutableEmptiedGrid = emptiedGrid.MakeMutableCopy();
            SudokuSolver sudokuSolver       = new SudokuSolver();

            sudokuSolver.Solve(mutableEmptiedGrid);
            mutableEmptiedGrid.PrintGrid();

            if (mutableEmptiedGrid.FindAllEmptySquares().Count != 0)
            {
                Console.WriteLine("\n");
                HarderSudokuSolver  harderSolver = new HarderSudokuSolver();
                ImmutableSudokuGrid solvedGrid   = harderSolver.Solve(emptiedGrid);
                solvedGrid.PrintGrid();
            }
        }
예제 #8
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //  deserialization
            Stream objstreamdeserialize_SudokuGrid = new FileStream("data.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
            Stream objstreamdeserialize_moveLog    = new FileStream("data1.bin", FileMode.Open, FileAccess.Read, FileShare.Read);

            try
            {
                sudokuGrid = (SudokuGrid)objBinaryFormatter.Deserialize(objstreamdeserialize_SudokuGrid);
                moveLog    = (Dictionary <Point, int>)objBinaryFormatter.Deserialize(objstreamdeserialize_moveLog);
                foreach (KeyValuePair <Point, int> kvp in moveLog)
                {
                    sudokuGrid.MakeMove(kvp.Key.X, kvp.Key.Y, kvp.Value);//y,x
                }
                gameBoard = new SudokuTableGraphic(sudokuGrid);
                this.Controls.Add(gameBoard);
            }
            catch (Exception ea)
            {
                DrawBoard();
            }
            finally
            {
                objstreamdeserialize_SudokuGrid.Close();
                objstreamdeserialize_moveLog.Close();
            }
        }
예제 #9
0
        public static byte[] ToBinary(SudokuGrid grid, SudokuConvertionAlgorithm algorithm)
        {
            var sb = new StringBuilder(grid.Metrics.CellsTotal);

            grid.IterateLinesXY((x, y) => sb.Append(grid.Cells[y, x].Number));
            return(ToBinary(sb.ToString(), algorithm, grid.Constraints, grid.Metrics));
        }
예제 #10
0
        public void DrawBoard()
        {
            if (!(gameBoard == null))
            {
                gameBoard.Dispose();
            }
            SudokuGrid.GameDifficulty diff;
            if (radioButton1.Checked)
            {
                diff = SudokuGrid.GameDifficulty.Hard;
            }
            else if (radioButton2.Checked)
            {
                diff = SudokuGrid.GameDifficulty.Medium;
            }
            else
            {
                diff = SudokuGrid.GameDifficulty.Easy;
            }
            sudokuGrid = new SudokuGrid(diff);
            gameBoard  = new SudokuTableGraphic(sudokuGrid);
            moveLog    = new Dictionary <Point, int>();

            this.Controls.Add(gameBoard);
        }
예제 #11
0
파일: Form1.cs 프로젝트: ilyakom/Sudoku
        private async void Cell_MouseClick(object sender, MouseEventArgs e)
        {
            if (_model.CurrentSudoku == null)
            {
                return;
            }

            var row    = SudokuGrid.GetRow((Label)sender);
            var column = SudokuGrid.GetColumn((Label)sender);

            var curValue     = ((Label)sender).Text == "" ? 0 : int.Parse(((Label)sender).Text);
            var initialValue = curValue;

            for (var i = 0; i < Domain.Sudoku.BigSide; i++)
            {
                _model.CurrentSudoku[row, column] = curValue >= 9 ? 0 : curValue + 1;

                if (await Task.Run(() => Validations.ValidateSudoku(_model.CurrentSudoku)))
                {
                    ((Label)sender).Text = _model.CurrentSudoku[row, column] == 0 ? "" : _model.CurrentSudoku[row, column].ToString();
                    return;
                }

                curValue = curValue >= 9 ? 0 : curValue + 1;
            }

            _model.CurrentSudoku[row, column] = initialValue;
        }
예제 #12
0
        public void GetSetNeighbors_VariableIsSet_ReturnsVariableWithNeighbors()
        {
            // Arrange
            var grid     = new SudokuGrid(EmptyGrid());
            var variable = grid[6, 1];

            variable.Value = 4;

            var neighbors = new Variable[]
            {
                grid[6, 6],                 // same row
                grid[2, 1],                 // same column
                grid[7, 0],                 // same square
                grid[6, 0]                  // same row and square
            };

            foreach (var neighbor in neighbors)
            {
                neighbor.Value = 7;
            }

            // Act
            var setNeighbors = new List <Variable>(grid.GetSetNeighbors(variable));

            // Assert
            CollectionAssert.DoesNotContain(setNeighbors, variable);
        }
        public SudokuSolvingIterationAssumptionTechnique(SudokuGrid grid) : base(grid)
        {
            rowsOfNumbers    = new SortedSet <byte> [Grid.Metrics.MaximumNumber];
            columnsOfNumbers = new SortedSet <byte> [Grid.Metrics.MaximumNumber];
            Grid.IterateLine(i =>
            {
                rowsOfNumbers[i]    = new SortedSet <byte>();
                columnsOfNumbers[i] = new SortedSet <byte>();
            });
            blocksOfNumbers = new SortedSet <byte> [Grid.Metrics.MaximumNumber / Grid.Metrics.BlockHeight,
                                                    Grid.Metrics.MaximumNumber / Grid.Metrics.BlockWidth];
            Grid.IterateBlocksXY((x, y) =>
            {
                blocksOfNumbers[y, x] = new SortedSet <byte>();
            });
            forbiddenCandidates     = new SortedSet <byte>();
            Grid.CellNumberChanged += (s, e) => RegisterCellAsClue(e.Cell);

            emptyCellsCount = Grid.Metrics.CellsTotal;

            //Grid.IterateLinesXY((x, y) =>
            //{
            //    SudokuGridCell cell = Grid.Cells[y, x];
            //    if (cell.IsClue)
            //    {
            //        RegisterCellAsClue(cell);
            //    }
            //});
        }
예제 #14
0
        private int RemoveElementsLogically(ref SudokuGrid grid, int toRemove)
        {
            if (toRemove == 0)
            {
                return(toRemove);
            }

            var values = Enumerable.Range(0, 80).ToList();

            values.Shuffle(_seed);

            // Reset cell value if it does not make the board ambigous,
            // i.e. if it is not possible to place another value in its place
            foreach (var cell in values)
            {
                var x            = cell % 9;
                var y            = cell / 9;
                var validNumbers = grid.ValidValuesCount(x, y);
                if (validNumbers == 1)
                {
                    grid.Grid[x, y].Value = 0;
                    toRemove--;

                    if (toRemove == 0)
                    {
                        return(0);
                    }
                }
            }

            return(toRemove);
        }
예제 #15
0
        static void Main(string[] args)
        {
            var sudoku = new SudokuGrid();

            sudoku.TrySetValue(1, 0, 6);
            sudoku.TrySetValue(2, 0, 8);
            sudoku.TrySetValue(3, 0, 0);
            sudoku.TrySetValue(4, 0, 2);
            sudoku.TrySetValue(5, 0, 1);
            sudoku.TrySetValue(7, 0, 7);
            sudoku.TrySetValue(8, 0, 4);

            sudoku.TrySetValue(1, 1, 5);
            sudoku.TrySetValue(3, 1, 4);
            sudoku.TrySetValue(4, 1, 8);
            sudoku.TrySetValue(6, 1, 6);
            sudoku.TrySetValue(0, 2, 4);
            sudoku.TrySetValue(2, 2, 7);
            sudoku.TrySetValue(3, 2, 6);
            sudoku.TrySetValue(6, 2, 1);
            sudoku.TrySetValue(7, 2, 0);

            sudoku.TrySetValue(0, 8, 1);
            sudoku.WriteToConsole();
        }
예제 #16
0
        public void equals_failure_basic()
        {
            SudokuGrid grid1 = new SudokuGrid("004300209005009001070060043006002087190007400050083000600000105003508690042910300");
            SudokuGrid grid2 = new SudokuGrid("004300209005009001070060043006002087190007400050083000600000105003508690042910301");

            Assert.False(grid1.Equals(grid2), "Grids created with the different puzzles should not be equal");
        }
예제 #17
0
        protected SudokuGrid GenerateFailingSudokuSquareCondition()
        {
            int  BoxNo   = RandomGenerator.Next() % 9;
            int  RowNo1  = RandomGenerator.Next() % 3;
            int  RowNo2  = RandomGenerator.Next() % 3;
            int  ColNo1  = RandomGenerator.Next() % 3;
            int  ColNo2  = RandomGenerator.Next() % 3;
            byte RandVal = (byte)((RandomGenerator.Next() % 9) + 1);

            while (ColNo1 == ColNo2)
            {
                ColNo2 = RandomGenerator.Next() % 3;
            }
            while (RowNo1 == RowNo2)
            {
                RowNo2 = RandomGenerator.Next() % 3;
            }

            var Output = new SudokuGrid();
            int SquareStartRow, SquareStartCol;

            SolverCore.SudokuGrid.SquareStartCoordinates(BoxNo, out SquareStartRow, out SquareStartCol);
            Output.Data[SquareStartRow + RowNo1, SquareStartCol + ColNo1] = RandVal;
            Output.Data[SquareStartRow + RowNo2, SquareStartCol + ColNo2] = RandVal;
            return(Output);
        }
예제 #18
0
        public void GenerateCandidateValues()
        {
            var values = new[] { 1, 9, 5, 6, 2, 7, 3, 8, 4,
                                 8, 7, 6, 4, 9, 3, 2, 1, 5,
                                 4, 3, 2, 5, 1, 8, 7, 9, 6,
                                 6, 2, 9, 3, 5, 1, 8, 4, 7,
                                 3, 1, 4, 8, 7, 6, 9, 5, 2,
                                 7, 5, 8, 9, 4, 2, 1, 6, 3,
                                 2, 4, 7, 1, 6, 9, 5, 3, 8,
                                 9, 6, 3, 7, 8, 5, 0, 0, 0,
                                 5, 8, 1, 2, 3, 4, 0, 0, 0 };

            var cells = GenerateCellGrid(values);

            var grid = new SudokuGrid(cells);

            grid.GenerateCandidateValues();

            grid.GetCandidateValues(7, 6).Count.ShouldBe(1);
            grid.GetCandidateValues(7, 7).Count.ShouldBe(1);
            grid.GetCandidateValues(7, 8).Count.ShouldBe(1);
            grid.GetCandidateValues(8, 6).Count.ShouldBe(1);
            grid.GetCandidateValues(8, 7).Count.ShouldBe(1);
            grid.GetCandidateValues(8, 8).Count.ShouldBe(1);
        }
예제 #19
0
        public void GetUnsetNeighbors_WithAnyVariable_ReturnsUnsetVariablesInRowColumnAndSquare()
        {
            // Arrange
            var grid     = new SudokuGrid(FullGrid());
            var variable = grid[4, 5];

            var expectedVariables = new Variable[]
            {
                grid[4, 6],                 // same row
                grid[1, 5],                 // same column
                grid[3, 3],                 // same square
                grid[5, 5]                  // same column and square
            };

            foreach (var neighbor in expectedVariables)
            {
                neighbor.Unset();
            }

            // Act
            var unsetNeighbors = new List <Variable>(grid.GetUnsetNeighbors(variable));

            // Assert
            CollectionAssert.AreEquivalent(expectedVariables, unsetNeighbors);
        }
예제 #20
0
 private static void GeneratePuzzle(SudokuGrid sudokuGrid,
                                    ISudokuProvider sudokuProvider)
 {
     sudokuGrid.SudokuProvider = sudokuProvider;
     sudokuGrid.NewGame();
     Console.WriteLine(sudokuGrid.ToString());
 }
예제 #21
0
        public SudokuTableGraphic(SudokuGrid sudokuGrid)
        {
            this.sudokuGrid = sudokuGrid;


            this.AutoSize = true;
            this.Top      = 60;
            this.Left     = 80;
            //  this.Width = 150;

            // this.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;


            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    SudokuCell sc = new SudokuCell(sudokuGrid[i, j], j, i);
                    if (!sudokuGrid[i, j].IsUnchangable)
                    {
                        sc.render(sudokuGrid[i, j].CellVal);
                    }
                    this.Controls.Add(sc, j, i);
                }
            }
        }
예제 #22
0
        /// <summary>
        /// Create a 3x3 grid, with focus set to center tile.
        /// </summary>
        public SudokuGrid SmallGrid()
        {
            SudokuGrid grid = new SudokuGrid(3, 3, 3);

            grid.Select(1, 1);
            return(grid);
        }
예제 #23
0
        private bool SolverFunction(ref SudokuGrid grid)
        {
            //grid.PrintAll(); // Debug only

            var cell = GetNextEmptyCell(grid);

            if (cell == null)
            {
                // No more empty cells. We are done.
                return(true);
            }

            var possibleValues = PossibleValuesForPosition(grid, cell);

            for (int i = 1; i < 10; i++)
            {
                if (possibleValues.Any(v => v == i))
                {
                    cell.Value = i;
                    if (SolverFunction(ref grid))
                    {
                        return(true);
                    }
                }
            }

            cell.Value = 0;
            return(false);
        }
예제 #24
0
        //returns current sudokuGrid
        public SudokuGrid CurrentGrid()
        {
            var cellArray = form1.Controls.OfType <MyCellBox>().ToList();
            //if invalid cell, make cell red
            //SudokuSolver sudokuSolver = new SudokuSolver();
            //var cellArray = this.Controls.OfType<MyCellBox>().ToList();
            SudokuSolver sudokuSolver = new SudokuSolver();

            int[,] intSudokuGrid = new int[9, 9];
            for (int row = 0; row < 9; row++)
            {
                for (int column = 0; column < 9; column++)
                {
                    if (sudokuSolver.isDigit(cellArray[row * 9 + column].Text))
                    {
                        intSudokuGrid[row, column] = int.Parse(cellArray[row * 9 + column].Text);
                    }
                    //intSudokuGrid[row, column] = int.Parse(cellArray[row * 9 + column].Text);
                }
            }
            SudokuGrid mySudoku = new SudokuGrid();

            mySudoku = sudokuSolver.FromIntArray(intSudokuGrid);
            return(mySudoku);
        }
예제 #25
0
        public static int[][] GetSolution(int[][] values)
        {
            var board = new SudokuGrid(values.Length);

            board.SetGridValues(values);
            board.Solve();
            return(board.GetGridValues());
        }
        private void Button_Restart_Click(object sender, RoutedEventArgs e)
        {
            SudokuGrid.RestartSudoku();

            UpdateProgressBar();
            RestartTimer();
            ClearMessage();
        }
예제 #27
0
 private bool CanAddValue(SudokuGrid grid, SudokuCell cell, int value)
 {
     if (IsInRow(grid, cell, value) || IsInColumn(grid, cell, value) || IsInSquare(grid, cell, value))
     {
         return(false);
     }
     return(true);
 }
        private void ComboBox_SudokuDifficulty_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SudokuGrid.GenerateAndPopulateSudoku();

            RestartTimer();
            UpdateProgressBar();
            ClearMessage();
        }
        private void Button_GenerateNew_Click(object sender, RoutedEventArgs e)
        {
            SudokuGrid.GenerateAndPopulateSudoku();

            RestartTimer();
            UpdateProgressBar();
            ClearMessage();
        }
 private void Button_Redo_Click(object sender, RoutedEventArgs e)
 {
     if (SudokuGrid.RedoPlayerAction())
     {
         UpdateProgressBar();
         ClearMessage();
     }
 }
        public void SingleSolutionSudokuProperlySolved()
        {
            var TestSudoku = new SudokuGrid();
            TestSudoku.LinearArr = new byte[81]
            {
                5, 3, 0, 0, 7, 0, 0, 0, 0,
                6, 0, 0, 1, 9, 5, 0, 0, 0,
                0, 9, 8, 0, 0, 0, 0, 6, 0,
                8, 0, 0, 0, 6, 0, 0, 0, 3,
                4, 0, 0, 8, 0, 3, 0, 0, 1,
                7, 0, 0, 0, 2, 0, 0, 0, 6,
                0, 6, 0, 0, 0, 0, 2, 8, 0,
                0, 0, 0, 4, 1, 9, 0, 0, 5,
                0, 0, 0, 0, 8, 0, 0, 7, 9
            };
            var TestSolver = new SudokuSolver();
            TestSolver.SetInput(TestSudoku);
            TestSolver.Solve();
            Assert.AreEqual(TestSolver.Solutions.Count, 1);

            var TestSolution = TestSolver.ConvertSolution(0);
            byte[] ExpectedSolution = new byte[81]
            {
                5, 3, 4, 6, 7, 8, 9, 1, 2,
                6, 7, 2, 1, 9, 5, 3, 4, 8,
                1, 9, 8, 3, 4, 2, 5, 6, 7,
                8, 5, 9, 7, 6, 1, 4, 2, 3,
                4, 2, 6, 8, 5, 3, 7, 9, 1,
                7, 1, 3, 9, 2, 4, 8, 5, 6,
                9, 6, 1, 5, 3, 7, 2, 8, 4,
                2, 8, 7, 4, 1, 9, 6, 3, 5,
                3, 4, 5, 2, 8, 6, 1, 7, 9
            };

            for (int i = 0; i < TestSolution.LinearArr.Length; i++)
            {
                Assert.AreEqual(ExpectedSolution[i], TestSolution.LinearArr[i]);
            }
        }
예제 #32
0
        static void Main(string[] args)
        {
            // http://www.telegraph.co.uk/news/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html
            int[] data = {
                8, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 3, 6, 0, 0, 0, 0, 0,
                0, 7, 0, 0, 9, 0, 2, 0, 0,
                0, 5, 0, 0, 0, 7, 0, 0, 0,
                0, 0, 0, 0, 4, 5, 7, 0, 0,
                0, 0, 0, 1, 0, 0, 0, 3, 0,
                0, 0, 1, 0, 0, 0, 0, 6, 8,
                0, 0, 8, 5, 0, 0, 0, 1, 0,
                0, 9, 0, 0, 0, 0, 4, 0, 0
            };

            var puzzle = new SudokuGrid(data);
            var solver = new Solver(puzzle);

            var start = DateTime.Now;
            var solved = solver.Solve();
            var finish = DateTime.Now;

            if (solved)
            {
                Console.Out.WriteLine(puzzle);

                var elapsedTime = finish - start;
                var elapsed = elapsedTime.Milliseconds;
                var seconds = elapsed / 1000.0 + (elapsed % 1000L) / 1000.0;

                Console.Out.WriteLine(string.Format(CultureInfo.CurrentCulture, "\n\nTime taken {0} seconds\n", seconds));
            }
            else
            {
                Console.Out.WriteLine("No solution exists");
            }
        }
        protected SudokuGrid GenerateRandomSudoku()
        {
            int RowNo = RandomGenerator.Next() % 9;
            int ColNo = RandomGenerator.Next() % 9;
            byte RandVal = (byte)((RandomGenerator.Next() % 9) + 1);

            var Output = new SudokuGrid();
            Output.Data[RowNo, ColNo] = RandVal;
            return Output;
        }
        protected SudokuGrid GenerateFailingSudokuSquareCondition()
        {
            int BoxNo = RandomGenerator.Next() % 9;
            int RowNo1 = RandomGenerator.Next() % 3;
            int RowNo2 = RandomGenerator.Next() % 3;
            int ColNo1 = RandomGenerator.Next() % 3;
            int ColNo2 = RandomGenerator.Next() % 3;
            byte RandVal = (byte)((RandomGenerator.Next() % 9) + 1);
            while (ColNo1 == ColNo2)
            {
                ColNo2 = RandomGenerator.Next() % 3;
            }
            while (RowNo1 == RowNo2)
            {
                RowNo2 = RandomGenerator.Next() % 3;
            }

            var Output = new SudokuGrid();
            int SquareStartRow, SquareStartCol;
            SolverCore.SudokuGrid.SquareStartCoordinates(BoxNo, out SquareStartRow, out SquareStartCol);
            Output.Data[SquareStartRow + RowNo1, SquareStartCol + ColNo1] = RandVal;
            Output.Data[SquareStartRow + RowNo2, SquareStartCol + ColNo2] = RandVal;
            return Output;
        }
예제 #35
0
        void InitializeGrid(string puzzleFile="")
        {
            //string puzEZ = "test_puzzleEZ.puz";
            //string puz4 = "puzzle4.puz";
            //string puzNakedPair = "test_puzzleNakedPair.puz";
            //string puzNakedPair2 = "test_puzzleNakedPair2.puz";

            List<SudokuCell> puzzle = new List<SudokuCell>();

            if(!String.IsNullOrWhiteSpace(puzzleFile))
            {
                puzzle = puzzleSerializer.LoadPuzzle(puzzleFile);
            }
            else
            {
                puzzle = puzzleSerializer.LoadPuzzle();
            }

            sudokuGrid = new SudokuGrid(puzzle);
            sudokuGrid.DisplayOutputFunction = DebugWrite;
            simpleSolver = new SudokuSolver.Simple(sudokuGrid);
            moderateSolver = new SudokuSolver.Moderate(sudokuGrid);
            moderateSolver.DisplayOutputFunction = DebugWrite;

            List<int> puzzleArray = puzzle.Select(c => c.Value).ToList();
            tbDebug.Text += string.Format("Puzzle: \"{0}\"{1}", StaticSudoku.ArrayToString(puzzleArray), Environment.NewLine);
        }
        protected SudokuGrid GenerateFailingSudokuColumnCondition()
        {
            int RowNo1 = RandomGenerator.Next() % 9;
            int RowNo2 = RandomGenerator.Next() % 9;
            int ColNo = RandomGenerator.Next() % 9;
            byte RandVal = (byte)((RandomGenerator.Next() % 9) + 1);
            while (RowNo1 == RowNo2)
            {
                RowNo2 = RandomGenerator.Next() % 9;
            }

            var Output = new SudokuGrid();
            Output.Data[RowNo1, ColNo] = RandVal;
            Output.Data[RowNo2, ColNo] = RandVal;
            return Output;
        }
예제 #37
0
 /// <summary>
 /// Generates sudoku grid
 /// </summary>
 /// <param name="n">Number of sectors in row and column. for n = 9 means a 3x3 sudoku grid</param>
 /// <returns></returns>
 public static SudokuGrid Generate(int n,SudokuGrid.LevelDifficulty levelDifficulty)
 {
     return new SudokuGrid().Generate(n, levelDifficulty);
 }
예제 #38
0
 public Solver(SudokuGrid start)
 {
     this.grid = start;
     this.width = this.grid.Width;
     this.height = this.grid.Height;
 }