Making a text based adventure for homework...

Recommended Videos

Artemis923

New member
Dec 25, 2008
1,493
0
0
So, I'm programming a text based adventure game for hw, and it's driving me crazy. I'm trying to use a conditional test to check if the player has a key before they walk through a locked door. However, I can't get the damned thing to work properly.

This is what I've got so far:

@ECHO OFF
REM-LABEL INDICATING THE BEGINNING OF THE DOCUMENT.
:BEGIN

SET IRONKEY = FALSE
SET BONEKEY = FALSE
SET FIREROD = FALSE
SET HAMMER = FALSE
SET CRYSTALSHIELD = FALSE
SET LONGSWORD = FALSE
SET STORMBRINGER = FALSE
REM - THE PLAYER DOES NOT START THE GAME WITH THESE ITEMS.
:START
CLS

:CELL
REM - BEGINNING CELL ROOM. PC SPAWN POINT.


ECHO YOU AWAKEN IN A DARK CHAMBER. THE SMELL OF BLOOD AND URINE ASSAULTS YOUR

ECHO NOSTRILS AS YOU SHAKILY CLIMB TO YOUR FEET. THE ONLY LIGHT SOURCE IS COMING

ECHO FROM A NEARBY ROOM TO THE NORTH. THE CELL DOOR IS PARTIALLY OPEN.

ECHO NOW IS YOUR CHANCE TO ESCAPE!

:MOVECELL
ECHO MOVE? CHOOSE A DIRECTION (N=NORTH, S=SOUTH, E=EAST, W=WEST)
SET /P INPUT=COMMAND?
IF %INPUT%==N GOTO TORTURE
IF %INPUT%==S GOTO NODOORCELL
IF %INPUT%==E GOTO NODOORCELL
IF %INPUT%==w GOTO NODOORCELL

:NODOORCELL
ECHO THERE IS NO DOOR HERE!
GOTO MOVECELL

:TORTURE
CLS
REM - TORTURE CHAMBER
ECHO YOU SEE A LARGE TABLE WITH A BODY STRAPPED TO IT. HIS STOMACH HAS BEEN

ECHO CARVED OPEN, AND BLOOD STAINS THE COLD STONE FLOOR IN A POOL UNDERNEATH

ECHO THE CORPSE. VARIOUS INSTRUMENTS OF TORTURE ARE ORGANIZED AND STOWED UPON THE

ECHO WALL. A CLOSED IRON MAIDEN RESTS IN THE CORNER. YOU CAN SEE TWO DOORS; ONE

ECHO TO THE NORTH, MADE OF SIMPLE WOOD, AND ANOTHER TO THE EAST, MADE OF STURDY

ECHO IRON.

ECHO MOVE OR ACT? (M=MOVE, A=ACT)
SET /P INPUT=COMMAND?
IF %INPUT%==M GOTO MOVETORTURE
IF %INPUT%==A GOTO ACTTORTURE

:MOVETORTURE
ECHO CHOOSE A DIRECTION (N=NORTH, S=SOUTH, E=EAST, W=WEST)
SET /P INPUT=COMMAND?
IF %INPUT%==N GOTO LAB
IF %INPUT%==S GOTO TORTCELL
IF %INPUT%==E && (%IRONKEY%==TRUE) GOTO ARMORY
ELSE GOTO LOCKED ARMORY
IF %INPUT%==W GOTO NODOORTORT

:LAB
ECHO THIS IS THE LAB. COMING SOON!
GOTO END

:TORTCELL
ECHO YOU'VE RETURNED TO YOUR CELL. THIS WON'T HELP YOU ESCAPE!
GOTO MOVECELL

:LOCKEDARMORY
ECHO THE DOOR IS LOCKED!
GOTO MOVETORTURE

:NODOORTORT
ECHO THERE IS NO DOOR HERE!
GOTO MOVETORTURE


So, basically, I am trying to check if the player has the IRONKEY before they go through to the ARMORY. What am I screwing up? I thought this would work...

IF %INPUT%==E && (%IRONKEY%==TRUE) GOTO ARMORY

...but it keeps giving me "&& was unexpected at this time" and it closes out.

Please, toss me some advice if you've got it, I've got other work to do besides just this.
 

