async def solver(puzzle, solution): domain = Sokoban() while True: game_properties = await puzzle.get() mapa = Map(game_properties["map"]) domain.set_mapa(mapa) p = SearchProblem(domain, mapa) t = SearchTree(p, 'a*') while True: await asyncio.sleep( 0) # this should be 0 in your code and this is REQUIRED break keys = await t.search() print(game_properties["map"]) print('terminals: {} non-terminals: {} depth: {}'.format( t.terminals, t.non_terminals, t.length)) print(len(keys), keys) await solution.put(keys)
def main(): f = open("board.txt") boardStr = f.read() sokoban = Sokoban(Sokoban.fromBoardString(boardStr)) root = Node(sokoban) print("Solving the provided puzzle...") node, count = root.astar() i = 1 if node is None: print('Puzzle had no solution.') else: print("Found a solution!\n") stack = node.from_top() moves = len(stack) w = open("moves.txt", "w") while stack: move = stack.pop() w.write(f"{move.move}\n") print(f'Move {i}: {move.move}') i += 1 w.close() print( f"\n{str(moves)} were required. \n{str(count)} nodes were visited." )
def test_skip(self): _, mock_view, _ = self.make_all_mocks() mock_level0 = Mock() mock_levels = [mock_level0] game = Sokoban(mock_levels) game.handle_key(Key.SKIP) self.assertEqual(mock_view.setup_world.call_count, 2)
def test_quit(self): _, mock_view, _ = self.make_all_mocks() mock_level0 = Mock() mock_levels = [mock_level0] game = Sokoban(mock_levels) game.handle_key(Key.QUIT) mock_view.quit.assert_called_with()
def test_game_over(self): mock_engine, mock_view, _ = self.make_all_mocks() mock_level0 = Mock() mock_levels = [mock_level0] game = Sokoban(mock_levels) mock_engine.is_game_over.return_value = True game.handle_key(Key.UP) self.assertEqual(mock_view.setup_world.call_count, 2)
def test_move_right(self): mock_engine, mock_view, mock_world = self.make_all_mocks() mock_level0 = Mock() mock_levels = [mock_level0] game = Sokoban(mock_levels) game.handle_key(Key.RIGHT) mock_engine.move.assert_called_with(Dir.RT, mock_world) mock_view.show_world.assert_called_with(mock_world)
async def solver(puzzle, solution): domain = Sokoban() while True: game_properties = await puzzle.get() mapa = Map(game_properties["map"]) p = SearchProblem(domain, mapa) t = SearchTree(p, 'greedy') while True: await asyncio.sleep( 0) # this should be 0 in your code and this is REQUIRED break keys = await t.search() # print(keys) await solution.put(keys)
from utils import GFrame from sokoban import Sokoban from sokoban import TileMap from argparse import ArgumentParser if __name__ == "__main__": parser = ArgumentParser() parser.add_argument("-m", "--map", help="Map to run on", type=str, required=False, default="test") args = parser.parse_args() frame = GFrame("Sokoban") frame.display(Sokoban(30, args.map)) frame.run() # Runs the Frame + Game
import pygame, os from pygame.locals import * from sokoban import Sokoban from direction import Direction from spot import Spot import solve from spritesheet import Spritesheet from button import Button W, H = 400, 400 window = pygame.display.set_mode((W, H)) game = Sokoban() level_name = 'levels/level.xsb' board = game.new_board(level_name) G_SIZE = int(min(W, H) / max(board.width, board.height)) sheet = Spritesheet() green = (20, 236, 136) blue = (72, 136, 240) L = Direction(Spot(-1, 0), 'l') R = Direction(Spot(1, 0), 'r') U = Direction(Spot(0, -1), 'u') D = Direction(Spot(0, 1), 'd') directions = [U, D, L, R] solve_button = Button((W - 60, 10, 50, 20), "Solve", blue) restart_button = Button((W - 60, 40, 50, 20), "Restart", green) def draw_player(board, window, last_dir):
b.setText(' O ') self.grid.addWidget(b, i, j) @staticmethod def __cell_color(cell_type): if cell_type == CellType.wall: return 'background-color:#A40' elif cell_type == CellType.box: return 'background-color:#0F0' elif cell_type == CellType.goal: return 'background-color:#F00' elif cell_type == CellType.done: return 'background-color:#FF0' elif cell_type == CellType.empty: return 'background-color:#FA0' designer = 'designer.ui' if __name__ == '__main__': file_path = 'asd.txt' sokoban = Sokoban(file_path) app = Qt.QApplication(sys.argv) w = Window() w.show() for model in sokoban.__iter__(): w.view(model.matrix, model.position) Qt.QTest.qWait(1000) sys.exit(app.exec_())
import sys from sokoban import Sokoban ''' Tests search algos Handles command line and user input ''' def runSearch(s, filename, option): ''' Runs the search based on filename and option selected ''' b = s.new_board(filename) print '\nSolving ' + filename + '...' s.doSearches(b, option) sok = Sokoban() print "Which algorithm?" print "1) Breadth first search" # print "2) Depth first search" print "3) Uniform cost search" # print "4) Greedy best first search" # print "5) A* search" print "6) all" p = raw_input("Type a number and press enter: ") option = int(p) # gets file from args and plays that puzzle if len(sys.argv) == 2: runSearch(sok, sys.argv[1], option) else: