{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from glob import glob\n", "from os.path import join as pjoin\n", "\n", "import gym\n", "import textworld.gym\n", "from textworld import EnvInfos\n", "\n", "GAMES_PATH = \"sample_games\" # This assumes `sample_games.zip` was first unzipped.\n", "gamefiles = glob(pjoin(GAMES_PATH, \"*.ulx\"))\n", "print(\"Found {} games.\".format(len(gamefiles)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Playing a game " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "gamefile = gamefiles[0] # Pick a game.\n", "\n", "requested_infos = EnvInfos(description=True, inventory=True, extras=[\"recipe\", \"walkthrough\"])\n", "env_id = textworld.gym.register_games([gamefile], requested_infos)\n", "\n", "agent = textworld.agents.HumanAgent()\n", "\n", "env = gym.make(env_id)\n", "obs, infos = env.reset()\n", "\n", "# Since we asked for more infos, let's print them.\n", "print(\"Walkthrough:\", \". \".join(infos[\"extra.walkthrough\"]))\n", "print(infos[\"extra.recipe\"])\n", "\n", "env.render() # Print the initial observation.\n", "\n", "score = 0\n", "done = False\n", "while not done:\n", " command = input('> ')\n", " ob, score, done, infos = env.step(command)\n", " env.render()\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualizing a `TextWorld.Game` object" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import textworld\n", "gamefile = gamefiles[0] # Pick a game.\n", "game = textworld.Game.load(gamefile.replace(\".ulx\", \".json\"))\n", "textworld.render.visualize(game)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }