TextWorld: A Learning Environment for Text-Based Games

Marc-Alexandre Côté, Ákos Kádár, Xingdi Yuan, Ben Kybartas, Tavian Barnes, Emery Fine, James Moore, Matthew J. Hausknecht, Layla El Asri, Mahmoud Adada, Wendy Tay, Adam Trischler

Abstract: We introduce TextWorld, a sandbox learning environment for the training and evaluation of RL agents on text-based games. TextWorld is a Python library that handles interactive play-through of text games, as well as backend functions like state tracking and reward assignment. It comes with a curated list of games whose features and challenges we have analyzed. More significantly, it enables users to handcraft or automatically generate new games. Its generative mechanisms give precise control over the difficulty, scope, and language of constructed games, and can be used to relax challenges inherent to commercial text games like partial observability and sparse rewards. By generating sets of varied but similar games, TextWorld can also be used to study generalization and transfer learning. We cast text-based games in the Reinforcement Learning formalism, use our framework to develop a set of benchmark games, and evaluate several baseline agents on this set and the curated list.

The Challenge of Text-Based Games

Reinforcement learning (RL) has achieved significant milestones in visual domains such as Atari and Go. However, natural language remains a formidable frontier. Text-based games—exemplified by classics like Zork—require agents to navigate environments where the state is described entirely in text and actions must be issued as natural language phrases.

These games present several "grand challenges" that distinguish them from typical RL benchmarks:

  • Combinatorial Action Space: Unlike a joystick with a few discrete directions, an agent must choose from a near-infinite combination of words (e.g., "take the small rusty key from the wooden cabinet").
  • Partial Observability: Agents only perceive their immediate surroundings or inventory, requiring them to build and maintain a mental map of the unseen world.
  • Sparse Rewards: Success often requires a long sequence of specific, unrewarded actions before a single point is earned.
  • Common Sense and Affordances: Humans intuitively know that one cannot "eat a table," but an agent must learn these physical constraints and object affordances from scratch.
Different interaction modes in text-based games: (a) Parser-based, (b) Choice-based, and (c) Hypertext-based.

Existing research has often been split between simple grid-worlds lacking linguistic depth and commercial games that are too difficult for foundational research. TextWorld bridges this gap by providing a tunable, generative environment.

The TextWorld Framework

TextWorld is a Python-based framework designed to facilitate the creation and play-through of text games. It consists of two primary components: a logic-based engine and a game generator.

System Architecture

The Game Engine is a logic-based inference engine that uses linear logic to define world mechanics. It tracks "facts" (e.g., at(apple, kitchen)) and applies rules to determine how player actions modify the world state. The Game Generator allows researchers to programmatically create new games. It utilizes a Random Walk algorithm for map generation and forward/backward chaining to design quests—sequences of actions required to reach a goal.

The TextWorld system architecture, showing the interaction between the Knowledge Base, Generator, and Engine.

Crucially, the environment uses Context-Free Grammars (CFGs) to translate the underlying logical state into human-readable text. This modularity allows researchers to swap "themes," changing a generic description into a specific narrative setting (e.g., a "medieval world" or a "home world") without altering the underlying logic.

Benchmarking and Evaluation

The paper introduces two primary resources for evaluating RL agents:

  1. The Curated List: A collection of 50 hand-authored classic text games (including Zork and The Hitchhiker's Guide to the Galaxy). These were analyzed across dimensions like "Cruelty" (how easily a game becomes unwinnable) and "Stochasticity."
  2. Treasure Hunter Benchmark: A suite of 30 levels generated by TextWorld that scale linearly in difficulty, ranging from simple 5-room layouts to complex 20-room environments with locked containers and specific key requirements.

Key Results

Evaluation of baseline agents (such as the BYU and Golovin agents) revealed that classic text games remain largely unsolved. Most agents scored zero on the majority of the 50 curated games, succeeding only where games provided clear starting points or simple navigation. On the Treasure Hunter benchmark, performance dropped sharply as complexity increased. Even state-of-the-art agents struggled to outperform a "choice-based random agent" once quests required navigating 20 rooms and managing complex inventories, highlighting that efficient navigation and long-term memory remain significant hurdles.

Conclusion and Future Work

TextWorld provides a necessary sandbox for studying the intersection of language and RL. By treating language as an abstraction of high-level logic, it allows agents to focus on the "why" and "when" of actions rather than low-level physics.

Future developments aim to address current limitations, such as the linear nature of generated quests and the lack of dynamic elements like non-player characters (NPCs) or time-based events. The authors also plan to expand the environment to support multi-user interactions, further pushing the boundaries of how machines learn to interact within complex, language-defined worlds.

Summary updated

BibTeX
@inproceedings{cote2018textworld,
  author = {Marc-Alexandre Côté and Ákos Kádár and Xingdi Yuan and Ben Kybartas and Tavian Barnes and Emery Fine and James Moore and Matthew J. Hausknecht and Layla El Asri and Mahmoud Adada and Wendy Tay and Adam Trischler},
  title = {{TextWorld: A Learning Environment for Text-Based Games}},
  booktitle = {CGW @ IJCAI},
  year = {2018},
  url = {https://openreview.net/forum?id=r1VK7Ez_-B},
}
Figure