mardi 22 décembre 2015

How can I CALL a random number generator, return a result, run an IF-ELSE parameter, and GOTO depending on the result?

I'm a fairly new coder just messing around as a hobby. I'm creating a text game in batch and wanted to throw in some variables that would branch the storyline depending on the result of actions based somewhat on random number chance.

I managed to find and execute a code for a random number generator (not shown here) in the batch file itself, under a :LABEL, which worked fine. However, when I tried to CALL the LABEL from another location in the file, the generator would not work at all and gave me an error ('NUMBER' is not recognized as an internal or external command, operable program or batch file). I want to use CALL so that I can return to the spot after the command is executed.

So, how can I go about completing this task? Sorry I'm not very good at this stuff yet, but I'm trying. I'm sure I'm missing key information. I will layout an example of how I want the flow to go:

@echo off  

:FORKINROAD
ECHO You arrive at a fork in the road, and must choose only one...
timeout /t 5 >nul
ECHO.
ECHO A: Path of Success
ECHO B: Path of Failure
ECHO.
SET/p input=What would you like to do?
IF %input%==A CALL NUMBER
IF %input%==B GOTO PATH2
ECHO.
ECHO Invalid command
timeout /t 3 /nobreak >nul
GOTO FORKINROAD

ECHO.
ECHO %NUM%
(if %NUM% GEQ 50 GOTO PATH1)
(IF %NUM% LSS 50 GOTO PATH2)

:NUMBER
(generate random number from 1-100) we can call it %NUM%
GOTO :EOF (to return back to the spot where the CALL was initiated?)

:PATH1
ECHO.
ECHO You proceed down the path of success...
timeout /t 3 /nobreak >nul
exit

:PATH2
ECHO.
ECHO You embark on the path of complete failure and anguish...
timeout /t 3 /nobreak >nul
exit

Aucun commentaire:

Enregistrer un commentaire