BBC BASIC for Windows
Programming >> BBC BASIC language >> PROC's
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1361716933

PROC's
Post by coolguy on Feb 24th, 2013, 1:42pm

when i run this code the reply is "bad call", how come this wouldn't work and what could be done to fix this??/

IF Lowerstrength%<=0 PRINT "Player 1 is DEAD!" ELSE
PROC _s
IF Lowerskill%<=0 PRINT " The skill of player 1 is less than 0 so it is stored as 0."
IF Lowerstrength2%<=0 PRINT "Player 2 is DEAD!"
IF Lowerskill2%<=0 PRINT " The skill of player 2 is less than 0 so it is stored as 0."
ENDIF

DEF PROC _s
IF Lowerstrength2% AND Lowerstrength%>=0 THEN PRINT " The strength of both players is more than 0"
ENDIF
INPUT "Do u wish to restart or draw the game? Restart/Draw? " Option2$
ENDPROC
UNTIL Option2$= "Draw"

Re: PROC's
Post by Malvern on Feb 24th, 2013, 6:28pm

PROC must be followed by the name without any spaces in between.
Re: PROC's
Post by coolguy on Feb 25th, 2013, 7:43pm

Thank you for the help, i have another question... when i use the following code, the computer always PRINTS "Player 2 is dead!", why is that?

IF B%>A% PRINT "Player 1 has a lower score so the new total strength of Player 1 is : ";Lowerstrength%
IF B%>A% PRINT "Player 1 has a lower score so the new total skill of Player 1 is : ";Lowerskill%
IF A%>B% PRINT "Player 2 has a lower score so the new total strength of Player 2 is : ";Lowerstrength2%
IF A%>B% PRINT "Player 2 has a lower score so the new total skill of Player 2 is : ";Lowerskill2%
ENDIF

IF Lowerstrength% <=0 PRINT "Player 1 is DEAD!"
IF Lowerskill%<=0 PRINT " The skill of player 1 is less than 0 so it is stored as 0."
IF Lowerstrength2% <=0 PRINT "Player 2 is DEAD!"
IF Lowerskill2%<=0 PRINT " The skill of player 2 is less than 0 so it is stored as 0."
IF Lowerstrength% AND Lowerstrength2%>=0 THEN PROC_s
ENDIF
END

DEF PROC_s
INPUT "Do u wish to restart or draw the game? Restart/Draw? " Option2$
UNTIL Option2$= "Draw"
ENDPROC


Re: PROC's
Post by Malvern on Feb 25th, 2013, 8:51pm

I would predict that you are not changing Lowerstrength2% like you think you are. Use the List Variables utility to see what the values are when running or put in a PRINT Lowerstrength2% statement to see what you actually have.
Re: PROC's
Post by sbracken on Feb 26th, 2013, 2:52pm

In addition to not doing what you want it to do (which is impossible to diagnose without the seeing the code that affects the values of your variables), your code has quite a few problems!

Firstly, you do not need either of the ENDIF statements. ENDIF is only required when you have a multiline IF-THEN-ELSE, along the lines of:

Code:
IF strength%>0 THEN
   PRINT "YOU ARE ALIVE"
ELSE
   PRINT "YOU ARE DEAD"
ENDIF 


which is the same as

Code:
IF strength%>0 THEN PRINT "YOU ARE ALIVE" ELSE PRINT "YOU ARE DEAD" 


Your first few lines check if A% is greater than B% or if B% is greater than A%, but make no allowance for the possibility that A%=B%.

The line:

Code:
IF Lowerstrength% AND Lowerstrength2%>=0 THEN PROC_s 


would actually be interpreted as:

Code:
IF Lowerstrength%=TRUE AND Lowerstrength2%>=0 THEN PROC_s 


BB4W does not have true boolean variables (TRUE and FALSE), so TRUE usually equates to -1 and FALSE to 0. However, when you test if something is true as part of an IF-THEN-ELSE statement, it is evaluated numerically. This means that anything that is not zero is considered true. In your case, PROC_s would be called if Lowerstrength% was any negative or positive number and Lowerstrength2% was greater than or equal to zero. You probably meant:

Code:
IF Lowerstrength%>=0 AND Lowerstrength2%>=0 THEN PROC_s 


Finally, PROC_s has an UNTIL statement that makes no sense. UNTIL must follow a REPEAT statement.

Code:
REPEAT
   INPUT"Do you want to draw restart or draw the game? Restart/Draw? "Option2$
UNTIL Option2$="Draw" 


Even with the REPEAT added, this is still not right. You are giving two options, but waiting for the player to respond with only one of them! How would the player signal they want to Restart? Also, expecting a player to type what they want to do and to get the capitalisation correct is not very user friendly. What happens if they actually type "DRAW" or "draw" or DrAw"? Giving them the option of pressing a single key would be better:

Code:
PRINT "Press R to Restart or D to Draw the game"
REPEAT
   g=GET
UNTIL g=68 OR g=100 OR g=82 OR g=114
IF g=68 OR g=100 THEN
   option2$="Draw"
ELSE
   option2$="Restart"
ENDIF 


g=GET waits for a key to be pressed and sets the variable g with the ASCII value of the key (68 is d, 82 is r, 100 is D and 114 is R - the player may decide to press SHIFT, or have CAPS LOCK on!). The REPEAT-UNTIL loop ensures that the program waits for the correct input, which can then be used to direct the program appropriately.

Hope this helps.
Simon
Re: PROC's
Post by coolguy on Feb 26th, 2013, 4:40pm

Hi simon,

This really did help a lot, i do have code above what i have shown so the repeat and Until will make sense when the whole code is displayed.

I really appreciate your help , thanking you