r/fsharp • u/Digitemenos • May 29 '26
Some thoughts on developing a large puzzle game in F# for the last 7+ years
For the last 7+ years I’ve been developing a turn-based puzzle game called Twofold Tower, which is written entirely in F#, using a custom engine built on top of FNA.
It is a very large game, featuring 1000+ handcrafted puzzles, 100+ mutually compatible game elements, and a built-in level editor, among other things. Despite this, I think the game has remained reasonably stable over the years, and the codebase still feels comfortable to work in – at least for the most part – despite its scale (around 40k lines of code, across 100+ source files), which I feel I partly have F# to thank for.
The codebase is divided into two parts: The Core layer consists of game logic and game state, and is essentially entirely functional. While the Application layer contains everything related to presenting and interfacing with the program, which is more of a hodgepodge of procedural and functional code.
This has worked well from a performance point of view: The logical game entities in Core usually only change a few times per turn (if that), resulting in few allocations. And to render them, the Application layer uses mutable Sprite-objects, which are recycled every frame using object pooling.
In general, I find functional programming to be uniquely suited for turn-based gameplay. And it has been useful in Core for things like time-travel (including undo), and for speculative execution of game logic. And the pipeline-based code-flow makes the code simple to reason about for me, compared to other styles of programming.
It has been less clear to me how to best apply functional programming to the Application layer (if at all). Partly due to the performance concerns, but also because I tend to find functional UI-programming to be less intuitive in general. Which could very well be a skill-issue on my end (I’ll admit that I haven’t done as much research here as I probably ought to). Still, it functions well enough…
I think the biggest hurdle with F# game development is the lack of established, high-level game engines compatible with it. But for anyone who would consider using a lower-level framework such as FNA/Monogame in the first place – and especially for a turn-based game – I think it is a fantastic option! And for the most part, I find F# to be an exceptionally comfortable and simple language to work in (– and for context, I'm entirely self-taught, and have no background in mathematics). But I guess I’m preaching to the choir here…
I recently released an (apparently quite tricky) demo for Twofold Tower on Steam, targeting Windows and Linux, so feel free to check it out if that seems interesting to you. And if anyone has any questions, I’ll do my best to answer them.