Writing an Adventure Game 03

Continuing from yesterday's Writing an Adventure game post, today we will solve the problem of the player walking off the edge of the map and crashing the game.  It is a simple fix to check that the player's movement will not take him/her beyond the allowed boundary.

In our PROC_executeCommand() procedure, you will find some code similar to the following:

      WHEN com$ = "n" OR com$ = "north"
         IF y%<3 THEN
           
y% += 1
         ELSE
           PRINT 
gridError$
         ENDIF

Which, roughly translated means:

WHEN THE PLAYER WANTS TO MOVE NORTH
    IF THE PLAYER IS NOT AT THE EDGE OF THE MAP
        MOVE NORTH
    ELSE
        DISPLAY AN ERROR MESSAGE

A similar construction occurs at all of the compass directions.

A simple fix to check for walking off the edge of the map.  In fact, you could allow the player to move off, but wrap around so that they appear on the other side of the map instead.

Next time - adding objects.

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 let's set up a 'world'
     
DIM World$(3,3) : 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$
     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
     
CASE TRUE OF
       WHEN 
com$ = "n" OR com$ = "north"
         IF y%<3 THEN
           
y% += 1
         ELSE
           PRINT 
gridError$
         ENDIF

       WHEN 
com$ = "s" OR com$ = "south"
         IF y% >1 THEN
           
y% -= 1
         ELSE
           PRINT 
gridError$
         ENDIF

       WHEN 
com$ = "e" OR com$ = "east"
         IF x%<3 THEN
           
x% += 1
         ELSE
           PRINT 
gridError$
         ENDIF

       WHEN 
com$ = "w" OR com$ = "west"
         IF x%>1 THEN
           
x% -= 1
         ELSE
           PRINT 
gridError$
         ENDIF

     ENDCASE
     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 3
       FOR x% = 1 TO 3
         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"



In fact, a slightly improved program would have a constant variable that sets the size of the game world, allowing the programmer to change the size of the game world to any size they desire without breaking this bit.  For the purpose of these posts we shall keep the size of the world fixed at '3', however I might be tempted to code like this instead:


     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 let's set up a 'world'
     
SIZE% = 3 : rem <--- sets the size of the world
     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$
     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
     
CASE TRUE OF
       WHEN 
com$ = "n" OR com$ = "north"
         IF y%<SIZE% THEN
           
y% += 1
         ELSE
           PRINT 
gridError$
         ENDIF

       WHEN 
com$ = "s" OR com$ = "south"
         IF y% >1 THEN
           
y% -= 1
         ELSE
           PRINT 
gridError$
         ENDIF

       WHEN 
com$ = "e" OR com$ = "east"
         IF x%<SIZE% THEN
           
x% += 1
         ELSE
           PRINT 
gridError$
         ENDIF

       WHEN 
com$ = "w" OR com$ = "west"
         IF x%>1 THEN
           
x% -= 1
         ELSE
           PRINT 
gridError$
         ENDIF

     ENDCASE
     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"