def test_inner_walls(self): # Create maze n_rows = 4 n_cols = 7 start = '2,0w' end = '0,3n' walls = ['2,3w', '2,4w', '2,5w'] maze = Maze(n_rows, n_cols, start, end, walls) maze._apply_valid_move_constraint() # Apply constraint to populate csp variables # Check to see that walls appear as variables in maze.csp for wall in walls: self.assertTrue(wall in maze.csp.variables) # Fix wall values maze._set_inner_walls() # Check to see that walls are fixed; they would no longer appear as variables for wall in walls: self.assertFalse(wall in maze.csp.variables) # Verify that walls are fixed to 0 # circle_solution surrounds the inner walls, if walls are fixed to 1, solution will fail valid_move_constraint. # Since start and end locations have not been fixed, a circle is valid path circle_solution = {'2,2n': 1, '3,2n': 1, '3,3w': 1, '3,4w': 1, '3,5w': 1, '3,5n': 1, '2,5n': 1, '1,5w': 1, '1,4w': 1, '1,3w': 1} fill_with_zeros(circle_solution, n_rows, n_cols) # No ignore_list because we didn't fix start and end self.assertTrue(maze.csp.check(circle_solution))
def test_valid_move_constraint(self): n_rows = 2 n_cols = 3 start = '0,1n' end = '0,3w' maze = Maze(n_rows, n_cols, start, end, ['1,2n', '1,2w']) maze._apply_valid_move_constraint() # No path at all, not even at start or end locations no_path_solution = {} fill_with_zeros(no_path_solution, n_rows, n_cols) self.assertTrue(maze.csp.check(no_path_solution)) # Broken path broken_path_solution = {'1,0n': 1, '1,1w': 1} fill_with_zeros(broken_path_solution, n_rows, n_cols) self.assertFalse(maze.csp.check(broken_path_solution)) # Revisiting a tile 0,1 revisit_tile_solution = {'0,1w': 1, '1,0n': 1, '1,1w': 1, '1,1n': 1, '0,2w': 1, start: 1, end: 1} fill_with_zeros(revisit_tile_solution, n_rows, n_cols) self.assertFalse(maze.csp.check(revisit_tile_solution)) # Good path good_path_solution = {'0,2w': 1, start: 1, end: 1} fill_with_zeros(good_path_solution, n_rows, n_cols) self.assertTrue(good_path_solution)
def test_borders_constraint(self): # Create maze n_rows = 3 n_cols = 3 start = '0,1n' end = '0,3w' maze = Maze(n_rows, n_cols, start, end, ['1,1w', '2,1n']) maze._apply_valid_move_constraint() # Apply constraint to populate csp variables maze._set_start_and_end() # Start and end locations should not be considered as borders # Grab border variables borders = {get_label(i, 0, 'w') for i in range(n_rows)} # West border borders.update({get_label(i, n_cols, 'w') for i in range(n_rows)}) # East border borders.update({get_label(0, j, 'n') for j in range(n_cols)}) # North border borders.update({get_label(n_rows, j, 'n') for j in range(n_cols)}) # South border borders.remove(start) borders.remove(end) # Check to see that border appears as a variable in maze.csp for border in borders: self.assertTrue(border in maze.csp.variables) # Fix borders maze._set_borders() # Check to see that borders are fixed; they would no longer appear as variables for border in borders: self.assertFalse(border in maze.csp.variables) # Verify that borders are fixed to 0. # If borders were fixed to 1, this good_path_solution would cause the valid_move_constraint to fail. good_path_solution = {'0,2w': 1} fill_with_zeros(good_path_solution, n_rows, n_cols, [start, end]) self.assertTrue(maze.csp.check(good_path_solution))
def test_set_start_and_end(self): # Create maze n_rows = 5 n_cols = 3 start = '4,3w' end = '0,2n' maze = Maze(n_rows, n_cols, start, end, []) maze._apply_valid_move_constraint() # Apply constraint to populate csp variables # Check to see that start and end are in csp.variables self.assertTrue(start in maze.csp.variables) self.assertTrue(end in maze.csp.variables) # Check to see that start and end have been fixed maze._set_start_and_end() self.assertFalse(start in maze.csp.variables) self.assertFalse(end in maze.csp.variables) # Check that start and end are fixed to 1. # If start and end were fixed to 0, valid_move_constraint would be satisfied with the no_path_solution. no_path_solution = {} fill_with_zeros(no_path_solution, n_rows, n_cols, [start, end]) self.assertFalse(maze.csp.check(no_path_solution))