Question

I'm designing a choice based adventure game in VB.NET 2010, where you are presented with a story label, and you choose between 2 buttons, which choice you want to make. I want to know the most efficient way to do this.

My goals are to store the labels for the buttons and the story in some form of data structure, which is temporarily a hash table right now, and some form of structure for the choices. Right now I am using a custom class that references the indexes of the next 2 choices and their respective label indexes which stores the class instances in a hashtable. I've looked into things like arrays, dictionaries, lists, and collections, but I'm not sure which one best fits what I'm after. Any .NET data structure would work. What is the most efficient data structure for these 2 pieces of data? Would just a string array work?

Was it helpful?

Solution

Here's how I'd define the structures (assuming by VB.Net-fu isn't failing me):

Public Class Decision
    Public Property Title As String
    Public Property Description As String
    Public Property FirstChoice As Decision
    Public Property SecondChoice As Decision
End Class

The Title is what you display on or above the button to select that choice.

The Description is what you display once you've committed that choice, and are coming to the next decision.

If FirstChoice or SecondChoice is null, you can choose to hide the button. This will allow you to make only one choice optional, and have it skewed to once side of the frame, for dramatic effect. E.g.:

                  You've found yourself in a narrow corridor



                                                              [Go Right]

If you want to enable more choices than two, or at least leave yourself free to do so in the future, you could define your data structure like this:

Public Class Decision
    Public Property Title As String
    Public Property Description As String
    Public Property Decisions As New List(Of Decision)
End Class

You could use a ListBox or GridView style control to display it to the user, or optionally switch between a list box and buttons depending on how many items you have in the list.

By the way, the data structure is intentionally recursive. This becomes a tree structure, and at each node of the tree you have a decision, and potentially child decisions.

Normally you wouldn't loop such a structure, but there is no reason you couldn't in this case. If you hit a "game over" style situation, your option can simply be to go back to the beginning, which will be linked to the root instance (and thus be a "cyclic graph").

Edit

Here's a code sample to show you how you can hook these together:

Dim darkAlley = New Decision With
{
    .Title = "Dark Alley",
    .Description = "You are in a deep dark alley." _
        + "  The night surrounds you and you feel a bit claustrophobic." _
        + "  Obvious exits are east and Salisbury street."
}

Dim eastOfDarkAlley = New Decision With
{
    .Title = "East of Dark Alley",
    .Description = "You are mauled by a bear!" _
        + "  He was a dire bear, so he had rabies.  Start over?"
}

Dim salisburyStreet = New Decision With
{
    .Title = "Salisbury street",
    .Description = "Mmm... Ground beef... Blarrghlhlap (*tongue hangs out*)"
}

darkAlley.FirstChoice = eastOfDarkAlley
darkAlley.SecondChoice = salisburyStreet

eastOfDarkAlley.FirstChoice = darkAlley

salisburyStreet.FirstChoice = darkAlley

OTHER TIPS

Your game is pretty clearly a decision tree. If there are only ever 2 choices, some modified form of a binary tree would be perfect. Otherwise, a somewhat more complicated n-ary tree would do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top