• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Better way of handling text-based narrative

 
Greenhorn
Posts: 2
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


etc til...


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?
 
Saloon Keeper
Posts: 9561
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Benjamin Flintstone
Greenhorn
Posts: 2
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 76427
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 76427
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Benjamin Flintstone wrote:. . .
Laziness is a programmers virtue, right? . . .


Not at all. What it means is that you spend lots of time thinking and end up writing shorter more elegant code. The more productive programmers write fewer pages of code.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic