Microsoft Small Basic

Program Listing: FCD758-1
' Text Adventure 0.2
' Program ID FCD758-1
Init()
Game()
' end of program
Sub Init
scenario[1] = "stage_0:"
scenario[2] = " You're at a fork in the road."
scenario[3] = " Which way do you go?"
scenario[4] = "LEFT,RIGHT,STAY"
scenario[5] = "-> stage_1_1,stage_1_2,stage_1_3,stage_0"

scenario[6] = "stage_1_1:"
scenario[7] = " Good choice, you find some money. :)"
scenario[8] = " Have a nice day."
scenario[9] = "-> end"

scenario[10] = "stage_1_2:"
scenario[11] = " You're at a stairs."
scenario[12] = " Which way do you go?"
scenario[13] = "UP,BACK"
scenario[14] = "-> stage_2,stage_0,stage_1_2"

scenario[15] = "stage_1_3:"
scenario[16] = " Nothing happend."
scenario[17] = " "
scenario[18] = "-> stage_0"

scenario[19] = "stage_2:"
scenario[20] = " Hard choice. But, good luck!"

scenario[21] = "end:"
scenario[22] = "-> stage_0"
nScenario = Array.GetItemCount(scenario)
pScenario = 1
EndSub
Sub Game
While pScenario <= nScenario
line = scenario[pScenario]
If Text.StartsWith(line, " ") Then
TextWindow.WriteLine(Text.GetSubTextToEnd(line, 2))
pScenario = pScenario + 1
ElseIf Text.EndsWith(line, ":") Then
pScenario = pScenario + 1
ElseIf Text.StartsWith(line, "->") Then
id = 1
Jump()
ElseIf Text.IsSubText(line, ",") Then
choices = line
Choose()
TextWindow.WriteLine("")
If id = 0 Then
id = n + 1
EndIf
pScenario = pScenario + 1
line = scenario[pScenario]
Jump()
Else
msg = "Unknown scenario: line " + pScenario
Error()
EndIf
EndWhile
EndSub
Sub Jump
' param id - choice
' param line
' work label - destination
len = Text.GetLength(line)
p = 3
While p <= len And Text.GetSubText(line, p, 1) = " "
p = p + 1
EndWhile
label = ""
For i = 1 To id
c = Text.GetIndexOf(Text.GetSubTextToEnd(line, p), ",")
If c = 0 Then
c = len - p + 2
EndIf
If i = id Then
label = Text.GetSubText(line, p, c - 1) + ":"
Else
p = p + c
If len < p Then
msg = "Label shortage: line " + pScenario
Error()
EndIf
EndIf
EndFor
For p = 1 To nScenario
If scenario[p] = label Then
pScenario = p
Goto break
EndIf
EndFor
msg = "Label " + label + " not found: line " + pScenario
Error()
break:
EndSub
Sub Error
TextWindow.ForegroundColor = "Red"
TextWindow.WriteLine(msg)
TextWindow.WriteLine("")
TextWindow.ForegroundColor = "Gray"
pScenario = nScenario + 1
EndSub
Sub Choose
' param choices - e.g. "A,B,C"
' return id - e.g. 1 for A
' work a,c,choice,i,len,n,p,u - will be broken

' Make array of choice
len = Text.GetLength(choices)
p = 1
i = 0
While p <= len
c = Text.GetIndexOf(Text.GetSubTextToEnd(choices,p), ",")
If c = 0 Then
c = len + 1
Else
c = c + p - 1
EndIf
i = i + 1
choice[i] = Text.GetSubText(choices, p, c - p)
p = c + 1
EndWhile
' Dispaly choices
n = i
For i = 1 To n
TextWindow.Write(choice[i])
If i < n - 1 Then
TextWindow.Write(" ")
ElseIf i = n - 1 Then
TextWindow.Write(" or ")
EndIf
EndFor
TextWindow.WriteLine("")
' Input
a = TextWindow.Read()
' Convert to upper case
u = Text.ConvertToUpperCase(a)
' Search id of choces
id = n
While choice[id] <> u And 0 < id
id = id - 1
EndWhile
EndSub