FlatStates: Game State As Logic Statements – Part 1

A lot of people have seen logic statements like p→q in high school. Most would consider these tidbits of knowledge useless in real life, like other stuff they learned at the time. However for a programmer using logic is part of the job.  We may not write p∧ q  on our screens, but we write p && q instead. Yet so much wisdom from those high school logic classes are lost to us. And some of them may be unexpectedly useful for game development.

I had a brief experience with Prolog in my undergrad years. It was quite interesting to write logical statements into the program but at the time I didn’t find a relevance to my game development activities. That belief changed when I started to delve into domain independent graph search algorithms in first year of my professional life. For uninitiated, graph search algorithms operate on graph structure to find answers to a particular question. Domain independent/configurable algorithms often provide an universal way of describing set of facts and possible operations so you can load different problems without changing the algorithm itself. And that universal way is using axioms.

Axioms are units of facts in your domain. For instance cat(whiskers)simply mean “whiskers” is a cat, an axiom with single term. A bit more complex one may be owner(schrödinger,whiskers); schrödinger owns whiskers. What meaning each axiom carry depends on how you design your domain. You can represent your game state in axioms, which makes it easy to read, write and serialize on to disk. This in itself is not that interesting. Why would you go out of your way to implement such world state? The answer is “to ask questions”.

Lets say for our AI code we need to find an NPC fitting some criteria, in this case an scientist with a cat. You can implement loops to iterate over all scientist objects then check all the items they have to see if they have a cat. Or you can ask scientist(X),owner(X,Y),cat(Y)to world state, which will answer {X=schrödinger, Y=whiskers}. This operation is called unification, for those who remember high school it is denoted with ∪ symbol. Neat stuff!!! Unification is a common operation in aforementioned graph search algorithms. So before diving deep into my AI work, I wanted to implement a simple Unification algorithm in C# to be used in my Unity projects. In this blog series I will attempt to do just that.

On the next part of the series we will start this attempt by writing up C# code for Axiom and Term classes.

FlatStates series:

About the author

Sercan Altun: Hello there fellow human, I am Sercan Altun: video game developer, coding aficionado & part-time weirdo. Check out my blog on game development and stuff.

B801 300D 93CC 11A9 3332 4270 A5FB C300 1020 24E7