• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • 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: 79177
377
  • 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: 79177
377
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic