Ejemplo n.º 1
0
    def render(self, mode="console"):
        if self.TESTING and mode == "human":
            mode = "console"

        if mode == "human":
            from time import sleep
            from gym_puyopuyo.rendering import ImageViewer, AnimationState

            if self.anim_state:
                self.anim_state.state.deals[1:] = self.state.deals[:-1]
            else:
                self.anim_state = AnimationState(self.state.clone())

            if not self.viewer:
                self.viewer = ImageViewer(width=self.anim_state.width + 4,
                                          height=self.anim_state.height)

            if self.last_action is not None:
                self.anim_state.state.play_deal(
                    *self.state.actions[self.last_action])
                self.anim_state.state.deals.pop()
                self.anim_state.infer_entities()

            for frame in self.anim_state.resolve():
                self.viewer.render_state(frame)
                sleep(0.05)
            return

        outfile = StringIO() if mode == "ansi" else sys.stdout
        self.state.render(outfile)
        if mode == "ansi":
            return outfile
Ejemplo n.º 2
0
    def render(self, mode: str = "console") -> StringIO:
        if self.TESTING and mode == "human":
            mode = "console"

        if mode == "human":
            from time import sleep
            from gym_puyopuyo.rendering import ImageViewer, AnimationState

            for i in range(len(self.state.players)):
                player = self.state.players[i]
                if self.anim_states[i]:
                    # TODO: Intra step frames
                    # self.anim_states[i].state.deals[1:] = player.deals[:-1]
                    self.anim_states[i].state.deals[:] = player.deals[:-1]
                    self.anim_states[i].state.field.data[:] = player.field.data
                else:
                    self.anim_states[i] = AnimationState(player.clone())
                if self.last_actions[i] is not None:
                    # TODO: Intra step frames
                    # self.anim_states[i].state.play_deal(*player.actions[self.last_actions[i]])
                    self.anim_states[i].infer_entities()

            if not self.viewer:
                self.viewer = ImageViewer(
                    width=(self.anim_states[0].width + 5) *
                    len(self.state.players) - 1,
                    height=self.anim_states[0].height)

            # TODO: Synchronous gravity resolution
            # persistent_frames = [None, None]
            # for frames in zip_longest(*(state.resolve() for state in self.anim_states)):
            #     self.viewer.begin_flip()
            #     for i, frame in enumerate(frames):
            #         if frame:
            #             persistent_frames[i] = frame
            #         self.viewer.render_state(
            #             persistent_frames[i],
            #             x_offset=i * (frame.width + 5),
            #             flip=False
            #         )
            #     self.viewer.end_flip()
            #     sleep(0.05)

            self.viewer.begin_flip()
            for i, frame in enumerate(self.anim_states):
                self.viewer.render_state(frame,
                                         x_offset=i * (frame.width + 5),
                                         flip=False)
            self.viewer.end_flip()
            sleep(0.5)

            return

        outfile = StringIO() if mode == "ansi" else sys.stdout
        self.state.render(outfile)
        if mode == "ansi":
            return outfile
Ejemplo n.º 3
0
class PuyoPuyoEndlessEnv(gym.Env):
    """
    Puyo Puyo environment. Single player endless mode.
    """

    TESTING = False

    metadata = {"render.modes": ["human", "console", "ansi"]}

    def __init__(self, height, width, num_colors, num_deals, tsu_rules=False):
        self.state = State(height,
                           width,
                           num_colors,
                           num_deals,
                           tsu_rules=tsu_rules)
        self.reward_range = (-1, self.state.max_score)

        self.action_space = spaces.Discrete(len(self.state.actions))
        self.observation_space = spaces.Tuple((
            spaces.Box(0,
                       1, (self.state.num_colors, self.state.num_deals, 2),
                       dtype=np.int8),
            spaces.Box(
                0,
                1,
                (self.state.num_colors, self.state.height, self.state.width),
                dtype=np.int8),
        ))
        self.seed()

        self.viewer = None
        self.anim_state = None
        self.last_action = None

    def seed(self, seed=None):
        return [self.state.seed(seed)]

    def reset(self):
        self.state.reset()
        if self.viewer:
            self.anim_state = None
            self.last_action = None
        return self.state.encode()

    def close(self):
        if self.viewer:
            self.viewer.close()

    def render(self, mode="console"):
        if self.TESTING and mode == "human":
            mode = "console"

        if mode == "human":
            from time import sleep
            from gym_puyopuyo.rendering import ImageViewer, AnimationState

            if self.anim_state:
                self.anim_state.state.deals[1:] = self.state.deals[:-1]
            else:
                self.anim_state = AnimationState(self.state.clone())

            if not self.viewer:
                self.viewer = ImageViewer(width=self.anim_state.width + 4,
                                          height=self.anim_state.height)

            if self.last_action is not None:
                self.anim_state.state.play_deal(
                    *self.state.actions[self.last_action])
                self.anim_state.state.deals.pop()
                self.anim_state.infer_entities()

            for frame in self.anim_state.resolve():
                self.viewer.render_state(frame)
                sleep(0.05)
            return

        outfile = StringIO() if mode == "ansi" else sys.stdout
        self.state.render(outfile)
        if mode == "ansi":
            return outfile

    def _step_state(self, state, action, include_observations=True):
        action = self.state.actions[action]
        reward = self.state.step(*action)
        if include_observations:
            return self.state.encode(), reward
        return reward

    def step(self, action):
        self.last_action = action
        observation, reward = self._step_state(self.state, action)
        return observation, reward, (reward < 0), {"state": self.state}

    def get_action_mask(self):
        return self.state.get_action_mask()

    def get_root(self):
        return self.state.clone()

    def read_record(self, file, include_last=False):
        """
        Reads a record and yields observations like step does.

        The actions played are available under the info element.
        """
        initial_state = self.state.clone()
        initial_state.reset()
        for state, action, reward in read_record(file,
                                                 initial_state,
                                                 include_last=include_last):
            info = {
                "state": state,
                "action": state.actions.index(action) if action else None,
            }
            done = True if reward is None else (reward < 0)
            yield state.encode(), reward, done, info
            if done:
                return

    @classmethod
    def permute_observation(cls, observation):
        """
        Permute the observation in-place without affecting which action is optimal
        """
        deals, colors = observation
        deals = np.copy(deals)
        colors = np.copy(colors)

        # Flip deals other than the first one as it affects next action
        for i in range(1, len(deals[0])):
            if random.random() < 0.5:
                for color in range(len(deals)):
                    deals[color][i][0], deals[color][i][1] = deals[color][i][
                        1], deals[color][i][0]

        perm = list(range(len(colors)))
        random.shuffle(perm)
        permute(deals, perm)
        permute(colors, perm)
        return (deals, colors)
