def test_init_grid(self): grid = SudokuGrid() for col in range(grid.GRID_SIZE): for row in range(grid.GRID_SIZE): self.assertTrue( grid.get(col, row) is None, "Element at ({},{}) was not None".format(col, row))
class TestSudokuSolver(unittest.TestCase): def setUp(self): self._grid = SudokuGrid( "349287501000000700000509002200095007001000400800720005100402000008000000000000376" ) self._solver = SudokuSolver(self._grid) def test_00_is_valid(self): self.assertTrue(self._solver.is_valid()) def test_01_is_solved(self): self.assertFalse(self._solver.is_solved()) def test_02_solve_step(self): self._solver.solve_step() self.assertEqual(list(self._grid.get_row(0))[7], 6) self.assertEqual(list(self._grid.get_row(2))[6], 8) self.assertEqual(list(self._grid.get_row(6))[6:], [9, 5, 8]) self.assertEqual(list(self._grid.get_row(7))[8], 4) def test_03_solve(self): sol = self._solver.solve() for i, row in enumerate( ([3, 4, 9, 2, 8, 7, 5, 6, 1], [5, 8, 2, 6, 4, 1, 7, 9, 3], [6, 1, 7, 5, 3, 9, 8, 4, 2], [2, 3, 4, 1, 9, 5, 6, 8, 7], [7, 5, 1, 8, 6, 3, 4, 2, 9], [8, 9, 6, 7, 2, 4, 1, 3, 5], [1, 6, 3, 4, 7, 2, 9, 5, 8], [9, 7, 8, 3, 5, 6, 2, 1, 4])): self.assertEqual(list(sol.get_row(i)), row)
def test_load_empty_grid(self): grid = SudokuGrid( os.path.join(self.THIS_DIR, self.TEST_RESOURCES_DIR, "empty_grid.txt")) for col in range(grid.GRID_SIZE): for row in range(grid.GRID_SIZE): self.assertTrue( grid.get(col, row) is None, "Element at ({},{}) was not None".format(col, row))
def test_00_init(self): with self.assertRaises(ValueError): grid = SudokuGrid("123456xyz" * 9) with self.assertRaises(ValueError): grid = SudokuGrid("123456abc" * 9) with self.assertRaises(ValueError): grid = SudokuGrid("123456") with self.assertRaises(ValueError): grid = SudokuGrid("12345678" * 11)
def test_00_init(self): with self.assertRaises(ValueError): # char grid = SudokuGrid("123456xyz" * 9) with self.assertRaises(ValueError): # hex values grid = SudokuGrid("123456abc" * 9) with self.assertRaises(ValueError): # too short grid = SudokuGrid("123456") with self.assertRaises(ValueError): # too long grid = SudokuGrid("12345678" * 11)
def main(cli_args): grid = SudokuGrid(cli_args.file) print("Grid input:\n{}\n".format(grid)) sudoku_solver = SudokuSolver(grid) solved_grid = sudoku_solver.solve() print("Solved grid:\n{}\n".format(solved_grid))
def start(): chx = 4 while chx not in (1, 2): chx = int( input( "Souhaiter vous entrer votre propre sudoku ou aller chercher cette grille dans un fichier ? (\"1\", \"2\") : " )) if chx == 2: fichier = input( "Veuillez saisir le nom du fichier (dans le meme dossier que les scripts) : " ) ligne = input("Veullez saisir la ligne à lire dans le fichier : ") Sudoku = SudokuGrid.from_file(fichier, int(ligne)) else: Sudoku = SudokuGrid.from_stdin() return Sudoku
def solve_all(running_times): for l in range(1, 245): g = SudokuGrid.from_file("../sudoku_db.txt", l) start = time.monotonic() solver = SudokuSolver(g) solver.solve() running_times.append(1000 * (time.monotonic() - start)) print("\r[{: <40}] ({:.0%})".format('=' * int(40 * l / 244), l / 244), end='')
def test_subregion_constraints(self): grid = SudokuGrid() grid.set(1, 1, 1) for (x, y) in [(0, 0), (0, 2), (2, 0), (2, 2)]: try: grid.set(x, y, 1) self.fail( "Trying to assign a 1 to ({}, {}) should have resulted in an exception" .format(x, y)) except SudokuError: pass
def test_col_constraints(self): grid = SudokuGrid() grid.set(0, 0, 1) for y in range(grid.GRID_SIZE): try: grid.set(0, y, 1) self.fail( "Trying to assign a 1 to ({}, {}) should have resulted in an exception" .format(0, y)) except SudokuError: pass
class TestSudokuGrid(unittest.TestCase): def setUp(self): self._grid = SudokuGrid( "349000000000000700000509002200095007001000400800720005100402000008000000000000376" ) def test_00_init(self): with self.assertRaises(ValueError): grid = SudokuGrid("123456xyz" * 9) with self.assertRaises(ValueError): grid = SudokuGrid("123456abc" * 9) with self.assertRaises(ValueError): grid = SudokuGrid("123456") with self.assertRaises(ValueError): grid = SudokuGrid("12345678" * 11) def test_01_from_file(self): grid = SudokuGrid.from_file("sudoku_db.txt", random.randint(1, 245)) def test_02_from_stdin(self): with unittest.mock.patch('sys.stdin', iter(("123456789" * 9, ))): with unittest.mock.patch('builtins.input', return_value="123456789" * 9): grid = SudokuGrid.from_stdin() def test_03_str(self): grid_str = str(self._grid) def test_04_get_row(self): self.assertEqual(list(self._grid.get_row(1)), [0, 0, 0, 0, 0, 0, 7, 0, 0]) self.assertEqual(list(self._grid.get_row(4)), [0, 0, 1, 0, 0, 0, 4, 0, 0]) def test_05_get_col(self): self.assertEqual(list(self._grid.get_col(4)), [0, 0, 0, 9, 0, 2, 0, 0, 0]) self.assertEqual(list(self._grid.get_col(5)), [0, 0, 9, 5, 0, 0, 2, 0, 0]) def test_06_get_region(self): self.assertEqual(list(self._grid.get_region(0, 0)), [3, 4, 9, 0, 0, 0, 0, 0, 0]) self.assertEqual(list(self._grid.get_region(2, 2)), [0, 0, 0, 0, 0, 0, 3, 7, 6]) def test_07_empty_pos(self): self.assertEqual( set(self._grid.get_empty_pos()), {(7, 3), (4, 7), (1, 3), (4, 8), (5, 6), (6, 6), (8, 0), (7, 7), (0, 7), (2, 1), (6, 2), (3, 7), (0, 3), (5, 1), (8, 5), (4, 0), (1, 2), (6, 7), (3, 3), (5, 5), (8, 1), (7, 6), (4, 4), (1, 5), (3, 6), (2, 2), (0, 4), (4, 1), (1, 1), (6, 4), (3, 2), (2, 6), (8, 2), (7, 1), (4, 5), (1, 4), (7, 5), (0, 5), (1, 0), (0, 8), (2, 7), (7, 8), (8, 3), (7, 0), (6, 8), (6, 1), (3, 1), (5, 7), (7, 4), (0, 6), (1, 8), (2, 0), (4, 3), (1, 7), (5, 2), (2, 4), (8, 4)}) def test_08_write(self): for _ in range(4): i, j = random.choice(list(self._grid.get_empty_pos())) val = random.randint(1, 9) self._grid.write(i, j, val) self.assertEqual(list(self._grid.get_row(i))[j], val) def test_09_copy(self): grid = self._grid.copy() for i in range(9): self.assertEqual(list(grid.get_row(i)), list(self._grid.get_row(i))) i, j = random.choice(list(self._grid.get_empty_pos())) val = random.randint(1, 9) grid.write(i, j, val) self.assertEqual(list(self._grid.get_row(i))[j], 0)
def test_02_from_stdin(self): with unittest.mock.patch('sys.stdin', iter(("123456789" * 9, ))): with unittest.mock.patch('builtins.input', return_value="123456789" * 9): grid = SudokuGrid.from_stdin()
def test_01_from_file(self): grid = SudokuGrid.from_file("sudoku_db.txt", random.randint(1, 245))
def test_01_from_file(self): grid = SudokuGrid.from_file(os.path.join(os.path.dirname(__file__), "..", "sudoku_db.txt"), random.randint(1, 244)) # File not found if instructions not followed while setting up the ws
def setUp(self): self._grid = SudokuGrid( "349000000000000700000509002200095007001000400800720005100402000008000000000000376" )
from grid import SudokuGrid fichier = str(input("Saisissez le nom du fichier : ")) ligne = int(input("Numero de ligne : ")) try: sudoku = SudokuGrid.from_file(fichier, ligne) except: sudoku = SudokuGrid.from_stdin() i = 0 while i == 0: print(sudoku) hor = int(input("Coordonnees de l'horizontale : ")) ver = int(input("Coordonnees de la verticale : ")) val = int(input("Valeur : ")) print(hor) while hor < 0 or hor > 8: print("Saisissez une valeur correcte (entre 0 et 8)") hor = int(input("Coordonnees de l'horizontale : ")) while ver < 0 or ver > 8: print("Saisissez une valeur correcte (entre 0 et 8)") ver = int(input("Coordonnees de la verticale : ")) while val < 1 or val > 9: print("Saisissez une valeur correcte (entre 1 et 9)") val = int(input("Valeur : ")) sudoku.write(ver, hor, val)
def setUp(self): self._grid = SudokuGrid( "349287501000000700000509002200095007001000400800720005100402000008000000000000376" ) self._solver = SudokuSolver(self._grid)
__author__ = 'jdubois' from grid import SudokuGrid from gui import Gui from algorithms import Solver file_to_open = "data/test_easy.txt" file_to_open = "data/test_hard.txt" grille = SudokuGrid(file_to_open) g = Gui() print("Displaying original grid") g.display(grille) Solver.deduction_solve(grille) print("Displaying grid after first logical deductions") g.display(grille) #print("overall possibilities:",sum([len(cell.possibilities) for cell in grille.empty_cells],0),"for a total of",len(grille.empty_cells)) ordered_cells = sorted(grille.empty_cells, key = lambda cell: len(cell.possibilities)) if len(ordered_cells) > 0: print("Trying remaining possibilities on empty cells") Solver.try_to_fill_randomly(grille, ordered_cells = ordered_cells) if grille.isFilled(): print("Displaying final grid") g.display(grille)
import sys from grid import SudokuGrid # python -i src/play_sudoku.py sudoku_db.txt 4 if len(sys.argv) == 3: grid = SudokuGrid.from_file(sys.argv[1], int(sys.argv[2])) else: grid = SudokuGrid.from_stdin() while len(grid.get_empty_pos()): print(grid) row = input("Ligne : ") col = input("Colonne : ") val = input("Valeur : ") input_ok = True if row.isdigit() and col.isdigit(): row = int(row) col = int(col) val = int(val) if not (0 <= row <= 8 and 0 <= col <= 8 and 1 <= val <= 9): input_ok = False if grid.grid[row][col]: input_ok = False else: input_ok = False if not input_ok:
def test_load_single_entry_grid_2(self): grid = SudokuGrid( os.path.join(self.THIS_DIR, self.TEST_RESOURCES_DIR, "single_entry_grid_2.txt")) self.assertTrue(grid.get(8, 8) == 1)
def __init__(self): try: self.grille = SudokuGrid.from_file(sys.argv[1], int(sys.argv[2])) except IndexError: self.grille = SudokuGrid.from_stdin() self.grille_initial = self.grille.copy()
class TestSudokuGrid(unittest.TestCase): def setUp(self): self._grid = SudokuGrid("349000000000000700000509002" \ + "200095007001000400800720005" \ + "100402000008000000000000376") def test_00_init(self): with self.assertRaises(ValueError): # char grid = SudokuGrid("123456xyz" * 9) with self.assertRaises(ValueError): # hex values grid = SudokuGrid("123456abc" * 9) with self.assertRaises(ValueError): # too short grid = SudokuGrid("123456") with self.assertRaises(ValueError): # too long grid = SudokuGrid("12345678" * 11) def test_01_from_file(self): grid = SudokuGrid.from_file(os.path.join(os.path.dirname(__file__), "..", "sudoku_db.txt"), random.randint(1, 244)) # File not found if instructions not followed while setting up the ws def test_02_from_stdin(self): with unittest.mock.patch('builtins.input', return_value="123456789" * 9): grid = SudokuGrid.from_stdin() def test_03_str(self): expected_str = ("349000000", "000000700", "000509002", "200095007", "001000400", "800720005", "100402000", "008000000", "000000376") grid_str = str(self._grid).splitlines() for grid_line, expected_line in zip(expected_str, grid_str): self.assertRegex(grid_line, ".*".join(expected_line.replace("0", "[0 ]"))) def test_04_get_row(self): self.assertEqual(list(self._grid.get_row(1)), [0, 0, 0, 0, 0, 0, 7, 0, 0]) self.assertEqual(list(self._grid.get_row(4)), [0, 0, 1, 0, 0, 0, 4, 0, 0]) def test_05_get_col(self): self.assertEqual(list(self._grid.get_col(4)), [0, 0, 0, 9, 0, 2, 0, 0, 0]) self.assertEqual(list(self._grid.get_col(5)), [0, 0, 9, 5, 0, 0, 2, 0, 0]) def test_06_get_region(self): self.assertEqual(list(self._grid.get_region(0, 0)), [3, 4, 9, 0, 0, 0, 0, 0, 0]) self.assertEqual(list(self._grid.get_region(2, 1)), [4, 0, 2, 0, 0, 0, 0, 0, 0]) def test_07_empty_pos(self): self.assertEqual(set(self._grid.get_empty_pos()), {(7, 3), (4, 7), (1, 3), (4, 8), (5, 6), (6, 6), (8, 0), (7, 7), (0, 7), (2, 1), (6, 2), (3, 7), (0, 3), (5, 1), (8, 5), (4, 0), (1, 2), (6, 7), (3, 3), (5, 5), (8, 1), (7, 6), (4, 4), (1, 5), (3, 6), (2, 2), (0, 4), (4, 1), (1, 1), (6, 4), (3, 2), (2, 6), (8, 2), (7, 1), (4, 5), (1, 4), (7, 5), (0, 5), (1, 0), (0, 8), (2, 7), (7, 8), (8, 3), (7, 0), (6, 8), (6, 1), (3, 1), (5, 7), (7, 4), (0, 6), (1, 8), (2, 0), (4, 3), (1, 7), (5, 2), (2, 4), (8, 4)}) def test_08_write(self): for _ in range(4): i, j = random.choice(list(self._grid.get_empty_pos())) val = random.randint(1, 9) self._grid.write(i, j, val) self.assertEqual(list(self._grid.get_row(i))[j], val) def test_09_copy(self): grid = self._grid.copy() for i in range(9): self.assertEqual(list(grid.get_row(i)), list(self._grid.get_row(i))) i, j = random.choice(list(self._grid.get_empty_pos())) val = random.randint(1, 9) grid.write(i, j, val) self.assertEqual(list(self._grid.get_row(i))[j], 0)
def test_load_easy_sudoku_1(self): grid = SudokuGrid( os.path.join(self.THIS_DIR, self.TEST_RESOURCES_DIR, "easy_sudoku_1.txt")) self.assertEquals(grid.get_valid_numbers(0, 1), {7})