Text-Mode Games as First Haskell Projects

Posted on May 28, 2022 by Jack Kelly
Tags: haskell, coding

Back in my school days, when my friend and I were first learning C++, we were voracious readers and exhausted our school’s programming curriculum very quickly. Our teacher challenged us to make something larger so we’d stop playing Tribes all the time. It worked: I spent the rest of the term building a text-mode platformer using <conio.h>, and he spent the rest of the term building and tweaking a small text-mode dungeon crawler.

Many new Haskellers make it through initial material (everything up to and including the Monad typeclass, let’s say), write a couple of “Hello, world!”-tier projects that use the IO type, but struggle to make the jump to industrial libraries and/or find projects that excite them. I think text-mode games can grow very smoothly alongside a programmer learning a new language, so here’s some thoughts on how to get started, how you might extend a game, and some advice for Haskell specifically.

A Simple Game

A text-mode dungeon crawler can start very small. My friend began with a core encounter loop, which was very much like a Pokémon battle: the player was placed into combat with a monster, given a choice between attacking and fleeing, and repeated this loop until either the player ran off or one defeated the other. You could imagine it looking something like:

There is a goblin in front of you.
You can ATTACK or RUN. What do you do?
[HP 98/100]> attack

You hit the goblin for 5 damage!
The goblin hits you for 7 damage!

There is a goblin in front of you.
You can ATTACK or RUN. What do you do?
[HP 91/100]> run

Okay, coward! See you later.

In Haskell, we might manually pass state between all our functions, and that state could be as simple as:

data GameState = GameState
  { playerHP :: Int
  , monsterHP :: Int

Extending Your Game

Once this is working, there are a lot of ways to extend it. Some ideas of things to add:

Haskell-Specific Advice

On the Haskell side, your goal should be to keep things as simple as possible. A big ball of IO with do-expressions everywhere is completely fine if it keeps you hacking on and extending your game. Don’t look at the dizzying array of advanced Haskell features, libraries, and techniques; wait until what you have stops scaling and only then look for solutions. Still, some Haskell-specific ideas might be helpful:

Go Forth and Hack (and Slash)!

A project like this can grow as far as you want, amusing you for a weekend or keeping you tinkering for years. Textmode games are an exceptionally flexible base on which to try out new languages or techniques. Start small, enjoy that incremental progress and use the problems you actually hit to help you choose what to learn about.

Previous Post
All Posts | RSS | Atom
Next Post
Copyright © 2023 Jack Kelly
Site generated by Hakyll (source)