private void Initialize()
        {
            this.Failed = false;

            for (int x = 0; x < 9; x++)
            {
                for (int y = 0; y < 9; y++)
                {
                    int boxX     = (int)Math.Floor(x / 3.0);
                    int boxY     = (int)Math.Floor(y / 3.0);
                    int boxIndex = 18 + boxX + (boxY * 3);
                    if (x == 0)
                    {
                        cellsets[y] = new SudokuRow();
                    }
                    if (y == 0)
                    {
                        cellsets[9 + x] = new SudokuColumn();
                    }
                    if (null == cellsets[boxIndex])
                    {
                        cellsets[boxIndex] = new SudokuBox();
                    }
                    SudokuCell cell = new SudokuCell(x, y);
                    cell.FailedEvent += cell_FailedEvent;
                    cellsets[y].Add(cell);
                    cellsets[9 + x].Add(cell);
                    cellsets[boxIndex].Add(cell);
                    allCells[x, y] = cell;
                }
            }
        }
Exemple #2
0
        private void ReduceBySoloSet()
        {
            lock (reduceLocker)
            {
                List <SudokuCell> checkSetRow = new List <SudokuCell>();
                List <SudokuCell> checkSetCol = new List <SudokuCell>();
                foreach (int i in Enumerable.Range(1, 9))
                {
                    checkSetRow.Clear();
                    checkSetCol.Clear();
                    SudokuRow    row = null;
                    SudokuColumn col = null;

                    bool isUniqueRow = true;
                    bool isUniqueCol = true;
                    foreach (SudokuCell possCell in GetPossibleCellsByValue(i))
                    {
                        if (possCell.IsSolved)
                        {
                            isUniqueCol = false;
                            isUniqueRow = false;
                            break;
                        }
                        if (null == col)
                        {
                            col = possCell.col;
                            row = possCell.row;
                        }
                        if (isUniqueRow)
                        {
                            if (row != possCell.row)
                            {
                                isUniqueRow = false;
                            }
                            checkSetRow.Add(possCell);
                        }
                        if (isUniqueCol)
                        {
                            if (col != possCell.col)
                            {
                                isUniqueCol = false;
                            }
                            checkSetCol.Add(possCell);
                        }
                    }
                    if (isUniqueCol && checkSetCol.Count > 1)
                    {
                        lock (setLocker)
                        {
                            // remove these values from other cells in the set
                            checkSetCol[0].col.RemoveValuesFromSet(i, checkSetCol);
                        }
                    }
                    if (isUniqueRow && checkSetRow.Count > 1)
                    {
                        lock (setLocker)
                        {
                            // remove these values from other cells in the set
                            checkSetRow[0].row.RemoveValuesFromSet(i, checkSetRow);
                        }
                    }
                }
            }
        }