def test_removing_a_piece(self): b = Board(4) b.place(0, 1) b.remove(0, 1) assert b.positions[0][1] is False
def test_getting_piece_positions(self): b = Board(4) b.place(0, 2) b.place(1, 1) piece_positions = b.get_piece_positions() assert piece_positions == [(0, 2), (1, 1)]
def test_placing_a_piece(self): b = Board(4) b.place(0, 1) assert b.positions[0][1] is True
def test_board_does_not_have_piece(self): b = Board(4) assert b.has_piece(0, 1) is False
def test_has_piece(self): b = Board(4) b.place(0, 2) assert b.has_piece(0, 2)
def _build_board(self): board = Board(self.size) for position in self.positions: board.place(*position) return board
def __init__(self, size): self.board = Board(size)
class Solver: def __init__(self, size): self.board = Board(size) def solve(self): self._solutions = [] self._place_in_row(0) return self._solutions def _place_in_row(self, row): if row == self.board.size: self._add_solution() else: self._try_in_row(row) def _add_solution(self): piece_positions = self.board.get_piece_positions() solution = Solution(size=self.board.size, positions=piece_positions) self._solutions.append(solution) def _try_in_row(self, row): for col in range(self.board.size): if self._is_valid(row, col): self.board.place(row, col) self._place_in_row(row + 1) self.board.remove(row, col) def _is_valid(self, row, col): return (self._top_is_clear(row, col) and self._top_left_is_clear(row, col) and self._top_right_is_clear(row, col)) def _top_is_clear(self, row, col): row -= 1 while row >= 0: if self.board.has_piece(row, col): return False row -= 1 return True def _top_left_is_clear(self, row, col): row -= 1 col -= 1 while row >= 0 and col >= 0: if self.board.has_piece(row, col): return False row -= 1 col -= 1 return True def _top_right_is_clear(self, row, col): row -= 1 col += 1 while row >= 0 and col < self.board.size: if self.board.has_piece(row, col): return False col += 1 row -= 1 return True