def test_actions_and_results(): wh = Warehouse() # read the puzzle from the multiline string wh.extract_locations(puzzle_t2.split(sep='\n')) print(wh) sp = SokobanPuzzle(wh) print(sp.actions(wh))
def test_solve_sokoban_macro(): wh = Warehouse() wh.extract_locations(puzzle_t3.split(sep='\n')) print(wh) answer = solve_sokoban_macro(wh) assert (answer == [((2, 3), 'Right'), ((2, 4), 'Right'), ((3, 3), 'Left'), ((3, 2), 'Left')])
def test_taboo_cells(n): problem_file = "./warehouses/warehouse_%s.txt" % str(n) wh = Warehouse() wh.read_warehouse_file(problem_file) answer = taboo_cells(wh) print(set(find_2D_iterator(answer.split('\n'), "X"))) print(answer)
def test_taboo_cells(n): problem_file = "./warehouses/warehouse_%s.txt" % str(n) print("Testing:", problem_file) wh = Warehouse() wh.load_warehouse(problem_file) answer = taboo_cells(wh) print(answer)
def test_can_go_there(): puzzle_t1 = '#######\n#@ $. #\n#######' wh = Warehouse() wh.extract_locations(puzzle_t1.split(sep='\n')) # first test answer = can_go_there(wh, (1, 2)) expected_answer = True fcn = test_can_go_there print('<< First test of {} >>'.format(fcn.__name__)) if answer == expected_answer: print(fcn.__name__, ' passed! :-)\n') else: print(fcn.__name__, ' failed! :-(\n') print('Expected ') print(expected_answer) print('But, received ') print(answer) # second test answer = can_go_there(wh, (1, 5)) expected_answer = False print('<< Second test of {} >>'.format(fcn.__name__)) if answer == expected_answer: print(fcn.__name__, ' passed! :-)\n') else: print(fcn.__name__, ' failed! :-(\n') print('Expected ') print(expected_answer) print('But, received ') print(answer)
def test_solve_comparison(): problem_file = "./warehouses/warehouse_35.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) print(wh) bfs = True elem = True macro = True if (bfs): sp = SokobanPuzzle(wh) start = time.time() answer = breadth_first_graph_search(sp).solution() end = time.time() print("BFS Elem took:", end - start, "secs") print(answer) if (elem): start = time.time() answer = solve_sokoban_elem(wh) end = time.time() print("A* elem took:", end - start, "secs") print(answer) if (macro): start = time.time() answer = solve_sokoban_macro(wh) end = time.time() print("A* macro took:", end - start, "secs") print(answer)
def __init__(self, master=None): tk.Frame.__init__(self, master) self.grid() self.configure(background="black") self.master.title("Sokoban v%s" % (__version__)) self.master.resizable(0,0) self.image_dict={'wall':tk.PhotoImage(file=os.path.join(_ROOT, 'images/wall.gif')), 'target':tk.PhotoImage(file=os.path.join(_ROOT, 'images/hole.gif')), 'box_on_target':tk.PhotoImage(file=os.path.join(_ROOT, 'images/crate-in-hole.gif')), 'box':tk.PhotoImage(file=os.path.join(_ROOT, 'images/crate.gif')), 'worker':tk.PhotoImage(file=os.path.join(_ROOT, 'images/player.gif')), 'smiley':tk.PhotoImage(file=os.path.join(_ROOT, 'images/smiley.gif')), 'worker_on_target':tk.PhotoImage(file=os.path.join(_ROOT, 'images/player-in-hole.gif')), } icon = self.image_dict['box'] self.warehouse_symbol = { 'wall':'#' , 'target':'.' , 'box_on_target': '*' , 'box':'$' , 'worker':'@', 'worker_on_target': '!', 'floor' : ' '} self.direction_offset = {'Left' :(-1,0), 'Right':(1,0) , 'Up':(0,-1), 'Down':(0,1)} # (x,y) = (column,row) self.master.tk.call('wm', 'iconphoto', self.master._w, icon) self.create_menu() self.DEFAULT_SIZE = 200 self.frame = tk.Frame(self, height=self.DEFAULT_SIZE, width=self.DEFAULT_SIZE) self.frame.grid() self.default_frame() self.cells={} # dict with key (x,y) and value Label widget to keep track of the Labels in the grid self.level_file_name = None self.warehouse = Warehouse()
def test_check_elem_action_seq(): wh = Warehouse() wh.load_warehouse("./warehouses/warehouse_01.txt") # first test answer = check_elem_action_seq(wh, ['Right', 'Right', 'Down']) expected_answer = '#### \n# .# \n# ###\n#* #\n# $@#\n# ###\n#### ' fcn = test_check_elem_action_seq print('<< First test of {} >>'.format(fcn.__name__)) if answer == expected_answer: print(fcn.__name__, ' passed! :-)\n') else: print(fcn.__name__, ' failed! :-(\n') print('Expected ') print(expected_answer) print('But, received ') print(answer) # second test answer = check_elem_action_seq(wh, ['Right', 'Right', 'Right']) expected_answer = 'Impossible' fcn = test_check_elem_action_seq print('<< Second test of {} >>'.format(fcn.__name__)) if answer == expected_answer: print(fcn.__name__, ' passed! :-)\n') else: print(fcn.__name__, ' failed! :-(\n') print('Expected ') print(expected_answer) print('But, received ') print(answer)
def test_warehouse_1(): wh = Warehouse() # read the puzzle from the multiline string wh.extract_locations(puzzle_t2.split(sep='\n')) print("\nPuzzle from multiline string") print(wh) print(actions(wh))
def test_check_elem_action_seq(): problem_file = "./warehouses/warehouse_01.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) print(wh) answer = check_action_seq(wh, ['Right', 'Right', 'Down', 'Left']) print(answer) assert (answer == expected_answer_1)
def test_taboo_cells(): problem_file = "./warehouses/warehouse_171.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) # wh.extract_locations(problem_file.split(sep='\n')) answer = taboo_cells(wh) print(answer) assert (answer == expected_answer_3)
def test_warehouse_2(): problem_file = "./warehouses/warehouse_01.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) print("\nPuzzle from file") print(wh) print(wh.worker) # x,y coords !! print(wh.walls) # x,y coords !!
def test_elem(n): problem_file = "./warehouses/warehouse_%s.txt" % str(n) print(n, ": ", end="") wh = Warehouse() wh.load_warehouse(problem_file) time1 = time.time() solve_sokoban_elem(wh) print('{:06.3f}s'.format(time.time() - time1))
def test_weighted(n): problem_file = "./warehouses/warehouse_%s.txt" % str(n) print(n, ": ", end="") wh = Warehouse() wh.load_warehouse(problem_file) time1 = time.time() solve_weighted_sokoban_elem(wh, [1 for box in wh.boxes]) print('{:06.3f}s'.format(time.time() - time1))
def test_can_go_there(): problem_file = "./warehouses/warehouse_01.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) answer = can_go_there(wh, (30, 2)) assert (answer == False) answer = can_go_there(wh, (6, 2)) assert (answer == True)
def test_solve_sokoban_elem(): problem_file = "./warehouses/warehouse_27.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) # wh.extract_locations(puzzle_t1.split(sep='\n')) print(wh) print('\nElementary solution') answer = solve_sokoban_elem(wh) print(answer)
def test_expected(n, expected): wh = Warehouse() # removes unneccessary \n wh.from_lines(n.split(sep='\n')) answer = solve_sokoban_macro(wh) if answer == expected: print('Test Passed') else: print("Test Failed")
def test(n): problem_file = "./warehouses/warehouse_%s.txt" % str(n) print("Testing:", problem_file) wh = Warehouse() wh.load_warehouse(problem_file) time1 = time.time() answer = solve_sokoban_elem(wh) print(time.time() - time1) print(answer)
def test_macro_search(): wh = Warehouse() wh.extract_locations(puzzle_t2.split(sep='\n')) print(str(wh)) goal = str(wh).replace("$", " ").replace(".", "*") h = lambda n: 1 node = iterative_deepening_astar(SokobanPuzzle(str(wh), goal), h, 15) print(str(node))
def test_can_go_there(): problem_file = "./warehouses/warehouse_01.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) answer = can_go_there(wh, (30, 2)) if answer == False: print("Test_can_go_there() test 1 pass") answer = can_go_there(wh, (6, 2)) if answer == True: print("Test_can_go_there() test 2 pass")
def test_can_go_there(): problem_file = "./warehouses/warehouse_02.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) print(wh) answer = can_go_there(wh,(2,6)) #assert( answer == False) answer = can_go_there(wh,(2,6)) #assert( answer == True) print(answer)
def test_search(): problem_file = "./warehouses/warehouse_143.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) print(wh) sp = SokobanPuzzle(wh) start = time.time() node = breadth_first_graph_search(sp) end = time.time() print(node.solution(), "found in", end - start, "seconds")
def test_can_go_there(test, dst, expected): wh = Warehouse() print(test) # removes unneccessary \n wh.from_lines(test.split(sep='\n')) answer = can_go_there(wh, dst) print(wh.worker, '->', dst, '=', answer) if answer is expected: print("Test Passed") else: print("Test Failed")
def test_check_elem_action_seq(): problem_file = "./warehouses/warehouse_01.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) print('Initial state \n', wh ,'\n') answer = check_action_seq(wh, ['Right', 'Right','Down']) if same_multi_line_strings(answer,expected_answer_1): print('Test check_elem_action_seq passed\n') else: print('** Test check_elem_action_seq failed\n')
def test_can_go_there(): problem_file = "./warehouses/warehouse_01.txt" wh = Warehouse() wh.read_warehouse_file(problem_file) print(wh) print(wh.worker) answer = can_go_there(wh,(30,2)) assert( answer == False) answer = can_go_there(wh,(6,4)) assert( answer == True) print('We did it!')
def test_taboo(test, expected): wh = Warehouse() print(test) # removes unneccessary \n wh.from_lines(test.split(sep='\n')) answer = taboo_cells(wh) print(answer) if same_multi_line_strings(answer, expected): print('Test Passed') else: print("Test Failed")
def print_taboo_spaces(warehouse_id): """ Calculates the taboo cells for the given warehouse id and prints them @param warehouse_id: ID of warehouse to load taboo spaces for """ problem_file = "./warehouses/warehouse_{:02d}.txt".format(warehouse_id) wh = Warehouse() wh.load_warehouse(problem_file) print(wh) print("TABOO CELLS: ") taboo = taboo_cells(wh) print(taboo)
def test_solve_sokoban_elem(): # problem_file = "./warehouses/warehouse_01.txt" wh = Warehouse() # wh.read_warehouse_file(problem_file) wh.extract_locations(puzzle_t1.split(sep='\n')) print(wh) print('\nElementary solution') answer = solve_sokoban_elem(wh) print(answer) if answer == ['Right', 'Right']: print('Test solve_sokoban_elem passed\n') else: print('** Test solve_sokoban_elem failed\n')
def test_taboo_cells(): wh = Warehouse() wh.load_warehouse("./warehouses/warehouse_01.txt") expected_answer = '#### \n#X # \n# ###\n# X#\n# X#\n#XX###\n#### ' answer = taboo_cells(wh) fcn = test_taboo_cells print('<< Testing {} >>'.format(fcn.__name__)) if answer==expected_answer: print(fcn.__name__, ' passed! :-)\n') else: print(fcn.__name__, ' failed! :-(\n') print('Expected ');print(expected_answer) print('But, received ');print(answer)
def test_solve_sokoban_elem(): puzzle_t1 = '#######\n#@ $. #\n#######' wh = Warehouse() wh.from_string(puzzle_t1) # first test answer = solve_sokoban_elem(wh) expected_answer = ['Right', 'Right'] fcn = test_solve_sokoban_elem print('<< First test of {} >>'.format(fcn.__name__)) if answer == expected_answer: print(fcn.__name__, ' passed! :-)\n') else: print(fcn.__name__, ' failed! :-(\n') print('Expected ') print(expected_answer) print('But, received ') print(answer) # second test puzzle_t2 = '#######\n#@ $ #.#\n#######' wh = Warehouse() wh.from_string(puzzle_t2) # second test answer = solve_sokoban_elem(wh) expected_answer = 'Impossible' print('<< Second test of {} >>'.format(fcn.__name__)) if answer == expected_answer: print(fcn.__name__, ' passed! :-)\n') else: print(fcn.__name__, ' failed! :-(\n') print('Expected ') print(expected_answer) print('But, received ') print(answer)