Ejemplo n.º 4
0
class PuyoPuyoVersusEnv(gym.Env):
    """
    Puyo Puyo environment. Versus mode.
    """

    TESTING: bool = False

    metadata: Dict[str, List[str]] = {"render.modes": ["human", "ansi"]}

    def __init__(self, opponent, state_params, garbage_clue_weight: int = 0):
        self.opponent = opponent
        self.state: Game = Game(state_params=state_params)
        self.garbage_clue_weight: int = garbage_clue_weight

        self.reward_range: Tuple[int, int] = (-1, 1)

        player: VersusState = self.state.players[0]
        max_steps: int = player.height * player.width
        if not player.tsu_rules:
            max_steps //= 2
        max_score: int = player.max_score + max_steps * player.step_bonus
        player_space: spaces.Dict = spaces.Dict({
            "deals":
            spaces.Box(0,
                       1, (player.num_colors, player.num_deals, 2),
                       dtype=np.int8),
            "field":
            spaces.Box(0,
                       1, (player.num_layers, player.height, player.width),
                       dtype=np.int8),
            "chain_number":
            spaces.Discrete(player.max_chain),
            "pending_score":
            spaces.Discrete(max_score),
            "pending_garbage":
            spaces.Discrete(max_score // player.target_score),
            "all_clear":
            spaces.Discrete(2),
        })
        self.observation_space: spaces.Tuple = spaces.Tuple(
            (player_space, player_space))
        self.action_space: spaces.Discrete = spaces.Discrete(
            len(player.actions))
        self.player: VersusState = player
        self.seed()

        self.viewer = None
        self.anim_states = [None, None]
        self.last_actions: List[Optional[int]] = [None, None]

    def seed(self, seed=None) -> List[int]:
        return [self.state.seed(seed)]

    def reset(self) -> List:
        self.state.reset()
        return self.state.encode()

    def close(self) -> None:
        if self.viewer:
            self.viewer.close()

    def render(self, mode: str = "console") -> StringIO:
        if self.TESTING and mode == "human":
            mode = "console"

        if mode == "human":
            from time import sleep
            from gym_puyopuyo.rendering import ImageViewer, AnimationState

            for i in range(len(self.state.players)):
                player = self.state.players[i]
                if self.anim_states[i]:
                    # TODO: Intra step frames
                    # self.anim_states[i].state.deals[1:] = player.deals[:-1]
                    self.anim_states[i].state.deals[:] = player.deals[:-1]
                    self.anim_states[i].state.field.data[:] = player.field.data
                else:
                    self.anim_states[i] = AnimationState(player.clone())
                if self.last_actions[i] is not None:
                    # TODO: Intra step frames
                    # self.anim_states[i].state.play_deal(*player.actions[self.last_actions[i]])
                    self.anim_states[i].infer_entities()

            if not self.viewer:
                self.viewer = ImageViewer(
                    width=(self.anim_states[0].width + 5) *
                    len(self.state.players) - 1,
                    height=self.anim_states[0].height)

            # TODO: Synchronous gravity resolution
            # persistent_frames = [None, None]
            # for frames in zip_longest(*(state.resolve() for state in self.anim_states)):
            #     self.viewer.begin_flip()
            #     for i, frame in enumerate(frames):
            #         if frame:
            #             persistent_frames[i] = frame
            #         self.viewer.render_state(
            #             persistent_frames[i],
            #             x_offset=i * (frame.width + 5),
            #             flip=False
            #         )
            #     self.viewer.end_flip()
            #     sleep(0.05)

            self.viewer.begin_flip()
            for i, frame in enumerate(self.anim_states):
                self.viewer.render_state(frame,
                                         x_offset=i * (frame.width + 5),
                                         flip=False)
            self.viewer.end_flip()
            sleep(0.5)

            return

        outfile = StringIO() if mode == "ansi" else sys.stdout
        self.state.render(outfile)
        if mode == "ansi":
            return outfile

    def step(self, action: int):
        self.last_actions[0] = action
        root: Game = self.get_root()
        root.players = root.players[::-1]
        opponent_action = self.opponent(root)
        self.last_actions[1] = opponent_action
        acts: List[Tuple[int, int]] = self.player.actions
        reward, garbage, done = self.state.step(
            [acts[action], acts[opponent_action]])
        reward += self.garbage_clue_weight * garbage
        observation: List[Dict[str, np.ndarray]] = self.state.encode()
        return observation, reward, done, {"state": self.state}

    def get_action_mask(self) -> int:
        return self.player.get_action_mask()

    def get_root(self) -> Game:
        return self.state.clone()