示例#1
0
def start(path: str) -> Environment:
    """ Starts a TextWorld environment to play a game.

    Args:
        path: Path to the game file.

    Returns:
        TextWorld environment running the provided game.

    """
    # Check the game file exists.
    if not os.path.isfile(path):
        msg = "Unable to find game '{}'.".format(os.path.abspath(path))
        raise IOError(msg)

    # Guess the backend from the extension.
    backend = "glulx" if path.endswith(".ulx") else "zmachine"

    if backend == "zmachine":
        env = JerichoEnvironment(path)
    elif backend == "glulx":
        env = GlulxEnvironment(path)
    else:
        msg = "Unsupported backend: {}".format(backend)
        raise ValueError(msg)

    return env
示例#2
0
def start(filename: str) -> Environment:
    """ Starts a TextWorld environment to play a game.

    Args:
        filename: Path to the game file.

    Returns:
        TextWorld environment running the provided game.

    """
    # Check the game file exists.
    if not os.path.isfile(filename):
        # Maybe it refers to a file in "games" directory.
        tentative = pjoin(DEFAULT_GAMES_REPOSITORY, filename)

        if not os.path.isfile(tentative):
            msg = "Unable to find game: '{}' or '{}'"
            msg = msg.format(os.path.abspath(filename),
                             os.path.abspath(tentative))
            raise IOError(msg)

        filename = tentative

    # Guess the backend from the extension.
    backend = "glulx" if filename.endswith(".ulx") else "zmachine"

    if backend == "zmachine":
        if os.path.basename(filename) in CUSTOM_ENVIRONMENTS:
            env = CUSTOM_ENVIRONMENTS[os.path.basename(filename)](filename)
        elif JerichoEnvironment:
            env = JerichoEnvironment(filename)
        else:
            env = FrotzEnvironment(filename)

    elif backend == "glulx":
        env = GlulxEnvironment(filename)
    else:
        msg = "Unsupported backend: {}".format(backend)
        raise ValueError(msg)

    return env