|
| 1 | +import random |
| 2 | +import math |
| 3 | +import copy |
| 4 | + |
| 5 | +class Sudoku: |
| 6 | + def __init__(self, N, E): |
| 7 | + self.N = N |
| 8 | + self.E = E |
| 9 | + |
| 10 | + # compute square root of N |
| 11 | + self.SRN = int(math.sqrt(N)) |
| 12 | + self.table = [[0 for x in range(N)] for y in range(N)] |
| 13 | + self.answerable_table = None |
| 14 | + |
| 15 | + self._generate_table() |
| 16 | + |
| 17 | + def _generate_table(self): |
| 18 | + # fill the subgroups diagonally table/matrices |
| 19 | + self.fill_diagonal() |
| 20 | + |
| 21 | + # fill remaining empty subgroups |
| 22 | + self.fill_remaining(0, self.SRN) |
| 23 | + |
| 24 | + # Remove random Key digits to make game |
| 25 | + self.remove_digits() |
| 26 | + |
| 27 | + def fill_diagonal(self): |
| 28 | + for x in range(0, self.N, self.SRN): |
| 29 | + self.fill_cell(x, x) |
| 30 | + |
| 31 | + def not_in_subgroup(self, rowstart, colstart, num): |
| 32 | + for x in range(self.SRN): |
| 33 | + for y in range(self.SRN): |
| 34 | + if self.table[rowstart + x][colstart + y] == num: |
| 35 | + return False |
| 36 | + return True |
| 37 | + |
| 38 | + def fill_cell(self, row, col): |
| 39 | + num = 0 |
| 40 | + for x in range(self.SRN): |
| 41 | + for y in range(self.SRN): |
| 42 | + while True: |
| 43 | + num = self.random_generator(self.N) |
| 44 | + if self.not_in_subgroup(row, col, num): |
| 45 | + break |
| 46 | + self.table[row + x][col + y] = num |
| 47 | + |
| 48 | + def random_generator(self, num): |
| 49 | + return math.floor(random.random() * num + 1) |
| 50 | + |
| 51 | + def safe_position(self, row, col, num): |
| 52 | + return (self.not_in_row(row, num) and self.not_in_col(col, num) and self.not_in_subgroup(row - row % self.SRN, col - col % self.SRN, num)) |
| 53 | + |
| 54 | + def not_in_row(self, row, num): |
| 55 | + for col in range(self.N): |
| 56 | + if self.table[row][col] == num: |
| 57 | + return False |
| 58 | + return True |
| 59 | + |
| 60 | + def not_in_col(self, col, num): |
| 61 | + for row in range(self.N): |
| 62 | + if self.table[row][col] == num: |
| 63 | + return False |
| 64 | + return True |
| 65 | + |
| 66 | + |
| 67 | + def fill_remaining(self, row, col): |
| 68 | + # check if we have reached the end of the matrix |
| 69 | + if row == self.N - 1 and col == self.N: |
| 70 | + return True |
| 71 | + |
| 72 | + # move to the next row if we have reached the end of the current row |
| 73 | + if col == self.N: |
| 74 | + row += 1 |
| 75 | + col = 0 |
| 76 | + |
| 77 | + # skip cells that are already filled |
| 78 | + if self.table[row][col] != 0: |
| 79 | + return self.fill_remaining(row, col + 1) |
| 80 | + |
| 81 | + # try filling the current cell with a valid value |
| 82 | + for num in range(1, self.N + 1): |
| 83 | + if self.safe_position(row, col, num): |
| 84 | + self.table[row][col] = num |
| 85 | + if self.fill_remaining(row, col + 1): |
| 86 | + return True |
| 87 | + self.table[row][col] = 0 |
| 88 | + |
| 89 | + # no valid value was found, so backtrack |
| 90 | + return False |
| 91 | + |
| 92 | + def remove_digits(self): |
| 93 | + count = self.E |
| 94 | + |
| 95 | + # replicates the table so we can have a filled and pre-filled copy |
| 96 | + self.answerable_table = copy.deepcopy(self.table) |
| 97 | + |
| 98 | + # removing random numbers to create the puzzle sheet |
| 99 | + while (count != 0): |
| 100 | + row = self.random_generator(self.N) - 1 |
| 101 | + col = self.random_generator(self.N) - 1 |
| 102 | + if (self.answerable_table[row][col] != 0): |
| 103 | + count -= 1 |
| 104 | + self.answerable_table[row][col] = 0 |
| 105 | + |
| 106 | + |
| 107 | + def puzzle_table(self): |
| 108 | + return self.answerable_table |
| 109 | + |
| 110 | + def puzzle_answers(self): |
| 111 | + return self.table |
| 112 | + |
| 113 | + |
| 114 | + def printSudoku(self): |
| 115 | + for row in range(self.N): |
| 116 | + for col in range(self.N): |
| 117 | + print(self.table[row][col], end=" ") |
| 118 | + print() |
| 119 | + |
| 120 | + print("") |
| 121 | + |
| 122 | + for row in range(self.N): |
| 123 | + for col in range(self.N): |
| 124 | + print(self.answerable_table[row][col], end=" ") |
| 125 | + print() |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + N = 9 |
| 130 | + E = (N * N) // 2 |
| 131 | + sudoku = Sudoku(N, E) |
| 132 | + sudoku.printSudoku() |
0 commit comments