MidnightCat

New member
Jul 21, 2009
125
0
0
Artemis923 said:
Please, toss me some advice if you've got it, I've got other work to do besides just this.
Okay, there are a couple of things:

Firstly, try replacing

IF %INPUT%==E && (%IRONKEY%==TRUE) GOTO ARMORY
ELSE GOTO LOCKED ARMORY

with

IF %INPUT%==E (IF %IRONKEY%==TRUE (GOTO ARMORY) ELSE (GOTO LOCKEDARMORY))

I couldn't get the && operator to work with your code, but the above statement did.

Otherwise, you've got spaces around the equals sign when you're setting your variables, which seems to prevent them being set.

This is the full, altered code that works for me:

@ECHO OFF
REM-LABEL INDICATING THE BEGINNING OF THE DOCUMENT.
:BEGIN

SET IRONKEY=FALSE
SET BONEKEY=FALSE
SET FIREROD=FALSE
SET HAMMER=FALSE
SET CRYSTALSHIELD=FALSE
SET LONGSWORD=FALSE
SET STORMBRINGER=FALSE
REM - THE PLAYER DOES NOT START THE GAME WITH THESE ITEMS.
:START
CLS

:CELL
REM - BEGINNING CELL ROOM. PC SPAWN POINT.

ECHO YOU AWAKEN IN A DARK CHAMBER. THE SMELL OF BLOOD AND URINE ASSAULTS YOUR

ECHO NOSTRILS AS YOU SHAKILY CLIMB TO YOUR FEET. THE ONLY LIGHT SOURCE IS COMING

ECHO FROM A NEARBY ROOM TO THE NORTH. THE CELL DOOR IS PARTIALLY OPEN.

ECHO NOW IS YOUR CHANCE TO ESCAPE!

:MOVECELL
ECHO MOVE? CHOOSE A DIRECTION (N=NORTH, S=SOUTH, E=EAST, W=WEST)
SET /P INPUT=COMMAND?
IF %INPUT%==N GOTO TORTURE
IF %INPUT%==S GOTO NODOORCELL
IF %INPUT%==E GOTO NODOORCELL
IF %INPUT%==w GOTO NODOORCELL

:NODOORCELL
ECHO THERE IS NO DOOR HERE!
GOTO MOVECELL

:TORTURE
CLS
REM - TORTURE CHAMBER
ECHO YOU SEE A LARGE TABLE WITH A BODY STRAPPED TO IT. HIS STOMACH HAS BEEN

ECHO CARVED OPEN, AND BLOOD STAINS THE COLD STONE FLOOR IN A POOL UNDERNEATH

ECHO THE CORPSE. VARIOUS INSTRUMENTS OF TORTURE ARE ORGANIZED AND STOWED UPON THE

ECHO WALL. A CLOSED IRON MAIDEN RESTS IN THE CORNER. YOU CAN SEE TWO DOORS; ONE

ECHO TO THE NORTH, MADE OF SIMPLE WOOD, AND ANOTHER TO THE EAST, MADE OF STURDY

ECHO IRON.

ECHO MOVE OR ACT? (M=MOVE, A=ACT)
SET /P INPUT=COMMAND?
IF %INPUT%==M GOTO MOVETORTURE
IF %INPUT%==A GOTO ACTTORTURE

:MOVETORTURE
ECHO CHOOSE A DIRECTION (N=NORTH, S=SOUTH, E=EAST, W=WEST)
SET /P INPUT=COMMAND?
IF %INPUT%==N GOTO LAB
IF %INPUT%==S GOTO TORTCELL
IF %INPUT%==E (IF %IRONKEY%==TRUE (GOTO ARMORY) ELSE (GOTO LOCKEDARMORY))
IF %INPUT%==W GOTO NODOORTORT

:LAB
ECHO THIS IS THE LAB. COMING SOON!
GOTO END

:TORTCELL
ECHO YOU'VE RETURNED TO YOUR CELL. THIS WON'T HELP YOU ESCAPE!
GOTO MOVECELL

:LOCKEDARMORY
ECHO THE DOOR IS LOCKED!
GOTO MOVETORTURE

:NODOORTORT
ECHO THERE IS NO DOOR HERE!
GOTO MOVETORTURE

