The previous post.
In our PROC_executeCommand() function I've added a new local variable - ok%. This starts each loop with a value of TRUE, but is set to false whenever the user's command is not valid.
You should only lose health if you have typed a valid command. |
Testing the program. Notice that the health goes down whenever the player makes a valid move. |
Full source code follows:
REM an adventure game in BB4W
REM by Mr Street
REM version 1.0.0.1 setting up the world
REM version 1.0.0.2 moving around the world
REM version 1.0.0.3 fixing it so you can't walk out of the grid
REM - alternative version -
REM version 1.0.0.4 making the player lose health each turn due to hunger
REM let's set up a 'world'
SIZE% = 3
DIM World$( SIZE%, SIZE% ) : REM creates a 3x3 grid
REM now 'populate' the grid with some space names
PROC_populateWorld
REM now some data about our player
REM player's starting position
x% = 2
y% = 2
REM player's health
health% = 50
PROC_main
DEFPROC_main
LOCAL command$ : REM user's input
REM the main program
REM repeat until the player is dead
REPEAT
REM show the player's current position
PROC_showCurrent
REM get some input from the player
INPUT "What now? > " command$
REM deal with user's input
PROC_executeCommand( command$ )
UNTIL health% <= 0
ENDPROC
DEFPROC_executeCommand( command$ )
REM deals with the users input
LOCAL com$, gridError$
LOCAL ok% : REM variable holds TRUE/FALSE whether the player's input was acceptable
gridError$ = "You can't go in that direction!" : REM a message for when you move off the grid
com$ = FN_convlc( command$ ) : REM convert to lowercase so it is easier
ok% = TRUE : REM assume user's input makes sense
CASE TRUE OF
WHEN com$ = "n" OR com$ = "north"
IF y%<SIZE% THEN
y% += 1
ELSE
PRINT gridError$
ok% = FALSE
ENDIF
WHEN com$ = "s" OR com$ = "south"
IF y% >1 THEN
y% -= 1
ELSE
PRINT gridError$
ok% = FALSE
ENDIF
WHEN com$ = "e" OR com$ = "east"
IF x%<SIZE% THEN
x% += 1
ELSE
PRINT gridError$
ok% = FALSE
ENDIF
WHEN com$ = "w" OR com$ = "west"
IF x%>1 THEN
x% -= 1
ELSE
PRINT gridError$
ok% = FALSE
ENDIF
OTHERWISE
PRINT "I don't understand you!"
ok% = FALSE
ENDCASE
REM if the user's input was acceptable, then they have
REM made one 'turn' and should lose one health point
IF ok% THEN
health% -= 1
ENDIF
ENDPROC
DEFPROC_showCurrent
PRINT "You are at the : "World$(x%, y%)
PRINT "Your health is : "STR$(health%)
ENDPROC
DEF FN_convlc(A$)
REM converts to lower case
SYS "CharLowerBuff", !^A$, LEN(A$)
= A$
DEFPROC_populateWorld
REM puts some names of the spaces into the World
LOCAL x%, y%
REM start with the first row
FOR y% = 1 TO SIZE%
FOR x% = 1 TO SIZE%
READ World$(x%,y%)
NEXT
NEXT
ENDPROC
:
REM the data for our 3x3 grid
DATA "Hills", "Mountains", "Forest"
DATA "Castle", "Village", "Fields"
DATA "Woods", "Swamp", "Lake"