public static Sudoku.coords ReplaceRandom() { var coordinates = new Sudoku.coords(); int missing = hint_initializers(); int k = 0; int rnd_pos = GetRandom(); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (Form1.cp_matrix[i, j] != hint_map[i, j]) { k++; if (k == rnd_pos) { Form1.cp_matrix[i, j] = hint_map[i, j]; coordinates.i = i; coordinates.j = j; return(coordinates); } } } } return(coordinates); }
//backtracking to get the solution of the Sudoku public static void Rezolvare(int[,] matrix, Sudoku.coords coordinates) { if (CheckCompletition(matrix)) { completed = true; return; } for (int l = 1; l <= 9; l++) { if (Checking(matrix, coordinates, l)) { matrix[coordinates.i, coordinates.j] = l; if (completed == false) { Rezolvare(matrix, GetEmptyField(matrix)); } if (completed == false) { matrix[coordinates.i, coordinates.j] = 0; } } } }
//method to return the first empty field public static Sudoku.coords GetEmptyField(int [,] game_board) { var field = new Sudoku.coords(); for (field.i = 0; field.i < 9; field.i++) { for (field.j = 0; field.j < 9; field.j++) { if (game_board[field.i, field.j] == 0) { return(field); } } } return(field); }
private void button82_Click(object sender, EventArgs e) { //The hint Button var coordinates = new Sudoku.coords(); coordinates = Hint.ReplaceRandom(); Hint.hint_uses++; buttons[coordinates.i, coordinates.j].Text = cp_matrix[coordinates.i, coordinates.j].ToString(); buttons[coordinates.i, coordinates.j].BackColor = Color.AliceBlue; if (Hint.CheckCompletition(cp_matrix)) { Form3 f3 = new Form3(); Hide(); stopwatch.Stop(); f3.ShowDialog(); Close(); return; } }
//checking if the number entered is correct public static bool Checking(int [,] matrix, Sudoku.coords coordinates, int value) { bool row = true, collumn = true, square = true; //checking row for (int k = 0; k < 9; k++) { if (matrix[coordinates.i, k] == value) { row = false; } } //checking collumn for (int k = 0; k < 9; k++) { if (matrix[k, coordinates.j] == value) { collumn = false; } } //checking square int ii = (coordinates.i / 3) * 3; int jj = (coordinates.j / 3) * 3; for (int p = ii; p < ii + 3; p++) { for (int q = jj; q < jj + 3; q++) { if (matrix[p, q] == value) { square = false; } } } return(row & collumn & square); }