Raise your hand if one of the first programming experiences related to a dungeon somehow. A D&D app? A character generator? A DM utility? An RPG game? All of the above?
When I saw this article I knew I had to share it. A Dungeon DSL? That's pretty cool, but the fact that the state machine behind the DSL is also shared makes it fun++
There's a magic and power behind DSL's and learning them this way makes them much less scary and much more fun...
In this article, we'll create our own simple programming language for mapping a dungeon role-playing game. Our language will consist of an external domain specific language, including types, source code sections, and strings. Our main program will load the external DSL code file into a state machine and execute the program, allowing us to walk through a deep, dank dungeon in search of treasure!
Sweeping the Dungeon Clean
Similar to the above internal DSL, an example external domain specific language might appear as follows:events moveSouth south moveNorth northendstate emptyRoom "Empty Room"endstate treasureRoom "Treasure Room"endconnect emptyRoom to treasureRoom by moveSouthconnect treasureRoom to emptyRoom by moveNorth
The above code requires no knowledge of C# .NET, nor any other programming language, and yet it allows the user to implement a fully operating program. The DSL created further down in this article will be similar to the above example, but we'll be adding an additional construct to include actions within a state.
Crawling the Dungeon
Running the example program with our dungeon.txt code file produces the following output:...Executing idleExecuting waitingForStateExecuting waitingForActionExecuting waitingForStateExecuting waitingForTransitionExecuting waitingForTransitionStateExecuting waitingForTransitionExecuting waitingForStateExecuting waitingForActionExecuting waitingForTransitionStateExecuting waitingForTransitionExecuting waitingForStateExecuting waitingForActionExecuting waitingForStateExecuting idleLoading of External DSL completed.> Q = Quit, ? = Available Commands> Entering the dungeon.Executing idlelight (You see an endless white light.)> ?northsouth> northExecuting treeStatetree (You see a circle of trees.)> southExecuting idlelight (You see an endless white light.)> southExecuting pitStatepit (You see a dark bottomless pit.)> eastExecuting boxStatebox (You see an ominous box with a smaller glitter inside.)>?westopen> openExecuting treasureStatetreasure (You find a pile of sparkling treasure!)> q...
Admit it... After reading the article, thoughts of building your own Zork are whirling around your head. The real magic will happen when you realize all the other things you can do with this idea.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to send us feedback you can Contact Us.