Last week, I introduced a text adventure game. If I expand the game story of that manner as in the program, the program will have a lot of If statements and Goto statements. Today, I will introduce rewritten program with less If and Goto statements. The updated version is published as FCD758-1,
Main
Main part of this program became very simple.
1.' Text Adventure 0.2
2.' Program ID FCD758-1
3.Init
(
)
4.Game
(
)
5.' end of program
Initialization for Scenario Data
This data is like a main program of the previous text adventure program introduced last week. So, this data is a kind of script language. The grammar of this scenario data is:
- A line started with " " (space) is displayed as a text.
- A line ended with ":" (colon) is treated as a label targeted by jump control statements.
- A line started with "->" is treated as a jump (like Goto) control statement. A jump with single label is non conditional. A jump with several labels is conditional.
- A line other than above and has "," (comma) is displayed as options and prompts user selection.
6.Sub
Init
7.
scenario
[
1
]
=
"stage_0:"
8.
scenario
[
2
]
=
" You're at a fork in the road."
9.
scenario
[
3
]
=
" Which way do you go?"
10.
scenario
[
4
]
=
"LEFT,RIGHT,STAY"
11.
scenario
[
5
]
=
"-> stage_1_1,stage_1_2,stage_1_3,stage_0"
12.
13.
scenario
[
6
]
=
"stage_1_1:"
14.
scenario
[
7
]
=
" Good choice, you find some money. :)"
15.
scenario
[
8
]
=
" Have a nice day."
16.
scenario
[
9
]
=
"-> end"
17.
18.
scenario
[
10
]
=
"stage_1_2:"
19.
scenario
[
11
]
=
" You're at a stairs."
20.
scenario
[
12
]
=
" Which way do you go?"
21.
scenario
[
13
]
=
"UP,BACK"
22.
scenario
[
14
]
=
"-> stage_2,stage_0,stage_1_2"
23.
24.
scenario
[
15
]
=
"stage_1_3:"
25.
scenario
[
16
]
=
" Nothing happend."
26.
scenario
[
17
]
=
" "
27.
scenario
[
18
]
=
"-> stage_0"
28.
29.
scenario
[
19
]
=
"stage_2:"
30.
scenario
[
20
]
=
" Hard choice. But, good luck!"
31.
32.
scenario
[
21
]
=
"end:"
33.
scenario
[
22
]
=
"-> stage_0"
34.
nScenario
=
Array
.
GetItemCount
(
scenario
)
35.
pScenario
=
1
36.EndSub
Parsing and Executing the Scenario
Subroutine Game parses the scenario and execute each statement.
37.Sub
Game
38.
While
pScenario
<
=
nScenario
39.
line
=
scenario
[
pScenario
]
40.
If
Text
.
StartsWith
(
line
,
" "
)
Then
41.
TextWindow
.
WriteLine
(
Text
.
GetSubTextToEnd
(
line
,
2
)
)
42.
pScenario
=
pScenario
+
1
43.
ElseIf
Text
.
EndsWith
(
line
,
":"
)
Then
44.
pScenario
=
pScenario
+
1
45.
ElseIf
Text
.
StartsWith
(
line
,
"->"
)
Then
46.
id
=
1
47.
Jump
(
)
48.
ElseIf
Text
.
IsSubText
(
line
,
","
)
Then
49.
choices
=
line
50.
Choose
(
)
51.
TextWindow
.
WriteLine
(
""
)
52.
If
id
=
0
Then
53.
id
=
n
+
1
54.
EndIf
55.
pScenario
=
pScenario
+
1
56.
line
=
scenario
[
pScenario
]
57.
Jump
(
)
58.
Else
59.
msg
=
"Unknown scenario: line "
+
pScenario
60.
Error
(
)
61.
EndIf
62.
EndWhile
63.EndSub
Processing for "->" Control Statement
"->" statement behaves as Goto statement if it has a single label. But it has some labels, it behaves like ON GOTO statement in BASIC language. If the previous line has options, label is selected as the selected option id (1, 2, ...). If the choice is invalid then the last label is selected.
64.Sub
Jump
65.
' param id - choice
66.
' param line
67.
' work label - destination
68.
len
=
Text
.
GetLength
(
line
)
69.
p
=
3
70.
While
p
<
=
len
And
Text
.
GetSubText
(
line
,
p
,
1
)
=
" "
71.
p
=
p
+
1
72.
EndWhile
73.
label
=
""
74.
For
i
=
1
To
id
75.
c
=
Text
.
GetIndexOf
(
Text
.
GetSubTextToEnd
(
line
,
p
)
,
","
)
76.
If
c
=
0
Then
77.
c
=
len
-
p
+
2
78.
EndIf
79.
If
i
=
id
Then
80.
label
=
Text
.
GetSubText
(
line
,
p
,
c
-
1
)
+
":"
81.
Else
82.
p
=
p
+
c
83.
If
len
<
p
Then
84.
msg
=
"Label shortage: line "
+
pScenario
85.
Error
(
)
86.
EndIf
87.
EndIf
88.
EndFor
89.
For
p
=
1
To
nScenario
90.
If
scenario
[
p
]
=
label
Then
91.
pScenario
=
p
92.
Goto
break
93.
EndIf
94.
EndFor
95.
msg
=
"Label "
+
label
+
" not found: line "
+
pScenario
96.
Error
(
)
97.
break
:
98.EndSub
Displaying Error
If the scenario has error, the program is not to be continued. So, the program displays error and stops.
99.Sub
Error
100.
TextWindow
.
ForegroundColor
=
"Red"
101.
TextWindow
.
WriteLine
(
msg
)
102.
TextWindow
.
WriteLine
(
""
)
103.
TextWindow
.
ForegroundColor
=
"Gray"
104.
pScenario
=
nScenario
+
1
105.EndSub
And subroutine Choice is the same as the previous text adventure program.
With small change, the scenario can be read from a text file. By today's update, procedures become simple. But data (scenario) becomes little complex. Because the scenario is a kind of script, it will be needed testing and debugging.
For this program, there is room for improvement. But I'm going to write about text window base game program with action next week.
Have a happy programming!