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
def _setup(self): self._frame = GFrame("Sokoban Simulator") self._game = Sokoban(30, self._map, True) self._frame.display(self._game)
from tetris import Tetris from utils import GFrame if __name__ == "__main__": frame = GFrame("Tetris") frame.display(Tetris(60, "standard")) frame.run() # Runs the Frame + Game
class Simulator(): def __init__(self, map="fake", wait_time=1): self._map = map self._wait_time = wait_time self._verbose = False def _setup(self): self._frame = GFrame("Sokoban Simulator") self._game = Sokoban(30, self._map, True) self._frame.display(self._game) def verbose(self, b): self._verbose = b def swap_map(self, map_file): self._map = map_file def _simulate(self, agent): action = agent.request_action(self._game.get_state()) if self._verbose: print("Move: ", action) self._game.move_player(action) self._game.move_enemies() return action def _simulate_tree_search(self, agent, record): if self._verbose: print("#------------STARTING SIMULATION------------#") # We change the way its scored # The agent just needs to turn on ONE switch by stepping on it self._game.get_state().update_obtained_points( 5) # Give the player boots (ability to activate switches) # Loop while no switches are activated while all(not t for t in self._game.get_state().get_switches().values()): sleep(self._wait_time) record.append(self._simulate(agent)) if self._verbose: print("#------------SIMULATION FINISHED------------#") self._game.set_done(True) def _simulate_probability_agent(self, agent, sense_func): if self._verbose: print("#------------STARTING SIMULATION------------#") # Simulate the given actions every "speed" seconds while self._game.get_state().get_mouse_locations(): sleep(self._wait_time) sense_func(self._game.get_state()) self._simulate(agent) self._game.set_done(True) if self._verbose: print("#------------SIMULATION FINISHED------------#") def simulate_generic_agent(self, agent): self._setup() # Quick Hack to help out when students haven't implemented a function yet try: agent.request_action(None) except AttributeError: pass record = [] # Setup the simulation in its own thread simulation_thread = Thread( target=lambda: self._simulate_tree_search(agent, record), daemon=True) simulation_thread.start() # Run the game and frame self._frame.run() return record def simulate_probability_agent(self, agent, sense_func): self._setup() # Quick Hack to help out when students haven't implemented a function yet try: sense_func(None) except AttributeError: pass # Setup the simulation in its own thread simulation_thread = Thread( target=lambda: self._simulate_probability_agent(agent, sense_func), daemon=True) simulation_thread.start() # Run the game and frame self._frame.run() return self._game.get_state().get_score()
class Simulator(): def __init__(self, map="sim_test", wait_time=1): self._map = map self._wait_time = wait_time def _setup(self): self._frame = GFrame("Sokoban Simulator") self._game = Sokoban(30, self._map, True) self._frame.display(self._game) def swap_map(self, map): self._map = map def swap_wait_time(self, wait_time): self._wait_time = wait_time def _simulate(self, actions): print("#------------STARTING SIMULATION------------#") # Simulate the given actions every "speed" seconds for action in actions: sleep(self._wait_time) print("Move: ", action) self._game.move_enemies() self._game.move_player(action) print("#------------SIMULATION FINISHED------------#") self._game.set_done(True) def _simulate_agent(self, agent): print("#------------STARTING SIMULATION------------#") # Simulate the given actions every "speed" seconds while self._game.get_state().get_mouse_locations(): sleep(self._wait_time) action = agent.request_action(self._game.get_state()) print("Move: ", action) self._game.move_player(action) self._game.move_enemies() self._game.set_done(True) print("#------------SIMULATION FINISHED------------#") def _simulate_listening_agent(self, agent): print("#------------STARTING SIMULATION------------#") # Simulate the given actions every "speed" seconds while self._game.get_state().get_mouse_locations(): sleep(self._wait_time) agent.listen(self._game.get_state()) action = agent.request_action(self._game.get_state()) print("Move: ", action) self._game.move_player(action) self._game.move_enemies() self._game.set_done(True) print("#------------SIMULATION FINISHED------------#") def _simulate_prediction_agent(self, agent): print("#------------STARTING SIMULATION------------#") # Simulate the given actions every "speed" seconds remaining_mice = self._game.get_state().get_mouse_locations() while remaining_mice: sleep(self._wait_time) agent.predict(self._game.get_state()) action = agent.request_action(self._game.get_state()) print("Move: ", action) self._game.move_player(action) self._game.move_enemies() print("#------------SIMULATION FINISHED------------#") self._game.set_done(True) def simulate(self, actions): self._setup() # Setup the simulation in its own thread simulation_thread = Thread(target=self._simulate, args = (actions, ), daemon=True) simulation_thread.start() # Run the game and frame self._frame.run() def simulate_agent(self, agent): self._setup() # Setup the simulation in its own thread simulation_thread = Thread(target=self._simulate_agent, args = (agent, ), daemon=True) simulation_thread.start() # Run the game and frame self._frame.run() def simulate_generic(self, agent): self._setup() # Setup the simulation in its own thread simulation_thread = Thread(target=self._simulate_agent, args = (agent, ), daemon=True) simulation_thread.start() # Run the game and frame self._frame.run() def simulate_listening_agent(self, agent): self._setup() # Setup the simulation in its own thread simulation_thread = Thread(target=self._simulate_listening_agent, args = (agent, ), daemon=True) simulation_thread.start() # Run the game and frame self._frame.run() def simulate_prediction_agent(self, agent): self._setup() # Setup the simulation in its own thread simulation_thread = Thread(target=self._simulate_prediction_agent, args = (agent, ), daemon=True) simulation_thread.start() # Run the game and frame self._frame.run()