コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: KidzCode1/Sudoku
        private bool ApplySolvers()
        {
            bool squaresSolved = false;

            foreach (BaseGroupSolver sudokuSolver in solvers)
            {
                // Calling solve 27 times for all the rows, columns, and blocks.
                for (int c = 0; c < 9; c++)
                {
                    if (sudokuSolver.Solve(SudokuBoard.GetColumn(c), GroupKind.Column) == SolveResult.SquaresSolved)
                    {
                        squaresSolved = true;
                    }
                }
                for (int r = 0; r < 9; r++)
                {
                    if (sudokuSolver.Solve(SudokuBoard.GetRow(r), GroupKind.Row) == SolveResult.SquaresSolved)
                    {
                        squaresSolved = true;
                    }
                }
                for (int r = 0; r < 3; r++)
                {
                    for (int c = 0; c < 3; c++)
                    {
                        if (sudokuSolver.Solve(SudokuBoard.GetBlock(r * 3, c * 3), GroupKind.Block) == SolveResult.SquaresSolved)
                        {
                            squaresSolved = true;
                        }
                    }
                }
            }

            return(squaresSolved);
        }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: KidzCode1/Sudoku
        private void ShowNotesForSquareAt(ISudokuSquare square)
        {
            ISudokuSquare[] column = SudokuBoard.GetColumn(square.Column);
            ISudokuSquare[] row    = SudokuBoard.GetRow(square.Row);
            ISudokuSquare[] block  = SudokuBoard.GetBlock(square.Row, square.Column);

            List <char> availableChars = new List <char>();

            foreach (char item in tbxAvailableCharacter.Text)
            {
                availableChars.Add(item);
            }

            RemoveCharactersFromGroup(availableChars, row);
            RemoveCharactersFromGroup(availableChars, column);
            RemoveCharactersFromGroup(availableChars, block);

            square.SetNotes(string.Join(", ", availableChars));
        }
コード例 #3
0
ファイル: SolverNxN.cs プロジェクト: KidzCode1/Sudoku
        private SolveResult SolveForManyWithIndicesToCheck(ISudokuSquare[] group, ref SolveResult result, List <int> indicesToCheck, GroupKind groupKind, List <List <int> > allNoteNumbers)
        {
            // We want to add the new logic!
            if (groupKind == GroupKind.Column || groupKind == GroupKind.Row)
            {
                // TODO: Group by block

                Dictionary <int, List <int> > blockMap = new Dictionary <int, List <int> >();

                foreach (int index in indicesToCheck)
                {
                    int key = group[index].Block;
                    if (!blockMap.ContainsKey(key))
                    {
                        blockMap.Add(key, new List <int>());
                    }

                    List <int> allBlockNotes = blockMap[key];
                    List <int> notes         = GetNumbers(group[index].Notes);
                    foreach (int note in notes)
                    {
                        if (allBlockNotes.IndexOf(note) < 0)
                        {
                            allBlockNotes.Add(note);
                        }
                    }
                }

                if (blockMap.Count > 1)
                {
                    foreach (int key in blockMap.Keys)
                    {
                        List <int> targetBlock = blockMap[key];
                        List <int> resultsForThisSubtraction = new List <int>();
                        resultsForThisSubtraction = targetBlock.ToList();
                        foreach (int sourceKey in blockMap.Keys)
                        {
                            List <int> sourceBlock = blockMap[sourceKey];
                            if (targetBlock == sourceBlock)
                            {
                                continue;
                            }

                            // We need to subtract!!
                            foreach (int note in sourceBlock)
                            {
                                if (resultsForThisSubtraction.IndexOf(note) >= 0)
                                {
                                    resultsForThisSubtraction.Remove(note);
                                }
                            }
                        }

                        // Subtraction for this block is done!

                        if (resultsForThisSubtraction.Count > 0)
                        {
                            // Magic!!! We can remove these from other squares in the targetBlock!
                            ISudokuSquare[] blocks = SudokuBoard.GetBlock(key);

                            // We need to make sure that any blocks we examine are not among those already in group[indicesToCheck]
                        }
                    }
                }

                //foreach (List<int> noteNumbers in allNoteNumbers)
                //{

                //}

                // TODO: Do we have more than one block?
                // Yes? It's magic!!!
                // TODO: Set Subtraction (e.g., b1 - b2) - b1 is the first block we subtract from
                // TODO: Set Subtraction the other (b2 - b1) - so b2 is the first block we subtract from
                // TODO: Anything left over???
                // Yes? Then we can remove those leftover numbers from **all the other squares** in the first block we subtracted from.
                // LaterCommands.Add(new RemoveCommand(b2, "1, 3"));
            }

            List <int> allNumbersFound = new List <int>();

            foreach (List <int> noteNumbers in allNoteNumbers)
            {
                AddNumbersTo(allNumbersFound, noteNumbers);
            }

            if (allNumbersFound.Count > indicesToCheck.Count)
            {
                return(SolveResult.None);
            }
            else
            {
                for (int i = 0; i < group.Length; i++)
                {
                    if (indicesToCheck.Contains(i))
                    {
                        continue;
                    }

                    // We can actually remove!!!
                    if (RemoveNotesFrom(group[i], string.Join(", ", allNumbersFound)) == SolveResult.SquaresSolved)
                    {
                        result = SolveResult.SquaresSolved;
                    }
                }
            }

            return(result);
        }
コード例 #4
0
ファイル: MainWindow.xaml.cs プロジェクト: KidzCode1/Sudoku
        bool CheckForConflicts(int r, int c, bool setHasConflictedProperty = true)
        {
            ISudokuSquare thisSquare = SudokuBoard.squares[r, c];
            string        text       = thisSquare.GetText();

            if (string.IsNullOrWhiteSpace(text))
            {
                return(false);
            }

            ISudokuSquare[] column = SudokuBoard.GetColumn(c);
            ISudokuSquare[] row    = SudokuBoard.GetRow(r);
            ISudokuSquare[] block  = SudokuBoard.GetBlock(r, c);

            bool isConflicted = false;

            for (int rowIndex = 0; rowIndex < 9; rowIndex++)
            {
                if (rowIndex != r && column[rowIndex].GetText() == text)
                {
                    if (setHasConflictedProperty)
                    {
                        thisSquare.HasConflict       = true;
                        column[rowIndex].HasConflict = true;
                    }
                    isConflicted = true;
                }
            }

            for (int colIndex = 0; colIndex < 9; colIndex++)
            {
                if (colIndex != c && row[colIndex].GetText() == text)
                {
                    if (setHasConflictedProperty)
                    {
                        thisSquare.HasConflict    = true;
                        row[colIndex].HasConflict = true;
                    }
                    isConflicted = true;
                }
            }

            for (int squareIndex = 0; squareIndex < 9; squareIndex++)
            {
                GetSquarePosition(block[squareIndex], out int blockRow, out int blockColumn);
                if (blockRow == r && blockColumn == c)
                {
                    continue;
                }

                if (block[squareIndex].GetText() == text)
                {
                    if (setHasConflictedProperty)
                    {
                        thisSquare.HasConflict         = true;
                        block[squareIndex].HasConflict = true;
                    }
                    isConflicted = true;
                }
            }

            return(isConflicted);
        }