So i have begun to code this text-based game. I'm pretty much a beginner, working my way through a beginners book in Java and an MOOC-style online course.
I know that you can't do "GOTO"-statements in Java like in BASIC, so my solution for "jumping" across different parts of the narrative in the game looks like this:
You guys have some tips for classes i should study in order to find better ways to do this? It just seems so lengthy to go on like this, not at all "elegant" or effective.
Laziness is a programmers virtue, right? Or am i stuck with 100+ if-statements?
if( narraative == 1 ) // etc
Should be replaced with a switch statement.
You might want to rewrite this to load a game design from a (text?) file, rather than hard-coding the whole thing. In the file use named states instead of numbered states so that the file is easily modified, and then have your game engine be able to jump to a named state.
Carey Brown wrote: You might want to rewrite this to load a game design from a (text?) file, rather than hard-coding the whole thing. In the file use named states instead of numbered states so that the file is easily modified, and then have your game engine be able to jump to a named state.
I was thinking about doing it in XML, so the engine would take the data in different nodes in the XML-document. Which XML-parser would you use for this task?
At this stage I would not even think of an XML parser. You have not yet worked out what you are going to do, so you cannot work out how to do it. Please explain carefully how you are going to use text to run your game. You will probably find that potential solutions come to mind as you do that.
Benjamin Flintstone wrote:So i have begun to code this text-based game.
OK, well from the look of it, it would appear to be a kind of "story" game, where the user can choose how they want to to continue/end.
My suggestion (basically the same as Campbell's):
1. StopCoding (←click).
2. Draw a "map" of the story on a piece of paper, with circles representing "parts" (maybe with a bit of text) and lines with numbers denoting which "option" gets you to the next part.
Then try and work out how you might code it. Right now, you're just trying to do it with 'if's and 'else's and that's likely to produce Spaghetti code.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here