Example #1
0
 def run(self):
     window = GameWindow("Snake", self._conf, self._map, self, self._on_exit, (
         ('<w>', lambda e: self._update_direc(Direc.UP)),
         ('<a>', lambda e: self._update_direc(Direc.LEFT)),
         ('<s>', lambda e: self._update_direc(Direc.DOWN)),
         ('<d>', lambda e: self._update_direc(Direc.RIGHT)),
         ('<r>', lambda e: self._reset()),
         ('<space>', lambda e: self._toggle_pause())
     ))
     if self._conf.mode == GameMode.NORMAL:
         window.show(self._game_main_normal)
     elif self._conf.mode == GameMode.TRAIN_DQN_GUI:
         window.show(self._game_main_dqn_train)
         self._plot_history()
 def run(self):
     if self._conf.mode == GameMode.BENCHMARK:
         self._run_benchmarks()
     else:
         window = GameWindow(
             "Snake", self._conf, self._map, self, self._on_exit,
             (('<w>', lambda e: self._update_direc(Direc.UP)),
              ('<a>', lambda e: self._update_direc(Direc.LEFT)),
              ('<s>', lambda e: self._update_direc(Direc.DOWN)),
              ('<d>', lambda e: self._update_direc(Direc.RIGHT)),
              ('<r>', lambda e: self._reset()),
              ('<space>', lambda e: self._toggle_pause())))
         if self._conf.mode == GameMode.NORMAL:
             window.show(self._game_main_normal)
Example #3
0
    def _run_benchmarks(self):
        STEPS_LIMIT = 3000
        NUM_EPISODES = int(input("Please input the number of episodes: "))

        print("\nMap size: %dx%d" % (self._conf.map_rows, self._conf.map_cols))
        print("Solver: %s\n" % self._conf.solver_name[:-6].lower())
        tot_suc, tot_suc_steps = 0, 0
        tot_len, tot_steps = 0, 0
        for _ in range(NUM_EPISODES):
            print("Episode %d - " % self._episode, end="")
            window = GameWindow(
                "Snake", self._conf, self._map, self, self._on_exit,
                (('<w>', lambda e: self._update_direc(Direc.UP)),
                 ('<a>', lambda e: self._update_direc(Direc.LEFT)),
                 ('<s>', lambda e: self._update_direc(Direc.DOWN)),
                 ('<d>', lambda e: self._update_direc(Direc.RIGHT)),
                 ('<r>', lambda e: self._reset()),
                 ('<space>', lambda e: self._toggle_pause())))
            while True:
                window.show(self._game_main_normal)
                if self._map.is_full():
                    tot_suc += 1
                    tot_suc_steps += self._snake.steps
                    print("FULL (len: %d | steps: %d)" %
                          (self._snake.len(), self._snake.steps))
                    break
                elif self._snake.dead:
                    print("DEAD (len: %d | steps: %d)" %
                          (self._snake.len(), self._snake.steps))
                    break
                elif self._snake.steps >= STEPS_LIMIT:
                    print("STEP LIMIT (len: %d | steps: %d)" %
                          (self._snake.len(), self._snake.steps))
                    break
            tot_len += self._snake.len()
            tot_steps += self._snake.steps
            self._reset()
        suc_ratio = tot_suc / (self._episode - 1)
        avg_suc_steps = 0
        if tot_suc != 0:
            avg_suc_steps = tot_suc_steps // tot_suc
        avg_len = tot_len / NUM_EPISODES
        avg_steps = tot_steps / NUM_EPISODES
        print(
            "\n[Summary]\nAverage Length: %.2f\nAverage Steps: %.2f\nTotal Runs: %d  Successful Runs: %d (%.2f%%)  \nAvg Successful steps: %d\n"
            % (avg_len, avg_steps, self._episode - 1, tot_suc, 100 * suc_ratio,
               avg_suc_steps))

        self._on_exit()
Example #4
0
 def __init__(self, conf):
     self.__conf = conf
     self.__map = Map(conf.map_rows + 2, conf.map_cols + 2)
     self.__snake = Snake(self.__map, conf.init_direc, conf.init_bodies,
                          conf.init_types)
     self.__pause = False
     self.__window = GameWindow(
         conf, self.__map, "Snake", self.__snake, self.__on_exit,
         (('<w>', lambda e: self.__update_direc(Direc.UP)),
          ('<a>', lambda e: self.__update_direc(Direc.LEFT)),
          ('<s>', lambda e: self.__update_direc(Direc.DOWN)),
          ('<d>', lambda e: self.__update_direc(Direc.RIGHT)),
          ('<r>', lambda e: self.__reset()),
          ('<space>', lambda e: self.__toggle_pause())))
     self.__solver = globals()[self.__conf.solver_name](self.__snake)
     self.__episode = 1
     self.__init_log_file()
Example #5
0
 def __init__(self, conf):
     self.__conf = conf
     self.__map = Map(
         conf.map_rows + 2, conf.map_cols +
         2)  # The extra two rows and columns are for the walls.
     self.__snake = Snake(self.__map, conf.init_direc, conf.init_bodies,
                          conf.init_types)
     self.__pause = False
     self.__window = GameWindow(
         conf, self.__map, "Snake", self.__snake, self.__on_exit,
         (('<w>', lambda e: self.__update_direc(Direc.UP)),
          ('<a>', lambda e: self.__update_direc(Direc.LEFT)),
          ('<s>', lambda e: self.__update_direc(Direc.DOWN)),
          ('<d>', lambda e: self.__update_direc(Direc.RIGHT)),
          ('<r>', lambda e: self.__reset()),
          ('<space>', lambda e: self.__toggle_pause())))
     self.__solver = globals()[self.__conf.solver_name](self.__snake)
     # By importing both the HamiltonSolver and the GreedySolver into the module,
     # we can freely access them from this module using the globals function.
     self.__episode = 1
     # This is for non-gui logging.
     self.__init_log_file()
