Esempio n. 1
0
    def __init__(self, rollout_len=100, all_moves=False, levels=None, random_state=None):
        self.rollout_len = rollout_len
        self.random_state = random_state
        self.all_moves = all_moves
        self.levels = levels or Match3Levels(LEVELS)
        self.h = self.levels.h
        self.w = self.levels.w
        self.n_shapes = self.levels.n_shapes
        self.__episode_counter = 0

        self.__game = Game(
            rows=self.h,
            columns=self.w,
            n_shapes=self.n_shapes,
            length=3,
            all_moves=all_moves,
            random_state=self.random_state)
        self.reset()
        self.renderer = Renderer(self.n_shapes)

        # setting observation space
        self.observation_space = spaces.Box(
            low=0,
            high=self.n_shapes,
            shape=self.__game.board.board_size,
            dtype=int)

        # setting actions space
        self.__match3_actions = self.__get_available_actions()
        self.action_space = spaces.Discrete(
            len(self.__match3_actions))
Esempio n. 2
0
 def setUp(self):
     self.level = Level(5, 6, 3, [
         [-1, -1, 0],
         [0, 0, -1],
         [0, 0, -1]
     ])
     self.m3levels = Match3Levels([self.level], -1)
Esempio n. 3
0
    def __init__(self, immovable_move, n_of_match_counts_immov,
                 match_counts_add_immovable, number_of_match_counts_add_immovable,
                 step_add_immovable, number_of_step_add_immovable,
                 no_legal_actions_do, immovable_shape=-1,
                 rollout_len=-1, all_moves=False, levels=None, random_state=None):
        self.no_legal_actions_do = no_legal_actions_do
        self.rollout_len = rollout_len
        self.random_state = random_state
        self.all_moves = all_moves
        self.levels = levels or Match3Levels(LEVELS)
        self.h = self.levels.h
        self.w = self.levels.w
        self.n_shapes = self.levels.n_shapes
        self.__episode_counter = 0

        self.__game = Game(rows=self.h, columns=self.w, n_shapes=self.n_shapes, length=3,
                           immovable_move=immovable_move,
                           n_of_match_counts_immov=n_of_match_counts_immov,
                           match_counts_add_immovable=match_counts_add_immovable,
                           number_of_match_counts_add_immovable=number_of_match_counts_add_immovable,
                           step_add_immovable=step_add_immovable,
                           number_of_step_add_immovable=number_of_step_add_immovable,
                           no_legal_actions_do=no_legal_actions_do,
                           all_moves=all_moves,
                           immovable_shape=immovable_shape,
                           random_state=self.random_state)
        self.reset()
        self.renderer = Renderer(self.n_shapes)

        # setting observation space
        self.observation_space = spaces.Box(
            low=0,
            high=self.n_shapes,
            shape=self.__game.board.board_size,
            dtype=int)

        # setting actions space
        self.__match3_actions = self.get_available_actions()
        self.action_space = spaces.Discrete(
            len(self.__match3_actions))
Esempio n. 4
0

env = Match3Env(
    step_add_immovable=parser.getboolean('gym_environment',
                                         'step_add_immovable'),
    number_of_step_add_immovable=int(
        parser.get('gym_environment', 'number_of_step_add_immovable')),
    match_counts_add_immovable=parser.getboolean('gym_environment',
                                                 'match_counts_add_immovable'),
    number_of_match_counts_add_immovable=int(
        parser.get('gym_environment', 'number_of_match_counts_add_immovable')),
    train_or_test=parser.get('gym_environment', 'train_or_test'),
    rollout_len=int(parser.get('gym_environment', 'rollout_len')),
    levels=Match3Levels(
        Getlevels(
            int(parser.get('gym_environment', 'board_width_and_hight')),
            int(
                parser.get('gym_environment',
                           'board_number_of_different_color')))),
    immovable_move_=parser.getboolean('gym_environment', 'immovable_move'),
    n_of_match_counts_immov=int(
        parser.get('gym_environment', 'number_of_immovable_add')),
    no_legal_shuffle_or_new_=parser.get('gym_environment',
                                        'no_legal_shuffle_or_new'))

