Dungeons, Dungeons, a DSL for a Dungeon...

Description

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?

[Hand raised]

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...

Creating a C# .NET External Domain Specific Language to Map a Dungeon

Creating your own programming language can be a daunting task. After all, that's why developers choose existing programming languages, such as C#, Ruby, Perl, JavaScript or other traditional languages. However, in certain instances, a case may arise where non-developer business users may be required to configure and modify programming logic. Since not all business users can be programmers in the native programming language, it may be beneficial to implement a domain specific language that 3rd-party users can utilize. Simple examples of such a language might include Windows INI files or XML configuration files. However, we can create an even more specific custom programming language, just for our software purposes, by using an External Domain Specific Language.

...

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.

...

Page Snap

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.

 

Page preview image curtsey of orkboi

The Discussion

  • User profile image
    Billy McCafferty

    Heh, the opening was hysterical...reminded me of my days of writing an RPG (based on D&D rules) on my TI-85 in AP Physics, junior year of high school.  (In hindsight, probably should've paid more attention to the class.)  Had to split it across two TI-85s due to the size of the program...part I and II with a transferrable data file to pick up where you left off in part II.  Loved those days!  (Later migrated it to Turbo Pascal.)  Sure would've been easier with a Dungeon DSL!!
    Billyhttp://www.sharprobotica.com 

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.