def test_get_number_of_queens_on_board(self): size = 5 queens = (XYLocation(1, 1), XYLocation(2, 4), XYLocation(3, 3)) nqb = NQueensBoard(size) nqb.set_board(queens) self.assertEqual(3, nqb.get_number_of_queens_on_board())
def test_get_number_of_attacking_pairs_v(self): size = 5 queens = (XYLocation(1, 3), XYLocation(1, 4), XYLocation(1, 0)) nqb = NQueensBoard(size) nqb.set_board(queens) self.assertEqual(3, nqb.get_number_of_attacking_pairs())
def test_move_queen(self): nqb = NQueensBoard(5) nqb.add_queen_at(XYLocation(3, 4)) nqrf = NQResultFunction() new_board = nqrf.result( nqb, QueenAction(QueenAction.MOVE_QUEEN, XYLocation(3, 1))) self.assertFalse(new_board.queen_exists_at_square(4, 3)) self.assertTrue(new_board.queen_exists_at_square(1, 3))
def test_remove_queen_action(self): queens = (XYLocation(1, 3), XYLocation(1, 4), XYLocation(1, 0)) nqb = NQueensBoard(5) nqb.set_board(queens) nqrf = NQResultFunction() new_board = nqrf.result( nqb, QueenAction(QueenAction.REMOVE_QUEEN, XYLocation(1, 4))) self.assertEquals(2, new_board.get_number_of_queens_on_board())
def test_actions(self): nqb = NQueensBoard(3) nqb.add_queen_at(XYLocation(0, 0)) nqiaf = NQIActionsFunctions() actions = nqiaf.actions(nqb) expected_actions = [ QueenAction(QueenAction.PLACE_QUEEN, XYLocation(1, 2)), QueenAction(QueenAction.PLACE_QUEEN, XYLocation(2, 1)) ] self.assertSameElements(expected_actions, actions)
def actions(self, board): actions = [] for r in range(board.size): for c in range(board.size): location = XYLocation(c, r) if not board.is_square_under_attack( location) and not board.queen_exists_at(location): actions.append( QueenAction(QueenAction.PLACE_QUEEN, XYLocation(c, r))) return actions
def test_get_state(self): string = "23041" conv = NQueensConverter(5) expected_board = NQueensBoard(5) expected_board.add_queen_at(XYLocation(0, 2)) expected_board.add_queen_at(XYLocation(1, 3)) expected_board.add_queen_at(XYLocation(2, 0)) expected_board.add_queen_at(XYLocation(3, 4)) expected_board.add_queen_at(XYLocation(4, 1)) self.assertEqual(expected_board, conv.get_state(string))
def test_place_queen(self): nqb = NQueensBoard(5) nqrf = NQResultFunction() new_board = nqrf.result( nqb, QueenAction(QueenAction.PLACE_QUEEN, XYLocation(1, 2))) self.assertTrue(new_board.queen_exists_at_square(2, 1))
def create_random_board(): board = NQueensBoard(SIZE) for c in range(SIZE): r = randint(0, SIZE - 1) board.add_queen_at(XYLocation(c, r)) return board
def get_state(self, string): board = NQueensBoard(self.size) for c in range(len(string)): r = int(string[c]) board.add_queen_at(XYLocation(c, r)) return board
def actions(self, board): actions = [] for r in range(board.size): for c in range(board.size): location = XYLocation(c, r) if not board.queen_exists_at(location): actions.append( QueenAction(QueenAction.MOVE_QUEEN, location)) return actions
def get_queen_positions(self): """ Get positions of queens on a board :return (list of XYLocation): list of locations of queens """ locations = [] for r in range(0, self.size): for c in range(0, self.size): if self.squares[r][c] == self.QUEEN: locations.append(XYLocation(c, r)) return locations
def get_unmarked_positions(self): """ Return coordinates of not empty positions :return list(XYPosition): coordinates of not empty positions on the board. """ result = [] for r in range(3): for c in range(3): if self.is_empty(r, c): result.append(XYLocation(c, r)) return result
def test_remove_queen_from(self): nqb = NQueensBoard(5) nqb.add_queen_at(XYLocation(3, 4)) nqb.remove_queen_from(XYLocation(3, 4)) self.assertEqual(0, len(nqb.get_queen_positions()))
def test_add_queen_at(self): nqb = NQueensBoard(5) nqb.add_queen_at(XYLocation(3, 4)) self.assertTrue(nqb.queen_exists_at_square(4, 3))
def test_get_unmarked_positions(self): ttb = TicTacToeBoard() expected_positions = [ XYLocation(0, 0), XYLocation(0, 1), XYLocation(0, 2), XYLocation(1, 0), XYLocation(1, 1), XYLocation(1, 2), XYLocation(2, 0), XYLocation(2, 1), XYLocation(2, 2) ] self.assertSameElements(expected_positions, ttb.get_unmarked_positions()) ttb.markO(1, 2) ttb.markX(0, 0) ttb.markO(2, 2) expected_positions = [ XYLocation(0, 1), XYLocation(0, 2), XYLocation(1, 0), XYLocation(1, 1), XYLocation(1, 2), XYLocation(2, 0) ] self.assertSameElements(expected_positions, ttb.get_unmarked_positions())