4 Replies - 750 Views - Last Post: 12 June 2012 - 02:19 PM Rate Topic: -----

#1 foxywaffle123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-June 12

how to "go back" in a text based game

Posted 08 June 2012 - 07:08 PM

alright guys I'm trying to make a text based game on free basic, so far i have a pretty good start the only problem is that i dont know how to go back to the last question i asked for example i did
[dim choice as integer]
[print"1.go in room"]
[print"2.go down stairs"]
[print"3.kill yourself"]
[input"choose your fate";choice]
i dont feel like typing a lot but if the player chose choice 3 and killed himself how would i make it where i can ask the player if they would like to return to the last question, so they could have another try?

Is This A Good Question/Topic? 0
  • +

Replies To: how to "go back" in a text based game

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,652
  • Joined: 02-June 10

Re: how to "go back" in a text based game

Posted 09 June 2012 - 07:03 AM

Keep a list of state objects?

if you make a class that represents the player's state:
Location
Inventory
Health
etc.

Then each time there is a change in any of those things, you clone the state before the change and add that instance to a list/array/collection.

Someone could then move back 1 move, or 10.


Or, just add a save state option. If they die, they can then load their save state and pick up again.
Was This Post Helpful? 1
  • +
  • -

#3 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 560
  • View blog
  • Posts: 1,258
  • Joined: 24-April 12

Re: how to "go back" in a text based game

Posted 11 June 2012 - 06:11 AM

View Postfoxywaffle123, on 08 June 2012 - 07:08 PM, said:

alright guys I'm trying to make a text based game on free basic, so far i have a pretty good start the only problem is that i dont know how to go back to the last question i asked for example i did
[dim choice as integer]
[print"1.go in room"]
[print"2.go down stairs"]
[print"3.kill yourself"]
[input"choose your fate";choice]
i dont feel like typing a lot but if the player chose choice 3 and killed himself how would i make it where i can ask the player if they would like to return to the last question, so they could have another try?


Wow. This one really takes me back. I kind of miss the old days of text games.

Anyway, basically the answer to your question is "assign a variable to hold the previous choice" (the choice before the decision). So, if you were doing a "Choose Your Own Adventure" type game with an actual book, you might have something similar. Say on page 23 you have the three options mentioned "Go in Room (page 46)", "Go Down Stairs (Page 67)", and "Kill Yourself (Page 84)". So, the player chooses "Go Down Stairs" and flips to page 67 to read that they "Go Down Stairs and the building collapses and they die". So, you want them to go back and get to answer the question again. One way to do it is to tell a friend "Hey, remember that I'm on page 23 before I flip the page and make my choice". Then when you want to go back, you just ask the friend "What page was on before?" and then flip to that page.

That's basically how you do it depending on how you've coded your game. One way you could do it would be with some sort of "array" where each "page" is an element in the array. Then you print array element 67 for "Go Down Stairs" which results in player death. So, you go before the player chooses, you keep track of the previous "page"/element number, so that you can go back and print that one again to "resurrect" the player.

There are actually probably several different ways you "could" code this, but that's probably what I would do. Keeping a list of state objects is correct too, although I think that would be for a more advanced game. And in that case you would just copy all the state objects and then reload the copy if needed. Same concept, just a whole lot more variables.

This post has been edited by BBeck: 11 June 2012 - 06:15 AM

Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: how to "go back" in a text based game

Posted 11 June 2012 - 06:39 AM

The key point is that there are a few standard ways to deal with something like this, and what works best will depend on the architecture of your game.

Judging by the multiple-choice presentation, I suspect that the unit of "state" in your game is a particular situation, probably a description and a set of choices, each of which leads to another description and a set of choices. If that's the case, then just push the situations onto a stack and pop them to undo. If state is more complex - for example, if the character has possessions and health and whatnot which matter to the game and vary independent of the room they're in - then the Klingon's solution is more what you'd want. But again, you'd make a State object and push it onto a stack.
Another way to do undo, which might not work so well in this scenario but deserves to be mentioned is the "command object" pattern. In this, you'd save the command itself onto a stack, and have instructions for undoing a command. So "drop torch" would be a command that changes your state in a particular way, and you could write an undo that would reverse that. This is tricky in text adventures because changes tend to not preserve information always (for example, you might have a random teleport scenario, which would be impossible to undo without saving state), and also because commands tend to have complex meanings. For example, "drop X" would generally mean "transfer X from player's pack to ground" but when you're on the Rope Bridge it would mean "transfer X from player's pack to the river a thousand feet below, losing it forever", and "drop torch" in the library might mean "the library catches fire and you barely escape with your life. there is an angry librarian here." So the command object pattern isn't the one I'd steer you towards in this case, but there are many cases where it's useful.
Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: how to "go back" in a text based game

Posted 12 June 2012 - 02:19 PM

Remember that this is a simple text-based game, so the more simple a game is the more basic the algorithms should be.

How I personally would approach this for this game would be to only store up to a fixed number of previous states, say five as a suitable figure, effectively ending up with a queue.

As tlhIn`toq quite rightly said have a class representing all the states of the previous action or whatever in an array, but there is no need to copy instances across, you just 'circularly reference' this array overwriting what was there before. This means that the array reference will always be some value (modulo 5) in this instance, removing the steps of traversing the array copying over the elements; making a O(n) process a O(1) one.

That's it! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1