A text-adventure in the style of the old UNIX game ADVENT, my game AdventureGame implements a minimal but complete D&D-style ruleset. It supports 24 interaction commands, such as LOOK AT, PICK UP, EQUIP, PICK LOCK, and ATTACK.
Under the hood, it has a fully fully object-oriented design using multiple inheritance, encapsulation, and separation of concerns. I wrote it via test-driven development using unittest.py, and a thorough unit testing suite is included.
It comes to 4,179 sloc not counting tests. Implementation details below.
Some screencaps:
AdventureGame
Source: GitHub Repository
adventuregame.elements
The lowest layer of the design, implementing the D&D-like game rules. Features:
Classes to model elements like types of item, types of door, etc. The game elements are stored as .ini file data embedded in the frontend program advgame.py; game element objects are instanced from this data.
The Character, Equipment, and Ability_Scores classes, which together model the player character.
The Doors_State, Containers_State, Creatures_State, Rooms_State, and Game_State classes, which index the element objects for the Command_Processor object (q.v.).
adventuregame.utility
A brief module containing utility functions used elsewhere in the package. Some examples:
lexical_number_to_digits(), which interprets textual numbers (like ‘one’ or ‘ninety-nine’) to digits.
roll_dice(), which interprets a classical D&D-style dice expression (such as ‘1d4+1’ or ‘2d12’) and executes a random number generation according to that expression, returning the result.
textwrapper(), which wraps text to a specified column width (80 by default).
adventuregame.statemsgs
Reserved for Game_State_Message and its 97 subclasses. Features:
Each subclass exists to encapsulate a specific type of message that can be displayed to the player. Nearly all of them accept a series of key-value pairs on instantiation.
A
Game_State_Message
subclass’s message property contains the logic that interprets the object data (if any) into one or more player-legible natural-language sentences.
adventuregame.processor
The command-processing layer of the design, which implements the game rules logic for each of the 24 commands. Features:
A monolithic class, Command_Processor, running to 1112sloc on its own. Has a method for each of the 24 commands, which implement the command logic.
Has a process() method, which accepts natural language command text. It detects the command used, tokenizes the string, and tail-calls the command method with those arguments.
Each command method returns a tuple of one or more Game_State_Message subclasses, representing messages to the display to the player.