News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

BASIC strings in text adventure

Started by zeropolis79, 18:28, 22 July 25

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

zeropolis79

Hi all,

I'm porting a BBC Micro text adventure to the CPC and to maintain a bit of continuity, I'm coding it in BASIC as the Beeb original was also written in BASIC.. Due to the simplicity of the adventure, I'm using strings for it - while I have some strings already working, I'm having problems with some of the others..

Room 3 involves you having to give a bone to a dog.. This you get in room 4 and is covered by the string bone$ which does work. Going west into Room 4 also works. 

If you type north, the string givebone$ is supposed to be checked and if it hasn't happned, you get a message saying you don't have the bone, where it's supposed to say "the dog barks at you". and even when bone$ is positive, it still gives the message.

When you GIVE BONE, the string givebone$ is activated and it tells you the dog wags it's tail which is what is supposed to happen. However, when I type UP, the program still says I don't have the bone. The line for this is

1223 if c$="up" and givebone$="yes" then goto 1500 else goto 1250 (1500 is room 5, not yet coded)
1250 print:print""The dog just barks at you.":call &bb18:goto 1200

1222 covers giving the dog the bone which goes to the message saying it wags it's tail. 
 (if c$="give bone" and bone$="yes" then givebone$="yes" then goto 1270 else goto 1260 (you don't have the bone message)

I'm not sure what I'm doing wrong here unless there is a limit on the number of strings you can set up. (the game already uses three or four for the first locations).

Many thanks. 

(I am aware I can use GAC or that to make it but I wanted to have a bash at a BASIC version) 


Sykobee (Briggsy)

Consider arrays and DATA statements to read in room descriptions, and exits. This also gives you room numbers for free (array index).

sorry my locomotive basic is very rusty atm and the below is more of an idea than working code.

DIM room$(10)
DIM exit%(10,6) ' 10 rooms, 6 directions (this is primitive, should be starting condition of game, not getting into resetting mutable data here)
...
FOR r=1 to 10:READ room$[r]:FOR dir%=1 to 6:READ exit%[r][dir]:NEXT:NEXT
...
DATA "A dark corridor", 2,0,0,0,0,0
DATA "By a pit", 3, 1, 0,0,0,0
...

You can then in the code in the code for "GIVE bone TO dog" set exit%[dog room][dog direction] to be the target room.

andycadley

Is this a line by line conversion from BBC Basic? It might help to compare the two if that's what you're doing. It's difficult to see where your logic is wrong from such a tiny fragment (and as Sykobee is saying having every decision spelt out rather than a data driven system just makes this harder).

However you say:

If you type north, the string givebone$ is supposed to be checked and if it hasn't happned, you get a message saying you don't have the bone

But your code:

1223 if c$="up" and givebone$="yes" then goto 1500 else goto 1250

Is checking for "up" rather than "north" - which seems suspicious. 

You also say:

However, when I type UP, the program still says I don't have the bone.

But up and UP are not the same thing, string comparisons in Locomotive BASIC (and most 80s BASIC variants) are case sensitive. So unless you're converting the input to be all lower case using LOWER$ somewhere else in your program, that may be another source of errors. 

zeropolis79

No, it's not a line by line conversion... The Beeb original uses PROC (prodecures).

As for teh difference between north and up, I made a mistake - whenever I say north, I mean up. 

and the game is currently set for lowercase so i always use lowercase in teh game, and only capitals for this posting 

I considered DATA statements and studied a few adventures but couldn't make head nor tail of them - also the game depends on text in the room descriptions changing when some things happen or are collected. 

arnoldemu

#4
Yes there is a limit to the number of strings and variables but I don't think you've reached that as you would see a MEMORY FULL error.

For values like bone$ and givebone$ in your code which look like they can only be 'yes' or 'no', I suggest you use integer variables and set to 0 for no and 1 for yes. e.g. if bone%=1 and givebone%=1. This will help save on string space in the code itself and variable space.

 to simulate procs use gosub and return.

to debug any issues use prints. e.g. PRINT givebone$ and c$ to check they are as you expect them to be in the code. The line itself looks ok but if you want to be sure about the condition use ().

e.g. 1223 if (c$="up" and givebone$="yes") then goto 1500 else goto 1250 (1500 is room 5, not yet coded)

eto

Quote from: zeropolis79 on 18:28, 22 July 251223 if c$="up" and givebone$="yes" then goto 1500 else goto 1250 (1500 is room 5, not yet coded)
1250 print:print""The dog just barks at you.":call &bb18:goto 1200

1222 covers giving the dog the bone which goes to the message saying it wags it's tail. 
 (if c$="give bone" and bone$="yes" then givebone$="yes" then goto 1270 else goto 1260 (you don't have the bone message)

Can you share the real code of that area? You can save the BASIC as ASCII and export that with an emulator to your PC drive. Then you can copy/paste the code here. Or take a screenshot if it's not too large. 

If you copy only a few lines and manually there's a good chance you add errors that are not in the real code and skip errors that are there but no longer visible here. 

zeropolis79

Quote from: arnoldemu on 08:01, 23 July 25Yes there is a limit to the number of strings and variables but I don't think you've reached that as you would see a MEMORY FULL error.

For values like bone$ and givebone$ in your code which look like they can only be 'yes' or 'no', I suggest you use integer variables and set to 0 for no and 1 for yes. e.g. if bone%=1 and givebone%=1. This will help save on string space in the code itself and variable space.

 to simulate procs use gosub and return.

to debug any issues use prints. e.g. PRINT givebone$ and c$ to check they are as you expect them to be in the code. The line itself looks ok but if you want to be sure about the condition use ().

e.g. 1223 if (c$="up" and givebone$="yes") then goto 1500 else goto 1250 (1500 is room 5, not yet coded)

Thanks for that - how would I turn on the integer variables and check them please? Thanks

arnoldemu

Quote from: zeropolis79 on 09:32, 23 July 25
Quote from: arnoldemu on 08:01, 23 July 25Yes there is a limit to the number of strings and variables but I don't think you've reached that as you would see a MEMORY FULL error.

For values like bone$ and givebone$ in your code which look like they can only be 'yes' or 'no', I suggest you use integer variables and set to 0 for no and 1 for yes. e.g. if bone%=1 and givebone%=1. This will help save on string space in the code itself and variable space.

 to simulate procs use gosub and return.

to debug any issues use prints. e.g. PRINT givebone$ and c$ to check they are as you expect them to be in the code. The line itself looks ok but if you want to be sure about the condition use ().

e.g. 1223 if (c$="up" and givebone$="yes") then goto 1500 else goto 1250 (1500 is room 5, not yet coded)

Thanks for that - how would I turn on the integer variables and check them please? Thanks

There isn't any enabling needed. Using % at the end of the name identifies the variable as integer.

zeropolis79

Thanks...

To the other person who asked, here is the proper code as shown in WinAPE..


zeropolis79

Just tried changing it to bone$=1 and givebone$=1 but got a Type Mismatch.

The thing is, the strings in Rooms 1 and 2 work fine..

Here is the complete listing of the game (50000 onwards is a double height text routine from Amstrad Action - the finished version of the game will include credits for it) (I've just noticed the typo in room 3's up routine re line number, it's all changed in my version)

5 MODE 1
6 BORDER 0:INK 0,0:INK 1,26
9 GOTO 50000
10 GOTO 20000
1000 REM Room 1
1005 CLS
1010 PRINT"You are in a ␏␂lovely garden.␏␁ A path leadseast. A door is west. A note on the doorreads: "
1020 LOCATE 8,3:IF bell$="yes" THEN PRINT "␏␃Use Bell␏␁" ELSE PRINT"␏␃Look for a bell. Go east.␏␁"
1021 LOCATE 8,3:IF open$="yes" THEN PRINT"␏␃Go west now.␏␁"
1030 LOCATE 1,10:INPUT" >> ",a$
1040 IF a$="east" OR a$="e" THEN GOTO 1100
1041 IF a$="west" OR a$="w" AND open$="yes" THEN PRINT:PRINT"The door closes behind you.":CALL &BB18:GOTO 1200
1044 IF a$="ring bell" OR a$="use bell" AND bell$="yes" THEN open$="yes":PRINT:PRINT"␏␃The door opens.␏␁":CALL &BB18:GOTO 1000 ELSE GOTO
1045
1045 PRINT:PRINT"␏␃You must take it first.␏␁":CALL &BB18::GOTO 1000
1050 GOTO 1030
1100 REM Room 2
1110 CLS
1120 PRINT"You are in a forest. To the west a path leads to ␏␂a magic garden.␏␁
1130 PRINT:IF bell$="yes" THEN PRINT " " ELSE PRINT"␏␃There is a bell.␏␁"
1140 LOCATE 1,10:INPUT" >> ",b$
1150 IF b$="take bell" THEN bell$="yes":GOTO 1100
1160 IF b$="west" THEN GOTO 1000
1170 GOTO 1140
1200 REM Room 3
1210 CLS
1215 PRINT"You are in the hall. A big dog sits on  the stairs. An old door is ot the east. To the west is a kitchen."
1220 LOCATE 1,10:INPUT" >> ",c$
1221 IF c$="w" OR c$="west" THEN GOTO 1300
1222 IF c$="give bone" AND bone$=1 THEN givebone$=1:GOTO 1270 ELSE GOTO 1260
1223 IF c$="up" AND givebone$=1 THEN GOTO 1500 ELSE GOTO 1250
1230 GOTO 1220
1250 PRINT:PRINT"The dog just barks at you.":CALL &BB18:GOTO 1200
1260 PRINT:PRINT"You don't have the bone.":CALL &BB18:GOTO 1200
1270 PRINT:PRINT"The dog wags it's tail.":CALL &BB18:GOTO 1200
1300 REM Room 4
1310 CLS
1315 PRINT"This is the kitchen. There are doors    both west and east. A trap-door leads  down to a cellar. "
1316 IF bone$="yes" THEN LOCATE 19,3:PRINT" " ELSE LOCATE 19,3: PRINT"There is a bone here."
1320 LOCATE 1,10:INPUT" >> ",d$
1321 IF d$="take bone" THEN bone$=1:GOTO 1300
1322 IF d$="e" OR d$="east" THEN GOTO 1200
1330 GOTO 1320
1400 REM *** Room 5 ***
1410 PRINT"Room 5"
20000 CLS
20010 choice$="              Choice Page"
20011 CALL &8000,@choice$
20020 PRINT:PRINT:PRINT:PRINT 
20030 choice1$="You can:"
20040 choice1$="        A  Have an Adventure":CALL &8000,@choice1$:PRINT:PRINT
20050 choice2$="        B  See the Notes":CALL &8000,@choice2$:PRINT:PRINT
20060 choice3$="        C  Stop":CALL &8000,@choice3$:PRINT
20070 PRINT:PRINT:INPUT" Type a letter then press ␏␃ENTER␏␁  " ,menu$
20080 IF menu$="a" THEN GOTO 1000
20090 IF menu$="c" THEN |BASIC
20091 IF menu$="b" THEN GOTO 20200
20100 GOTO 20070
20200 REM The Notes
20210 CLS
20220 PRINT"            ␏␃The Lost FrogOC
20230 PEN 1
20240 PRINT:PRINT"A frog who lives in a magic carden has  been lost. Perhaps you can find him and take him back to his garden."
20250 PRINT:PRINT"To look for him, type ␏␂U␏␁ or ␏␂Up␏␁ to go up, ␏␂D␏␁ or ␏␂Down␏␁ to go down, ␏␂W␏␁ or ␏␂West␏␁ to go  west, ␏␂␅␏␁ or ␏␂East␏␁ to go east."
20251 LOCATE 15,25:PRINT"Press ENTER":CALL &BB18
20252 CLS
20253 PRINT"As you look you will find things you    will need to use to help you.
20254 PRINT:PRINT"You can ␏␂TAKE␏␁ something if you find it,  ␏␂USE␏␁ it if you are in danger, and then  ␏␂DROP␏␁ it somewhere, e.g.":PRINT
20260 PRINT:PRINT"                ␏␂Take Bell"
20270 PRINT"                ␏␂Use honey"
20280 PRINT"                ␏␂Drop Bone␏␁"
20290 LOCATE 15,25:PRINT"Press ENTER":CALL &BB18
50000 ' Double height with machine-code
50010 ' RpM    Amstrad Action    Dec 86
50020 '
50030 FOR a=&8000 TO &806D:READ a$
50040 b=b+VAL("&"+a$)
50050 POKE a,VAL("&"+a$):NEXT
50060 IF b<>&3670 THEN PRINT"ERROR":STOP
50070 GOTO 10
50090 DATA DD,66,01,DD,6E,00,7E,F5,C1,E5
50100 DATA DD,E1,DD,23,DD,66,01,DD,6E,00
50110 DATA 2B,C5,23,E5,7E,47,CD,06,B9,F5
50120 DATA 78,CD,A5,BB,DD,21,68,80,06,08
50130 DATA 7E,DD,77,00,DD,23,DD,77,00,DD
50140 DATA 23,23,10,F2,F1,CD,0C,B9,3E,FE
50150 DATA 21,68,80,CD,A8,BB,3E,FF,21,70
50160 DATA 80,CD,A8,BB,3E,FE,CD,5A,BB,3E
50170 DATA 0A,CD,5A,BB,3E,08,CD,5A,BB,3E
50180 DATA FF,CD,5A,BB,3E,0B,CD,5A,BB,E1
50190 DATA C1,10,AE,C9,00,00,00,00,00,00
50200 ' Define a string variable, eg.
50210 ' a$="HELLO, READERS"
50220 ' then CALL &8000,@a$
50230 GOTO 10

eto

BASIC will never get to line 1223.

In 1222 you write 


IF c$="give bone" AND bone$="yes" THEN givebone$="yes":GOTO 1270 ELSE GOTO 1260
The ELSE will always jump to 1260 unless c$ is give bone AND bone$ is yes. 

You probably wanted the else only to happen in case of c$=give bone:

IF c$="give bone" THEN IF bone$="yes" THEN givebone$="yes":GOTO 1270 ELSE GOTO 1260
or
 IF c$="give bone" AND bone$="yes" THEN givebone$="yes":GOTO 1270 ELSE  IF c$="give bone" GOTO 1260



If you want to use integers instead of Strings you don't use $ in the variable name.

So bone=1 will work or bone$="yes" but not bone$=1

Nich

Quote from: zeropolis79 on 15:21, 23 July 25Here is the complete listing of the game (50000 onwards is a double height text routine from Amstrad Action - the finished version of the game will include credits for it)
You will need to add the command MEMORY &7FFF to tell BASIC not to use the area of memory that is used by the double height routine, otherwise your BASIC program could overwrite it and crash the next time it accesses the double height routine.

andycadley

Quote from: zeropolis79 on 15:21, 23 July 25Just tried changing it to bone$=1 and givebone$=1 but got a Type Mismatch
I wouldn't worry about that initially. Strings will use much more memory than integers, so generally it's better to use integers where possible (the classic example being something than can only ever be yes/no for example)

If you aren't getting Out Of Memory errors right now, it's not worth refactoring everything (but something to consider for future)

zeropolis79

Thanks - in no particular order

I've only coded 4 of the 13 rooms of the game and still got some puzzles to go..

1222 - c$ is the input string and it's suppose to check you've typed in give bone and if positive, let you go up the strairs but if a negative, get the message that the dog barks at you.

I've not had out of memory messages yet - I'm doing the game one room at a time with whatever puzzles I can do. I've never used integersin a listing yet... 

Nich - would I need to put that memory command at the beginning of the listing?

Some bits in the text not recreated when I did a LIST#8 to a text file were inbuilt command codes in BASIC to change the colour instead of using PEM x:LOCATE x,x;PRINT"" although in some cases with messages, it was necessary.

The BBC Micro version used all double height text for both room descrption and inputs but I know for this version would be impossible, in addition to having colour text (another part of the Beeb original). 

zeropolis79

Just tried to revised code and it works thanks!

Nich

Quote from: zeropolis79 on 19:33, 23 July 25Nich - would I need to put that memory command at the beginning of the listing?
It doesn't have to be at the beginning but I would definitely advise that it should be used as early as possible in the program.

zeropolis79

Thanks.. at least the game is now making progress.. Hope to finish it during August, health affected by heat permitting. 

Powered by SMFPacks Menu Editor Mod