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.22.' Program ID FCD758-13.Init()4.Game()5.' end of programInitialization 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 = 136.EndSubParsing and Executing the Scenario
Subroutine Game parses the scenario and execute each statement.
37.Sub Game38. While pScenario <= nScenario39. line = scenario[pScenario]40. If Text.StartsWith(line, " ") Then41. TextWindow.WriteLine(Text.GetSubTextToEnd(line, 2))42. pScenario = pScenario + 143. ElseIf Text.EndsWith(line, ":") Then44. pScenario = pScenario + 145. ElseIf Text.StartsWith(line, "->") Then46. id = 147. Jump()48. ElseIf Text.IsSubText(line, ",") Then49. choices = line50. Choose()51. TextWindow.WriteLine("")52. If id = 0 Then53. id = n + 154. EndIf55. pScenario = pScenario + 156. line = scenario[pScenario]57. Jump()58. Else59. msg = "Unknown scenario: line " + pScenario60. Error()61. EndIf62. EndWhile63.EndSubProcessing 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 Jump65. ' param id - choice66. ' param line67. ' work label - destination68. len = Text.GetLength(line)69. p = 370. While p <= len And Text.GetSubText(line, p, 1) = " "71. p = p + 172. EndWhile73. label = ""74. For i = 1 To id75. c = Text.GetIndexOf(Text.GetSubTextToEnd(line, p), ",")76. If c = 0 Then77. c = len - p + 278. EndIf79. If i = id Then80. label = Text.GetSubText(line, p, c - 1) + ":" 81. Else82. p = p + c83. If len < p Then84. msg = "Label shortage: line " + pScenario85. Error()86. EndIf87. EndIf88. EndFor89. For p = 1 To nScenario90. If scenario[p] = label Then91. pScenario = p92. Goto break93. EndIf94. EndFor95. msg = "Label " + label + " not found: line " + pScenario96. Error()97. break:98.EndSubDisplaying Error
If the scenario has error, the program is not to be continued. So, the program displays error and stops.
99.Sub Error100. TextWindow.ForegroundColor = "Red"101. TextWindow.WriteLine(msg)102. TextWindow.WriteLine("")103. TextWindow.ForegroundColor = "Gray"104. pScenario = nScenario + 1105.EndSubAnd 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!