public bool InitializeFieldByField(int row, int col, SingleField field) { if (field != null) { int newValue = field.Value; if (newValue < 0 || newValue > 9) { return(false); } if (!CheckIfSafe(Grid, row, col, newValue)) { return(false); } Grid[row, col] = field; return(true); } return(true); }
public bool InitializeFieldByUser(int row, int col, string num) { if (num != "") { int newValue; if (!int.TryParse(num, out newValue)) { return(false); } if (!CheckIfSafe(Grid, row, col, newValue)) { return(false); } Grid[row, col] = new SingleField(newValue, false); return(true); } return(true); }
private void FillField(int row, int col) { int num; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { do { Random rnd = new Random(); num = rnd.Next(1, 10); }while (!IsNumberNotInBox(Grid, row, col, num)); Grid[row + i, col + j] = new SingleField(num, true); } } }
public SudokuGenerator(string _difficulty) { Console.WriteLine(_difficulty); GameLevel result; Enum.TryParse(_difficulty, out result); Difficulty = result; EmptyFieldsSolution = new SingleField[9, 9]; Grid = new SingleField[9, 9]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { Grid[i, j] = new SingleField(0, true); } } UndoStack = new Stack <Tuple <int, int, SingleField, SingleField> >(); RedoStack = new Stack <Tuple <int, int, SingleField, SingleField> >(); }
private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { TextBoxError.Text = ""; TextBox changedField = (TextBox)sender;//validate that it is number; if (changedField.IsFocused) { string num = changedField.Text.ToString(); int lenght = changedField.Name.ToString().Count(); int row = Int32.Parse(changedField.Name[lenght - 2].ToString()); int col = Int32.Parse(changedField.Name[lenght - 1].ToString()); SingleField oldValue = Sudoku.Grid[row, col]; if (!Sudoku.InitializeFieldByUser(row, col, num)) { changedField.Text = ""; TextBoxError.Text = "Not Valid"; } else { SingleField newValue = num == "" ? new SingleField(0, false) : new SingleField(int.Parse(num), false); Sudoku.AddToUndoStack(new Tuple <int, int, SingleField, SingleField>(row, col, oldValue, newValue)); } } }
public SingleField(SingleField _singleField) : this(_singleField.Value, _singleField.Constant) { }
public void ChangeFieldValue(int row, int col, SingleField newValue) { Grid[row, col] = newValue; }