Battle Knights ASCII Art Game

Michael Hayman
3 min readFeb 17, 2021

For a recent interview, my take home challenge was to build a Python program which would output the state of Battle Knights: a simple game involving four knights and four items in a space resembling a chess board. The knights move around the board, find and hold items depending on their values, and win or lose fights depending on the attributes of their items and their role in the fight. The program had to take a text file as output, and output the final game state in the JSON format (see full details in these rules). This all seemed fairly straightforward to me, so I decided to animate the game in the command line using ASCII art as well.

ASCII art is incredible. It draws on the 95 printable characters in the ASCII Standard to create graphic art. Dating back to 1963, these characters allow for creative graphic representation that predates modern computer graphics, and are available for us in all sorts of computing environments — sometimes even hidden in plain sight, as in the comments of web pages’ HTML. If you’ve always thought of code as just the means to an end, instead of an intrinsically beautiful art form in itself, this is just the tip of the iceberg. You can learn a lot from this fantastic presentation by Dylan Beattie, for a start: he performs a song with lyrics that write FizzBuzz in Rockstar (a language he wrote which takes clichéd lyrics from 1980s power ballads as expressions), and a melody that writes FizzBuzz in Sonic Pi (a language written by Sam Aaron that generates music using coding logic and the mathematical representation of sound); he also discusses such marvels as the Quine Relay (see below) — an astonishing feat of software engineering for us to explore another day.

My Battle Knights program includes the following files (available on GitHub for you to play with — let me know your thoughts):

  • run.py: Running this file will illustrate the game, as in the GIF above, and output the final game state in command line and as a JSON file.
  • setup.py: Defines classes for knights and items, and creates each knight and item as instances of these classes.
  • states.py: Creates templates for the board state — as illustrated in the animation — and game state in JSON format.
  • art.py: The fun part — some ASCII art that I made for each different game action.
  • move_details.py: Using the move list text file, this determines the resulting outcomes — how a knight should move, interact with items, and fight.
  • process.py: Uses move details to illustrate the board state and ASCII art for each move, and also defines a welcome function to illustrate the start of the game.

I like to keep my posts short, so that’s all for today. If there’s an audience for such obscurities, I’ll come back to this topic in a future post, perhaps with a detailed tutorial. Let me know what interests you most in the comments.

--

--

Michael Hayman

Building a better future through original and innovative data science solutions to novel problems.