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
        public SudokuCell(SudokuGame parent, int row, int column)
        {
            Parent = parent;
            Row    = row;
            Column = column;

            CellPossibleValues = GetInitialPossibleValueList();
        }
Esempio n. 3
0
 private SudokuCell(SudokuGame parent, SudokuCell original)
 {
     Parent             = parent;
     Row                = original.Row;
     Column             = original.Column;
     CellValue          = original.CellValue;
     CellPossibleValues = new List <int>(original.CellPossibleValues);
 }
Esempio n. 4
0
        public bool IsEqual(SudokuGame other)
        {
            for (int row = 0; row < 9; row++)
            {
                for (int column = 0; column < 9; column++)
                {
                    if (!Grid[row, column].IsEqual(other[row, column]))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 5
0
        public bool Solve()
        {
            SudokuGame solver = Copy();

            solver = Search(solver);

            if (solver == null)
            {
                return(false);
            }

            var sublist = solver.History.GetRange(History.Count, (solver.History.Count - History.Count));

            sublist.ForEach(cell => { SetValue(cell.Item1, cell.Item2, cell.Item3); });

            return(true);
        }
Esempio n. 6
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. 7
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);
        }
Esempio n. 8
0
 private SudokuGame Search(SudokuGame sudokuGame)
 {
     PerformanceCounter = new PerformanceCounter();
     return(Search(sudokuGame, 1));
 }
Esempio n. 9
0
 internal SudokuCell Copy(SudokuGame parent)
 {
     return(new SudokuCell(parent, this));
 }