Beispiel #1
0
def read(f: Union[IOBase, str],
         board: sdk_board.Board = None) -> sdk_board.Board:
    """Read a Sudoku board from a file.  Pass in a path
    or an already opened file.  Optionally pass in a board to be
    filled.
    """
    if isinstance(f, str):
        log.debug("Reading from string")
        f = open(f, "r")
    else:
        log.debug(f"Reading from file {f}")
    if board is None:
        board = sdk_board.Board()
    values = []
    for row in f:
        row = row.strip()
        log.debug(f"Reading row |{row}|")
        values.append(row)
        if len(row) != NROWS:
            raise InputError("Puzzle row wrong length: {}"
                             .format(row))
    log.debug(f"Read values: {values}")
    if len(values) != NROWS:
        raise InputError("Wrong number of rows in {}"
                         .format(values))
    board.set_tiles(values)
    f.close()
    return board
Beispiel #2
0
 def test_properties(self):
     board = sdk_board.Board()
     board.set_tiles(wikipedia_example)
     self.assertTrue(board.is_consistent())
     self.assertFalse(board.is_solved())
     board.set_tiles(wikipedia_solved)
     duplicate_reports = board.duplicates()
     self.assertEqual(duplicate_reports, [])
     self.assertTrue(board.is_consistent())
     self.assertTrue(board.is_solved())
     board.set_tiles(wikipedia_wrong)
     self.assertFalse(board.is_solved())
     self.assertFalse(board.is_consistent())
Beispiel #3
0
 def test_constructor(self):
     """Constructor creates empty board"""
     board = sdk_board.Board()
     self.assertEqual(len(board.tiles), 9)
     for row in board.tiles:
         self.assertEqual(len(row), 9)
     # Each tile should appear in 3 groups (row, column, block)
     board.set_tiles(naked_single_example)
     self.assertEqual(board.as_list(), naked_single_example)
     three_count = 0
     for group in board.groups:
         for tile in group.tiles:
             if tile.value == '3':
                 three_count += 1
     self.assertEqual(three_count, 3)
Beispiel #4
0
 def test_hidden_single(self):
     board = sdk_board.Board()
     board.set_tiles(hidden_single_example)
     sdk_solver.propagate(board)
     self.assertEqual(board.as_list(), hidden_single_propagated)