Example #6
0
def test_game_window():
    game_conf = GameConf()
    game_conf.map_rows = 15
    game_conf.map_cols = game_conf.map_rows
    game_conf.show_grid_line = True
    game_conf.show_info_panel = False

    game_map = Map(game_conf.map_rows + 2, game_conf.map_cols + 2)
    contents = (
        # Walls
        (Pos(1, 6), PointType.WALL),
        (Pos(1, 7), PointType.WALL),
        (Pos(1, 8), PointType.WALL),
        (Pos(1, 9), PointType.WALL),
        (Pos(1, 10), PointType.WALL),
        (Pos(15, 6), PointType.WALL),
        (Pos(15, 7), PointType.WALL),
        (Pos(15, 8), PointType.WALL),
        (Pos(15, 9), PointType.WALL),
        (Pos(15, 10), PointType.WALL),
        (Pos(6, 1), PointType.WALL),
        (Pos(7, 1), PointType.WALL),
        (Pos(8, 1), PointType.WALL),
        (Pos(9, 1), PointType.WALL),
        (Pos(10, 1), PointType.WALL),
        (Pos(6, 15), PointType.WALL),
        (Pos(7, 15), PointType.WALL),
        (Pos(8, 15), PointType.WALL),
        (Pos(9, 15), PointType.WALL),
        (Pos(10, 15), PointType.WALL),
        # Food
        (Pos(4, 6), PointType.FOOD),
        (Pos(4, 10), PointType.FOOD),
        (Pos(6, 4), PointType.FOOD),
        (Pos(10, 4), PointType.FOOD),
        (Pos(6, 12), PointType.FOOD),
        (Pos(10, 12), PointType.FOOD),
        (Pos(12, 6), PointType.FOOD),
        (Pos(12, 10), PointType.FOOD),
        # Top-left
        (Pos(2, 2), PointType.BODY_VER),
        (Pos(3, 2), PointType.BODY_VER),
        (Pos(4, 2), PointType.BODY_UR),
        (Pos(4, 3), PointType.BODY_LU),
        (Pos(3, 3), PointType.BODY_VER),
        (Pos(2, 3), PointType.BODY_RD),
        (Pos(2, 4), PointType.BODY_DL),
        (Pos(2, 4), PointType.BODY_DL),
        (Pos(3, 4), PointType.BODY_VER),
        (Pos(4, 4), PointType.HEAD_D),
        # Top-right
        (Pos(2, 14), PointType.BODY_VER),
        (Pos(3, 14), PointType.BODY_VER),
        (Pos(4, 14), PointType.BODY_LU),
        (Pos(4, 13), PointType.BODY_UR),
        (Pos(3, 13), PointType.BODY_VER),
        (Pos(2, 13), PointType.BODY_DL),
        (Pos(2, 12), PointType.BODY_RD),
        (Pos(3, 12), PointType.BODY_VER),
        (Pos(4, 12), PointType.HEAD_D),
        # Bottom-left
        (Pos(14, 2), PointType.BODY_VER),
        (Pos(13, 2), PointType.BODY_VER),
        (Pos(12, 2), PointType.BODY_RD),
        (Pos(12, 3), PointType.BODY_DL),
        (Pos(13, 3), PointType.BODY_VER),
        (Pos(14, 3), PointType.BODY_UR),
        (Pos(14, 4), PointType.BODY_LU),
        (Pos(13, 4), PointType.BODY_VER),
        (Pos(12, 4), PointType.HEAD_U),
        # Bottom-right
        (Pos(14, 14), PointType.BODY_VER),
        (Pos(13, 14), PointType.BODY_VER),
        (Pos(12, 14), PointType.BODY_DL),
        (Pos(12, 13), PointType.BODY_RD),
        (Pos(13, 13), PointType.BODY_VER),
        (Pos(14, 13), PointType.BODY_LU),
        (Pos(14, 12), PointType.BODY_UR),
        (Pos(13, 12), PointType.BODY_VER),
        (Pos(12, 12), PointType.HEAD_U),
        # Middle
        (Pos(10, 6), PointType.HEAD_L),
        (Pos(10, 7), PointType.BODY_HOR),
        (Pos(10, 8), PointType.BODY_HOR),
        (Pos(10, 9), PointType.BODY_HOR),
        (Pos(10, 10), PointType.BODY_LU),
        (Pos(9, 10), PointType.BODY_VER),
        (Pos(8, 10), PointType.BODY_DL),
        (Pos(8, 9), PointType.BODY_HOR),
        (Pos(8, 8), PointType.BODY_HOR),
        (Pos(8, 7), PointType.BODY_HOR),
        (Pos(8, 6), PointType.BODY_UR),
        (Pos(7, 6), PointType.BODY_VER),
        (Pos(6, 6), PointType.BODY_RD),
        (Pos(6, 7), PointType.BODY_HOR),
        (Pos(6, 8), PointType.BODY_HOR),
        (Pos(6, 9), PointType.BODY_HOR),
        (Pos(6, 10), PointType.HEAD_R)
    )
    for content in contents:
        game_map.point(content[0]).type = content[1]

    GameWindow("Basic Elements", game_conf, game_map).show()