Also, if you're altering your code directly you should remove the space between LOCKED and ARMORY in ELSE GOTO LOCKED ARMORY

Hope this helps!
 

DoPo

"You're not cleared for that."
Jan 30, 2012
8,663
0
0
Does it HAVE to be written as a batch script? If it does, does it HAVE to be a text based adventure? Because I think that particular combination is not healthy to...anyone who comes in contact with it. There are some free systems to help you with that, such as ALAN [http://www.alanif.se/], Quest [http://www.textadventures.co.uk/quest/], SUDS [http://www.sudslore.org/], TADS [http://www.tads.org/].

I'm afraid I can't help out more than that - anything more than a 2-3 line quick and dirty batch script is beyond me.
 

DoPo

"You're not cleared for that."
Jan 30, 2012
8,663
0
0
A followup - I decided to take a couple of minutes to stare at your code. In the MOVE OR ACT? section, if you type in anything other than M/A, you go to the code block below that. And you have a lower case 'w' expected in one of the cases in :MOVECELL - not that it changes anything, but I'd just fix it for consistency.

Actually, I think you can fix the :NODOORCELL somewhat - make a new global variable called LASTLOCATION (or whatever) and at the end of each section just set it to the section label. Then ehn somebody reaches :NODOORCELL, you can straight send them back to LASTLOCATION. Here is some pseudocode:

SET LAST LOCATION = FALSE

:CELL
SET LASTLOCATION = :CELL
REM your text here

:MOVECELL
ECHO MOVE? CHOOSE A DIRECTION (N=NORTH, S=SOUTH, E=EAST, W=WEST)
SET /P INPUT=COMMAND?
IF %INPUT%==N GOTO TORTURE
IF %INPUT%==S GOTO NODOOR
IF %INPUT%==E GOTO NODOOR
IF %INPUT%==W GOTO NODOOR

GOTO INVALIDSELECTION

:TORTURE
CLS
SET LASTLOCATION = :TORTURE
REM - your text here

ECHO CHOOSE A DIRECTION (N=NORTH, S=SOUTH, E=EAST, W=WEST)
SET /P INPUT=COMMAND?
IF %INPUT%==N GOTO LAB
IF %INPUT%==S GOTO TORTCELL
IF %INPUT%==E && (%IRONKEY%==TRUE) GOTO ARMORY
ELSE GOTO LOCKED ARMORY
IF %INPUT%==W GOTO NODOOR

GOTO INVALIDSELECTION

:NODOOR
ECHO THERE IS NO DOOR HERE!
GOTO %LASTLOCATION%

:INVALIDSELECTION
ECHO COMMAND NOT RECOGNISED, TRY ONE OF THE OPTIONS BELOW
GOTO %LASTLOCATION%

Well, I think you can do that - as I said, I suck at batch scripting. But I beleve you get the idea. Also, have a look at this [http://stackoverflow.com/questions/2143187/logical-operators-and-or-in-dos-batch] for using AND in batch.

EDIT: Just tested that and it didn't work. I don't want to mess around with it too much, but if you can't use GOTO with a variable, you can still use the variable in the :NODOOR block - just do something along the lines of IF LASTLOCATION == CELL GOTO CELL and so on. Sure it sucks but it's better than making a new code block for each room. You can also re-use the same

codeblock for both :NODOOR and :INVALIDSELECTION, something along the lines of

:NODOOR
ECHO THERE IS NO DOOR HERE!
REM fallthrough

:INVALIDSELECTION
ECHO TRY ONE OF THE OPTIONS BELOW
IF %LASTLOCATION%==CELL GOTO CELL
IF %LASTLOCATION%==MOVETORTURE GOTO MOVETORTURE
IF %LASTLOCATION%==TORTURE GOTO TORTURE

So you're adding a single line for each section for both code blocks.
Otherwise, there is something about using variables as labels here [http://ss64.com/nt/goto.html] but I have no idea how or why it would work.
 

MidnightCat

New member
Jul 21, 2009
125
0
0
DoPo said:
EDIT: Just tested that and it didn't work. I don't want to mess around with it too much, but if you can't use GOTO with a variable, you can still use the variable in the :NODOOR block - just do something along the lines of IF LASTLOCATION == CELL GOTO CELL and so on. Sure it sucks but it's better than making a new code block for each room. You can also re-use the same

codeblock for both :NODOOR and :INVALIDSELECTION, something along the lines of

:NODOOR
ECHO THERE IS NO DOOR HERE!
REM fallthrough

:INVALIDSELECTION
ECHO TRY ONE OF THE OPTIONS BELOW
IF %LASTLOCATION%==CELL GOTO CELL
IF %LASTLOCATION%==MOVETORTURE GOTO MOVETORTURE
IF %LASTLOCATION%==TORTURE GOTO TORTURE

So you're adding a single line for each section for both code blocks.
Otherwise, there is something about using variables as labels here [http://ss64.com/nt/goto.html] but I have no idea how or why it would work.
That's a good idea!
I messed around with it a bit and GOTO does work with a variable, I tried it out with this:

ECHO YOU AWAKEN IN A DARK CHAMBER. THE SMELL OF BLOOD AND URINE ASSAULTS YOUR
ECHO NOSTRILS AS YOU SHAKILY CLIMB TO YOUR FEET. THE ONLY LIGHT SOURCE IS COMING
ECHO FROM A NEARBY ROOM TO THE NORTH. THE CELL DOOR IS PARTIALLY OPEN.
ECHO NOW IS YOUR CHANCE TO ESCAPE!
SET LOCATION=MOVECELL

:MOVECELL
ECHO MOVE? CHOOSE A DIRECTION (N=NORTH, S=SOUTH, E=EAST, W=WEST)
SET /P INPUT=COMMAND?
IF %INPUT%==N GOTO TORTURE
IF %INPUT%==S GOTO INVALIDMOVE
IF %INPUT%==E GOTO INVALIDMOVE
IF %INPUT%==W GOTO INVALIDMOVE
GOTO INVALIDMOVE

:INVALIDMOVE
ECHO THERE IS NO DOOR HERE!
GOTO %LOCATION%

And there could easily be similar blocks with set text for invalid selection, invalid action and so on.

Something else that's neat, I tried INVALIDSELECT for the selection in the Torture Chamber but because the screen is cleared at the beginning of that block the "THAT IS AN INVALID SELECTION" text wouldn't show up. So if you put GOTO TORTURE at the end of that block, if the user doesn't input M or A it'll just keep looping until they do, with no change on the screen.
 

DoPo

"You're not cleared for that."
Jan 30, 2012
8,663
0
0
MidnightCat said:
I messed around with it a bit and GOTO does work with a variable
Hmm, it just crashed for me when I tried it and I had the same thing. But I just ran it straight up, so I might have mistyped something. And it's too late to check what happened, since I deleted the script (like 3 minutes ago...). Anyway, if it works, that's even better

MidnightCat said:
Something else that's neat, I tried INVALIDSELECT for the selection in the Torture Chamber but because the screen is cleared at the beginning of that block the "THAT IS AN INVALID SELECTION" text wouldn't show up. So if you put GOTO TORTURE at the end of that block, if the user doesn't input M or A it'll just keep looping until they do, with no change on the screen.
OK, solution - have a variable with extra text. SET ERRORMSG = FALSE on top and at the beginning of each room, you'll check if ERRORMSG is not false, then show it as part of that room's message and then set it to false. Of course, then the error conditions will just set the variable. In fact, you can build it up as you go along or something similar, so you reduce the number of lines and repeated text. See my example above, you can just rework that, if needed.

EDIT: I'm sure there is a better way to do this, but again - batch scripting. It's not really meant to be elegant.

EDIT 2: OK, slightly more elegant - have a codeblock that shows a room, something like

:SHOWROOM
CLS
IF NOT "ERRORMSG"=FALSE (
ECHO %ERRORMSG%
SET ERRORMSG=FALSE
)
GOTO %LASTLOCATION%
You're saving yourself to type that at the beginning of each room. Going to a room will just be setting the variable and then using GOTO SHOWROOM

EDIT 3: damn IF has some wacky syntax. Fixed it
 

omicron1

New member
Mar 26, 2008
1,729
0
0
You're starting down a perilous road. Before you know it, you'll be writing roguelikes in database scripting languages during your off-hours.
 

Twilight_guy

Sight, Sound, and Mind
Nov 24, 2008
7,131
0
0
First of all, remember to clearly say what language you are using when asking for advise. Not only do people not know every single language by sight, many languages look very similar syntactically and can be hard to differentiate without explicitly stating what they are. Anyways, since your if statements are not using else, any number of them may fire at the same time. Since most of your conditions are mutually exclusive, i.e. input is either E or W not both, its not a problem for most lines. In this one however:
IF %INPUT%==E && (%IRONKEY%==TRUE) GOTO ARMORY
ELSE GOTO LOCKED ARMORY

The line should be something more like:
IF %INPUT%==E
( if (%IRONKEY%==TRUE) GOTO ARMORY
ELSE GOTO LOCKED ARMORY)
So that if input is E then the statement fires and then based on weather or not IronKey is true, you'll either go to armory or locked armory. Otherwise, that else statement will most likely mean that the last if statement can never fire because that else statement would fire either when input is not E or ironkey is not true or both. Its confusing unless you actually map out the logic.

Also, you're going to run into the problem of expandability. Your making a new function for movement and action in each room, when you should attempt to have one function for movement and actions and then just handle the results based on the room. I'd suggest asking your instructor for help with that because its going make your code a great big nightmare very shortly if you don't plan for it.
 

Esotera

New member
May 5, 2011
3,396
0
0


Why do you have to learn to program in a batch script? I strongly recommend you pick up a nicer and more portable language to replace it, or at least on the side.

I don't actually know the syntax of batch that well, but I'd recommend adding in a case for what to do if someone adds something other than N, S, E, or W. At the moment I think your program just does nothing. Also, as you're starting out I'd suggest adding a short one-line comment explaining what each logical section of your program does. You might have done this with REM but again, not too sure on batch.
 

DoPo

"You're not cleared for that."
Jan 30, 2012
8,663
0
0
Twilight_guy said:
Also, you're going to run into the problem of expandability. Your making a new function for movement and action in each room, when you should attempt to have one function for movement and actions and then just handle the results based on the room.
I caught that but I've got no idea how can you do it in a batch script. Aside from making it a great big nightmare and build from there, that is. I'd say the current approach is correct(-ish) - have each room be handled on its own. It's just a homework, not a big assignment or a project that's likely to exist for very long - no need to overengineer it to that extent.
 

DoPo

"You're not cleared for that."
Jan 30, 2012
8,663
0
0
Esotera said:
I don't actually know the syntax of batch that well, but I'd recommend adding in a case for what to do if someone adds something other than N, S, E, or W.
You're going to love this - there is no case statement in batch. However, you could model it

GOTO CASE_%I%
:CASE_0
ECHO i equals 0
GOTO END_SWITCH
:CASE_1
ECHO i equals 1
GOTO END_SWITCH
:END_SWITCH
(Just found the code in Google) Just imagine having two case statements now...


Esotera said:
Also, as you're starting out I'd suggest adding a short one-line comment explaining what each logical section of your program does. You might have done this with REM but again, not too sure on batch.
Yes, REM is used for comments, I mean "remarks". Isn't it obvious? Did I mention how much I don't like batch? I've got no idea why, though.
 

Esotera

New member
May 5, 2011
3,396
0
0
DoPo said:
Esotera said:
I don't actually know the syntax of batch that well, but I'd recommend adding in a case for what to do if someone adds something other than N, S, E, or W.
You're going to love this - there is no case statement in batch. However, you could model it

GOTO CASE_%I%
:CASE_0
ECHO i equals 0
GOTO END_SWITCH
:CASE_1
ECHO i equals 1
GOTO END_SWITCH
:END_SWITCH
(Just found the code in Google) Just imagine having two case statements now...
I think I'm going to have nightmares now...I didn't actually mean the case keyword though, more like this:

Code:
if input = n 
{
    call_something()
}
else 
{
    do_something_else()
}
Then again, it wouldn't surprise me at all if batch doesn't have the else keyword.
 

TrevHead

New member
Apr 10, 2011
1,458
0
0
Cool I loved text adventures as a kid, might I suggest adding some witty jokes for ppl entering swear words.

Half the fun was of getting stuck and resorting to swearing at the computer in frustration lol.