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 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_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 main(): for i in range(0, NUMBER_OF_TESTS): sa = SimulateAnnealingSearch(AttackingPairHeuristic()) random_board = create_random_board() print("Test number: " + str(i)) print("Random init board:") print(random_board) # Create N queen problem with N queens on the board p = Problem(random_board, NQCActionsFunction(), NQResultFunction(), NQueensGoalTest()) # Search for a solution actions = sa.search(p) if not sa.failed(): print("Test succeeded") print("Actions:") for action in actions: print(action) else: print("Search failed") print("Result state:") print(sa.last_state) print("\n\n")
__author__ = 'Ivan Mushketik' ## # Example of solving N-queens problem with help of DFS # Size of the chess board SIZE = 5 # Using tree DFS ts = TreeSearch() dfs = DepthFirstSearch(ts) # Create empty board empty_board = NQueensBoard(SIZE) # Define N-queen board p = Problem(empty_board, NQIActionsFunctions(), NQResultFunction(), NQueensGoalTest()) # Get list of actions to solve a problem actions = dfs.search(p) board_to_show = NQueensBoard(SIZE) rf = NQResultFunction() # Print each action and perform each action (putting new queen on a board) for action in actions: print(action) board_to_show = rf.result(board_to_show, action) # Print result board print(board_to_show)
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))