available_actions = {
    v: k
    for k, v in dict(enumerate(env.get_available_actions())).items()
}

reward_list = []
for i_episode in range(10):  #玩 1次遊戲
Esempio n. 5
0
def Getlevels(WidthAndHeight, shapes):
    import numpy as np
    from gym_match3.envs.levels import Level
    new_level = [Level(WidthAndHeight, WidthAndHeight, shapes, np.zeros((WidthAndHeight, WidthAndHeight)).tolist())]
    return Match3Levels(new_level)
Esempio n. 6
0
config = ConfigParser()
parser = ConfigParser(interpolation=ExtendedInterpolation())
parser.read('configure.ini')

width_hight = int(parser.get('gym_environment', 'board_width_and_hight'))
n_shapesss = int(
    parser.get('gym_environment', 'board_number_of_different_color'))


def Getlevels(WnH, shapes):
    LEVELS = [Level(WnH, WnH, shapes, np.zeros((WnH, WnH)).tolist())]
    return LEVELS


env = Match3Env(levels=Match3Levels(Getlevels(width_hight, n_shapesss)))


class One_hot(gym.ObservationWrapper):
    def __init__(self, env=None):
        gym.ObservationWrapper.__init__(self, env)

    def observation(self, observation):
        observation = observation.reshape(width_hight, width_hight).astype(int)
        grid_onehot = np.zeros(shape=(env.n_shapes, width_hight, width_hight))
        table = {i: i for i in range(-1, n_shapesss)}

        for i in range(width_hight):
            for j in range(width_hight):
                grid_element = observation[i][j]
                grid_onehot[table[grid_element]][i][j] = 1
Esempio n. 7
0
    def __init__(self,
                 step_add_immovable,
                 number_of_step_add_immovable,
                 match_counts_add_immovable,
                 number_of_match_counts_add_immovable,
                 immovable_move_,
                 n_of_match_counts_immov,
                 train_or_test,
                 no_legal_shuffle_or_new_,
                 rollout_len=100,
                 all_moves=True,
                 levels=None,
                 random_state=None):

        self.step_add_immovable = step_add_immovable
        self.number_of_step_add_immovable = number_of_step_add_immovable
        self.match_counts_add_immovable = match_counts_add_immovable
        self.number_of_match_counts_add_immovable = number_of_match_counts_add_immovable
        self.train_or_test = train_or_test
        self.rollout_len = rollout_len
        self.immovable_move = immovable_move_
        self.n_of_match_counts_immov = n_of_match_counts_immov
        self.no_legal_shuffle_or_new = no_legal_shuffle_or_new_

        self.random_state = random_state
        self.all_moves = all_moves
        self.levels = levels or Match3Levels(LEVELS)
        self.h = self.levels.h
        self.w = self.levels.w
        self.n_shapes = self.levels.n_shapes
        self.episode_counter = 0
        self.possible_move = random_state

        self.game = Game(
            rows=self.h,
            columns=self.w,
            n_shapes=self.n_shapes,
            length=3,
            all_moves=all_moves,
            random_state=self.random_state,
            no_legal_shuffle_or_new=self.no_legal_shuffle_or_new,
            number_of_match_counts_add_immovable=self.
            number_of_match_counts_add_immovable,
            train_or_test=self.train_or_test,
            filler=Filler(
                immovable_move=self.immovable_move,
                n_of_match_counts_immov=self.n_of_match_counts_immov,
                number_of_match_counts_add_immovable=self.
                number_of_match_counts_add_immovable,
                match_counts_add_immovable=self.match_counts_add_immovable))

        self.reset()[np.newaxis, :]
        self.renderer = Renderer(self.levels.h, self.levels.w, self.n_shapes)

        # setting observation space
        self.observation_space = spaces.Box(low=0,
                                            high=self.n_shapes,
                                            shape=(1, self.h, self.w),
                                            dtype=int)

        # setting actions space
        self.__match3_actions = self.get_available_actions()
        self.action_space = spaces.Discrete(len(self.__match3_actions))