0

when you get eaten, destroyed or you attack, it always runs the next line of script, for instance, you destroy the monster, but then it prints you were eaten and you got a potion but should just say you killed the monster and earned the potion.

570
3 PRINT "welcome to the dungeon, " + name$ + "!"
4 PRINT "monster!!! attack or run away"
5 INPUT co$
6 IF co$ = "attack" GOTO 7 ELSE GOTO 9
7 PRINT "you kill'd the monster"
8 INPUT n$
9 IF n$ = "next" THEN 11
10 PRINT "the monster ate you. Have a fun time in his belly!"
11 PRINT "You won a potion!!!"
12 PRINT "uh oh! You found a dragon"
13 PRINT "Use the potion, attack or run away"
14 INPUT com$
15 IF com$ = "attack" GOTO 18
16 IF com$ = "use potion" THEN 19 ELSE PRINT "fried human for mr dragon!!!"
18 PRINT "bye bye dragon"
19 PRINT "the dragon got to sleep and you got to get away!"
20
  • Could you edit your question to include an example, ie. what the input was, what output you expected to see and what the actual output was? – dave Oct 24 '17 at 01:17
  • Where did this code come from? It's ancient for one thing, but it won't even compile either, since it's missing `THEN` on line 6, and `GOTO` on line 9, etc. This must be made up since you couldn't have even run it in the first place, to know that it falls through. – Dave Cousineau Oct 24 '17 at 01:31
  • This code compiles fine in qb64. – Joe Oct 24 '17 at 15:08
  • I think it is a nice hack at D&D.. That's how I got my start. – eoredson Oct 24 '17 at 23:30
  • @Joe does it permit things like `IF...GOTO` and `IF...THEN – Dave Cousineau Oct 26 '17 at 01:28
  • @DaveCousineau Yes. I couldn't find any official documentation on it, but if you test it, it will allow both cases, even in Qbasic 1.1. – Joe Oct 26 '17 at 14:22
  • The following are valid in QB - IF X THEN 100/IF X GOTO 100/IF X THEN GOTO 100 – eoredson Nov 02 '17 at 23:22

3 Answers3

1

There are various ways to improve your program.

  • Use labels instead of line numbers. If you must use line numbers, at least count by 10s or 100s so that you can insert new lines more easily.
  • Your IF statements are missing END IFs.
  • You are missing THEN and GOTO keywords in a few places.
  • When you're done (eaten by a monster), then exit the program. I'm not sure if there's an actual quit or exit statement, but a label at the end works well enough.
  • Use blank lines to group logically related lines of code together.

Here's a working example that combines all of this:

PRINT "welcome to the dungeon, " + name$ + "!"
PRINT "monster!!! attack or run away"
INPUT co$

IF co$ = "attack" THEN
  GOTO ATTACK
ELSE
  GOTO RUNAWAY
END IF

ATTACK:
PRINT "you kill'd the monster"
GOTO POTION

RUNAWAY:
PRINT "the monster ate you. Have a fun time in his belly!"
GOTO ENDPROGRAM

POTION:
PRINT "You won a potion!!!"
PRINT "uh oh! You found a dragon"
PRINT "Use the potion, attack or run away"
INPUT com$

IF com$ = "attack" THEN GOTO ATTACKDRAGON

IF com$ = "use potion" THEN
   GOTO SLEEPDRAGON
ELSE
   PRINT "fried human for mr dragon!!!"
   GOTO ENDPROGRAM
END IF

ATTACKDRAGON:
PRINT "bye bye dragon"
GOTO ENDPROGRAM

SLEEPDRAGON:
PRINT "the dragon got to sleep and you got to get away!"
GOTO ENDPROGRAM

ENDPROGRAM:
PRINT "done"
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
1

Your game works fine for me when I enter:

attack
next
attack

However, the way you wrote it is very picky. If I don't type that exactly (if I type it in all caps ATTACK or attack with a space at the end), it won't match the if-statement and it'll fall through.

You can force the input to lowercase and trim any spaces with:

INPUT co$
co$ = RTRIM$(LTRIM$(LCASE$(co$)))

This will let you accept a weird input like " aTTaCK ".

Second, it looks like when you run away, the monster is supposed to eat you. Is that your intention? If so, then there's nothing in place to stop it from falling to the next line of code.

6 IF co$ = "attack" GOTO 7 ELSE GOTO 9
7 PRINT "you kill'd the monster"
8 INPUT n$
9 IF n$ = "next" THEN 11
10 PRINT "the monster ate you. Have a fun time in his belly!"
11 PRINT "You won a potion!!!"

On line 10, after the monster eats you, you can either: add another GOTO statement to jump to another part of the program, or END the program. Here's an example to END the program afterward:

6 IF co$ = "attack" GOTO 7 ELSE GOTO 9
7 PRINT "you kill'd the monster"
8 INPUT n$
9 IF n$ = "next" THEN 11
10 PRINT "the monster ate you. Have a fun time in his belly!": END
11 PRINT "You won a potion!!!"

Lastly, in Qbasic, you don't need to enter numbers for each line. That's a nightmare to maintain and bugs like this will keep popping up because they're hard to catch when you write code this way. You can make it easier on yourself by using labels instead. Here's you code with the line numbers removed and replaced with labels (for the GOTO statements):

PRINT "welcome to the dungeon, " + name$ + "!"
PRINT "monster!!! attack or run away"
INPUT co$
IF co$ = "attack" GOTO 7 ELSE GOTO 9
7:
PRINT "you kill'd the monster"
INPUT n$
9:
IF n$ = "next" THEN 11
PRINT "the monster ate you. Have a fun time in his belly!"
END
11:
PRINT "You won a potion!!!"
PRINT "uh oh! You found a dragon"
PRINT "Use the potion, attack or run away"
INPUT com$
IF com$ = "attack" GOTO 18
IF com$ = "use potion" THEN 19 ELSE PRINT "fried human for mr dragon!!!"
18:
PRINT "bye bye dragon"
19:
PRINT "the dragon got to sleep and you got to get away"

You can also use letters for labels, and form descriptive names. Such as:

IF co$ = "attack" GOTO KillMonster ELSE GOTO RunAway
KillMonster:
PRINT "you kill'd the monster"
INPUT n$
RunAway:
IF n$ = "next" THEN GOTO WonPotion:
PRINT "the monster ate you. Have a fun time in his belly!"
END
WonPotion:
PRINT "You won a potion!!!"

The easier it is to read your code, the easier it is to understand and see problems. You'll also have more fun.

Joe
  • 1,330
  • 13
  • 15
0

You could try simple menus for commands:

REM Code snip using menus in QBasic.
AttackMonster:
COLOR 15
PRINT "You encounter a dragon!"
COLOR 14
PRINT "Options:"
PRINT "  (A)ttack"
PRINT "  (R)un away"
PRINT "  (C)ast spell"
PRINT "Enter(A/R/C)";
INPUT P$
P$ = LCASE$(P$)
COLOR 15
IF P$ = "a" THEN PRINT "The dragon died!"
IF P$ = "r" THEN PRINT "You run away screaming!"
IF P$ = "c" THEN PRINT "You cast a spell on the dragon!"
RETURN
eoredson
  • 1,167
  • 2
  • 14
  • 29