Esempio n. 1
0
 private SudokuGame(SudokuGame original, string name)
 {
     Name = name;
     Grid = new SudokuCell[9, 9];
     original.GetCellList().ForEach(cell => { Grid[cell.Row, cell.Column] = cell.Copy(this); });
     History = new List <IntegerTriple>(original.History);
 }
Esempio n. 2
0
        private SudokuGame Search(SudokuGame sudokuGame, int level)
        {
            // check vor valid game
            if (!sudokuGame.IsValid())
            {
                sudokuGame.GetFirstInvalid();
                return(null);
            }

            List <SudokuCell> cellsToFill = sudokuGame.GetCellList().Where(ce => (ce.CellValue == 0)).ToList();

            // nothing left to fill : it is a valid solution
            if (cellsToFill.Count == 0)
            {
                return(sudokuGame);
            }

            int        c    = 0;
            SudokuCell cell = cellsToFill.First();

            foreach (int possibleValue in cell.CellPossibleValues)
            {
                PerformanceCounter.Update(cell.CellPossibleValues.Count, ++c);
                //make copy of the game
                SudokuGame tryGame = sudokuGame.Copy();

                // try the value
                if (tryGame.SetValue(cell.Row, cell.Column, possibleValue))
                {
                    // now try one recursion deeper, with one field more set
                    PerformanceCounter.Down();
                    tryGame = Search(tryGame, level + 1);
                    PerformanceCounter.Up();
                    if (tryGame != null)
                    {
                        // the value contributes to a valid solution
                        return(tryGame);
                    }
                }
            }

            // no cell and no field generates a valid solution
            return(null);
        }
Esempio n. 3
0
        public List <IntegerTriple> Generate()
        {
            SudokuGame sudokuGameGenerator = new SudokuGame("L1");

            // populate the 3x3 sub matrix on the main diagonal
            // that can be done without checks
            sudokuGameGenerator.PopulateSubmatrix(0, 2, 0, 2);
            sudokuGameGenerator.PopulateSubmatrix(3, 5, 3, 5);
            sudokuGameGenerator.PopulateSubmatrix(6, 8, 6, 8);

            // Fill the empty
            sudokuGameGenerator = Search(sudokuGameGenerator);

            if (sudokuGameGenerator == null)
            {
                return(null);
            }

            List <IntegerTriple> list = new List <IntegerTriple>();

            sudokuGameGenerator.GetCellList().ForEach(cell => list.Add(new IntegerTriple(cell.Row, cell.Column, cell.CellValue)));
            return(list);
        }