private Sudoku Parse(string sudokuString) { var cells = new List <SudokuCell>(81); var lines = sudokuString.Split('\n'); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { var cell = new SudokuCell(); cell.X = j; cell.Y = i; var charVal = lines[i][j]; cell.Value = charVal == '.' ? (int?)null : Convert.ToInt32(charVal.ToString()); cell.Initial = cell.Value != null; cell.Block = SudokuCell.GetBlock(cell.X, cell.Y); cells.Add(cell); } } return(new Sudoku(cells)); }
public CellNumberEntered(SudokuCell a_cell, int a_number_index) { m_cell = a_cell; m_number_index = a_number_index; m_states = (from num in a_cell.Numbers() select num.State).ToArray(); }
public void SetXandY() { var sut = new SudokuCell(0, 0); Assert.AreEqual(0, sut.X); Assert.AreEqual(0, sut.Y); }
/// <summary> /// Creates a new, empty SudokuField with the specified numer of rows and columns /// </summary> /// <param name="rows"></param> /// <param name="columns"></param> public void CreateNew(int rows, int columns) { if (StatusChanged != null) { StatusChanged("Creating new puzzle"); } IsNewPuzzle = true; NumRows = rows; NumColumns = columns; Init(); if (NumRows > 9) { ContentType = ContentType.HexaDecimal; } //this is an aid to help registering the cells with their section SudokuCell[,] speelveld = new SudokuCell[rows, columns]; for (int rowIndex = 0; rowIndex < rows; rowIndex++) { for (int columnIndex = 0; columnIndex < columns; columnIndex++) { SudokuCell sudokuCell = new SudokuCell(new RegistrationKey(rowIndex, columnIndex)); speelveld[rowIndex, columnIndex] = sudokuCell; } } RegisterField(speelveld); if (StatusChanged != null) { StatusChanged("Finished creating new puzzle"); } }
private void FillTextBoxesWithData() { for (int i = 0; i < Size; i++) { for (int j = 0; j < Size; j++) { var tb = _tbs[i, j]; SudokuCell cell = _data[i, j]; if (cell.Value == 0) { tb.Text = string.Empty; } else if (cell.IsPermanent) { tb.Text = cell.Value.ToString(); tb.IsEnabled = false; tb.Foreground = Brushes.Black; tb.FontWeight = FontWeights.UltraBold; } else if (cell.Value > 0 && cell.Value <= 9) { tb.Text = cell.Value.ToString(); } else { tb.Text = string.Empty; } } } }
/// <summary> /// Creates a new, empty SudokuField with the specified numer of rows and columns /// </summary> /// <param name="rows"></param> /// <param name="columns"></param> /// <returns></returns> public static SudokuField CreateNewField(int rows, int columns) { SudokuField newField = new SudokuField(rows, columns); newField.IsNewPuzzle = true; newField.NumRows = rows; newField.NumColumns = columns; if (newField.NumRows > 9) { newField.ContentType = ContentType.HexaDecimal; } //this is an aid to help registering the cells with their section newField.speelveld = new SudokuCell[rows, columns]; for (int rowIndex = 0; rowIndex < rows; rowIndex++) { for (int columnIndex = 0; columnIndex < columns; columnIndex++) { SudokuCell sudokuCell = new SudokuCell(new RegistrationKey(rowIndex, columnIndex)); newField.speelveld[rowIndex, columnIndex] = sudokuCell; } } newField.RegisterField(newField.speelveld); return(newField); }
/// <summary> /// Retrieves a cell by key /// </summary> /// <param name="key"></param> /// <returns></returns> public SudokuCell GetCellByKey(RegistrationKey key) { SudokuCell cell = null; _cells.TryGetValue(key, out cell); return(cell); }
public SudokuCell CreateCell() { SudokuCell cell = new SudokuCell(); cells.Add(cell); return(cell); }
public void SwapArrayCell(ref SudokuCell cell1, ref SudokuCell cell2) { var temp = cell2; cell2 = cell1; cell1 = temp; }
private int CheckIfThereIsUniqueRectangleOnRows(SudokuCell firstCell, SudokuCell secondCell) { var row = firstCell.Row; var secondRow = secondCell.Row; for (int col = 0; col < 9; col++) { if (sudokuDifficulty.sudoku[row, col].Equals(firstCell) || sudokuDifficulty.sudoku[row, col].Value != 0 || sudokuDifficulty.sudoku[row, col].PossibleValues.Count != 2) { continue; } var tempList = sudokuDifficulty.sudoku[row, col].PossibleValues.Where(x => firstCell.PossibleValues.Contains(x)).ToList(); if (tempList.Count == 2) { if (sudokuDifficulty.sudoku[secondRow, col].PossibleValues.Count == 2 && sudokuDifficulty.sudoku[secondRow, col].Value == 0) { tempList = sudokuDifficulty.sudoku[secondRow, col].PossibleValues.Where(x => firstCell.PossibleValues.Contains(x)).ToList(); if (tempList.Count == 2) { return(col); } } } } return(-1); }
private bool IsValuePossibleToBeInCell(IEnumerable <SudokuCell> cells, SudokuCell cell, int value) { if (cell == null || cell.Value.HasValue) { throw new ArgumentException("cell"); } // value exist in a block if (cells.Count(c => c.Block == cell.Block && c.Value.HasValue && c.Value.Value == value) > 0) { return(false); } // horizontal check if (GetValues(cells, cell, Orientation.Horizontal).Contains(value)) { return(false); } // vertical check if (GetValues(cells, cell, Orientation.Vertical).Contains(value)) { return(false); } return(true); }
private int FindHiddenSingle(Sudoku p_sudoku, SudokuCell p_sudokuCell) { IList <SudokuCell> row = p_sudoku.GetRow(p_sudokuCell.Row); IList <SudokuCell> column = p_sudoku.GetColumn(p_sudokuCell.Column); IList <SudokuCell> square = p_sudoku.GetSquare(p_sudokuCell.Row, p_sudokuCell.Column); int uniqueCandidateValueRow = SudokuOperations.GetUniqueCandidateValue(row); if (uniqueCandidateValueRow != 0) { return(uniqueCandidateValueRow); } int uniqueCandidateValueColumn = SudokuOperations.GetUniqueCandidateValue(column); if (uniqueCandidateValueColumn != 0) { return(uniqueCandidateValueColumn); } int uniqueCandidateValueSquare = SudokuOperations.GetUniqueCandidateValue(square); if (uniqueCandidateValueSquare != 0) { return(uniqueCandidateValueSquare); } return(0); }
private int FindNakedSingleInternal(SudokuCell p_sudokuCell, IList <SudokuCell> p_row, IList <SudokuCell> p_column, IList <SudokuCell> p_square) { IList <int> candidates = new List <int>(); for (int i = 0; i < p_row.Count; i++) { bool isMissingInRow = p_row.All(p_cell => p_cell.Value != i + 1); bool isMissingInColumn = p_column.All(p_cell => p_cell.Value != i + 1); bool isMissingInSquare = p_square.All(p_cell => p_cell.Value != i + 1); if (isMissingInRow && isMissingInColumn && isMissingInSquare) { candidates.Add(i + 1); } } if (candidates.Count == 1) { return(candidates.FirstOrDefault()); } p_sudokuCell.AddCandidates(candidates); return(0); }
private bool Validate(int[,] data, SudokuCell cell, int value) { for (int i = 0; i < BlockCount; i++) { // Check row if (i != cell.Y && data[cell.X, i] == value) { return(false); } // Check column if (i != cell.X && data[i, cell.Y] == value) { return(false); } } int xStartPosition = BlockHelper.GetBlockStartPosition(cell.X, BlockRowCount); int xEndPosition = BlockHelper.GetBlockEndPosition(cell.X, BlockRowCount); int yStartPosition = BlockHelper.GetBlockStartPosition(cell.Y, BlockRowCount); int yEndPosition = BlockHelper.GetBlockEndPosition(cell.Y, BlockRowCount); // Check sector for (int i = xStartPosition; i < xEndPosition; i++) { for (int j = yStartPosition; j < yEndPosition; j++) { if (i != cell.X && j != cell.Y && data[i, j] == value) { return(false); } } } return(true); }
private void FindAllCandidates(int[,] data) { _cellCandidatesDictionary = new Dictionary <SudokuCell, List <int> >(); for (int i = 0; i < BlockCount; i++) { for (int j = 0; j < BlockCount; j++) { if (data[i, j] == 0) { for (int k = 1; k <= BlockCount; k++) { if (Validate(data, new SudokuCell(i, j), k)) { SudokuCell newCell = new SudokuCell(i, j); if (!_cellCandidatesDictionary.ContainsKey(newCell)) { _cellCandidatesDictionary.Add(newCell, new List <int>()); } if (!_cellCandidatesDictionary[newCell].Contains(k)) { _cellCandidatesDictionary[newCell].Add(k); } } } } } } }
public SudokuCell[,] CreateBaseRow() { int[] TempRowWorkIndexes = new int[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //SudokuCell[] ResultRow = new SudokuCell[9]; SudokuCell[,] ResultRow = new SudokuCell[1, 9] { { new SudokuCell(), new SudokuCell(), new SudokuCell(), new SudokuCell(), new SudokuCell(), new SudokuCell(), new SudokuCell(), new SudokuCell(), new SudokuCell() } }; Random RandomSeed = new Random(); int ItemIndx; for (int Count = 0; Count < 9; Count++) { ItemIndx = RandomSeed.Next(0, TempRowWorkIndexes.Length - 1); ResultRow[0, Count].SudokuNumber = TempRowWorkIndexes[ItemIndx]; //ResultRow[Count].IsVisible = true; TempRowWorkIndexes = UpdateIndxArray(TempRowWorkIndexes, ItemIndx); } return(ResultRow); }
public SudokuCell CreateCell(RegistrationKey key) { SudokuCell cell = new SudokuCell(key); cells.Add(cell); return(cell); }
public void TrySetInvalidValue() { SudokuCell cell = new SudokuCell(); Assert.IsFalse(cell.TrySet(-1, false)); Assert.IsFalse(cell.TrySet(10, false)); }
public static SudokuCell BaseCellToBruteForceCell(SudokuCell cell) { if (cell == null) { return(null); } if (cell is BruteForceAlgoCell) { return((BruteForceAlgoCell)cell); } BruteForceAlgoCell retCell = null; if (cell.Value != 0) { retCell = new BruteForceAlgoCell(cell.Value); // se il valore è stato definito, non serve propagare gli AllowableValues. } else { retCell = new BruteForceAlgoCell(); // propago eventuali AllowableValues. Questo ottimizza il lavoro dell'algoritmo BruteForce. if (cell is LogicalBasedAlgoCell) { retCell.AllowableValues = new List <int>(((LogicalBasedAlgoCell)cell).AllowableValues); } } return(retCell); }
private bool RemoveNumberFromCols(SudokuCell firstCell, SudokuCell secondCell, int number) { var result = false; var col = firstCell.Col; for (int row = 0; row < 9; row++) { if (sudokuDifficulty.sudoku[row, col].Equals(firstCell) || sudokuDifficulty.sudoku[row, col].Equals(secondCell)) { continue; } if (sudokuDifficulty.sudoku[row, col].Value == 0) { sudokuDifficulty.sudoku[row, col].PossibleValues.Remove(number); if (sudokuDifficulty.sudoku[row, col].PossibleValues.Count == 1) { sudokuDifficulty.sudoku[row, col].Value = sudokuDifficulty.sudoku[row, col].PossibleValues[0]; sudokuDifficulty.sudoku[row, col].PossibleValues.Clear(); sudokuDifficulty.counter++; result = true; } } } return(result); }
/// <summary> /// Turns the jagged byte array array into a SudokuRow array. /// </summary> public static SudokuRow[] GenerateSudokuGridFromBoard(byte[][] sudokuBoard) { if (!IsSudokuBoardValid(sudokuBoard)) { throw new ArgumentException(InvalidSudokuBoardMessage); } var sudokuGrid = new SudokuRow[9]; for (int i = 0; i < 9; i++) { sudokuGrid[i] = new SudokuRow(); for (int j = 0; j < 9; j++) { if (sudokuBoard[i][j] == 0) { sudokuGrid[i][j] = new SudokuCell(null, false);; } else { sudokuGrid[i][j] = new SudokuCell(sudokuBoard[i][j], true); } } } return(sudokuGrid); }
private SudokuCell CheckIfNumberIsFoundOnlyTwoTimesOnRow(int number, SudokuCell sudokuCell) { var listWithCells = new List <SudokuCell>(); var row = sudokuCell.Row; for (int col = 0; col < 9; col++) { if (sudokuDifficulty.sudoku[row, col].Equals(sudokuCell)) { continue; } if (sudokuDifficulty.sudoku[row, col].Value == 0) { if (sudokuDifficulty.sudoku[row, col].PossibleValues.Contains(number)) { listWithCells.Add(sudokuDifficulty.sudoku[row, col]); } } } if (listWithCells.Count == 1) { return(listWithCells[0]); } return(null); }
private RegistrationKey CreateBlockKey(SudokuCell cell) { int rowIndex = (int)(cell.Key.RowIndex / Math.Sqrt(NumRows)); int columnindex = (int)(cell.Key.ColumnIndex / Math.Sqrt(NumColumns)); return(new RegistrationKey(rowIndex, columnindex)); }
public SudokuCell[,] ShiftSudokuCells(SudokuCell[,] OldRow, int NumShifts) { SudokuCell[,] TempRow = new SudokuCell[1, 9]; Array.Copy(OldRow, NumShifts - 1, TempRow, 0, OldRow.Length - (NumShifts - 1)); Array.Copy(OldRow, 0, TempRow, (OldRow.Length - (NumShifts - 1)), (NumShifts - 1)); return(TempRow); }
public void SetCell(SudokuCell cell) { this.cell = cell; this.label1.BackColor = Control.DefaultBackColor; this.cell.DuplicateEvent += cell_DuplicateEvent; this.cell.ValueChanged += cell_ValueChanged; this.cell_ValueChanged(null, null); }
public PuzzleCellBox(SudokuCell cell) { InitializeComponent(); context = SynchronizationContext.Current; this.textBox1.KeyUp += textBox1_KeyUp; this.textBox1.KeyDown += textBox1_KeyDown; SetCell(cell); }
public void DisallowSetIsChangeableFromFalseToTrue() { SudokuCell cell = new SudokuCell(); // once we set up cell from file or array and say its NOT changeable, we cannot set it back to be changeable Assert.IsTrue(cell.TrySet(1, false)); Assert.IsFalse(cell.TrySet(0, true)); }
public void TrySetValueToNotInitiallySetCell() { SudokuCell cell = new SudokuCell(); // overwriting cell value is allowed, provided the flag is set to changeable Assert.IsTrue(cell.TrySet(1, true)); Assert.IsTrue(cell.TrySet(9, true)); }
public void TrySetValueToCellThatAlreadyHasValue() { // overwriting existing cell is allowed, providing it is not set as initialized from array or file SudokuCell cell = new SudokuCell(); Assert.IsTrue(cell.TrySet(1, false)); Assert.IsFalse(cell.TrySet(1, false)); }
public void TrySetValueToInitiallySetCell() { SudokuCell cell = new SudokuCell(); // overwriting cell value SET initially from file or array is NOT allowed Assert.IsTrue(cell.TrySet(1, false)); Assert.IsFalse(cell.TrySet(0, false)); }
public SudokuBoard(UIElementCollection coll) { string file = @"g:\1.txt"; try { sudokee = sm.LoadFromFile(file); } catch (Exception) { MessageBox.Show("Error during loading.", "Error"); } cell = new SudokuCell(coll,sudokee.BaseSize,this); }
private void Dump(SudokuCell[][] cells) { foreach (var cellCollection in cells) { StringBuilder stringBuilder = new StringBuilder(); foreach (var cell in cellCollection) { if (stringBuilder.Length != 0) Console.Write(","); Console.Write(cell.ToString()); } Console.WriteLine(); } Console.WriteLine("------------------------------------------------------------------"); }
private void InitializeComponent() { int x = this._PanelOffsetX; int y = this._PanelOffsetY; for (int i = 1; i <= 9; i++) { x = this._PanelOffsetX; for (int j = 1; j <= 9; j++) { SudokuCell squares = new SudokuCell(i - 1, j - 1, this._BoxWidth, this.BackColor); squares.Location = new Point(x, y); this.DoTable[i - 1, j - 1] = squares.DoCell; this._SudokuCellArray[i - 1, j - 1] = squares; squares.ParentPanel = this; squares.TextChanged += new EventHandler(Squares_TextChanged); this.Controls.Add(squares); x = x + squares.Width; if (j == 9) { x = x + this._PanelOffsetX; y = y + squares.Height; } } } }
void cellValueChanged(SudokuCell sender, int newValue) { IEnumerable<SudokuCell> cellsInScope = GetCellsInScope(sender); List<int> values = GetValuesInCells(cellsInScope.ToArray()); if (values.Contains(newValue)) { if (HighlightOnError) { sender.HighlightError(); } } else { sender.HighlightDefault(); } }
public IEnumerable<SudokuCell> GetCellsInScope(SudokuCell Cell) { List<SudokuCell> result = new List<SudokuCell>(); result.AddRange( GetBlockScope(Cell.Block) ); result.AddRange( GetRowScope(Cell.Row) ); result.AddRange( GetColumnScope(Cell.Column) ); result = result.Distinct().ToList(); result.Remove(Cell); return result; }
public void OnValueChanged(SudokuCell Source, int NewValue) { if (ValueChanged != null) { ValueChanged.Invoke(Source, NewValue); } }
public int EliminateCandidatesInScopeOfCell(SudokuCell Cell) { int eliminatedTotal = EliminateCandidatesInScope(GetRegionScope(SudokuRegion.Row, Cell.Row)); eliminatedTotal += EliminateCandidatesInScope(GetRegionScope(SudokuRegion.Column, Cell.Column)); eliminatedTotal += EliminateCandidatesInScope(GetRegionScope(SudokuRegion.Block, Cell.Block)); return eliminatedTotal; }
protected abstract char SelectCandidateValue(SudokuCell CandidateCell);