Confusion with batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Falchiliar
Posts: 1
Joined: 21 Apr 2016 16:41

Confusion with batch file

#1 Post by Falchiliar » 21 Apr 2016 16:50

So recently I started learning Batch-file, and today I decided to try and make a game with it. I have a few errors, and can't figure out what is wrong with the code.
Heres the code:


Code: Select all

@echo off
title Batch Rpg
color 07

echo Welcome to Batch Rpg!
echo.
echo.
pause

:menu
cls
echo.
echo   -Menu-
echo.
echo.
echo.
echo   1) Begin
echo.
echo   2) How to play
echo.
echo   3) Exit
echo.
echo.
echo.
echo   ---------
echo.
   set /p c=Choice:
      if %c%==1 goto prestart1
      if %c%==2 goto howtoplay
      if %c%==3 goto cfr_exit
      if NOT %c%==1 if NOT %c%==2 if NOT %c%==3 goto menu


:cfr_exit
cls
echo.
echo   Are you sure you want to exit?
echo.
   set /p c=(Y/N):
      if %c%=="Y" exit
      if %c%=="N" goto menu
      if NOT %c%=="Y" if NOT %c%=="N" goto cfr_exit2

:cfr_exit2
cls
echo.
echo   You must enter a valid option.
echo.
pause
goto cfr_exit

:howtoplay
cls
echo.
echo   -How to play-
echo.
echo.
echo.
echo   This game is very simple. There will be a number with an option after it, type the option in and it will perform an action(As the option would say).
echo.
pause
goto menu

:prestart1
cls
echo.
echo   Welcome to land of Fageryth!
echo.
echo   What is your name, adventurer?
echo.
   set /p playername=Name:
goto prestart2

:prestart2
cls
echo.
echo   What would be your more valued statistic, Attack damage, or Hit points?
echo.
echo.
echo.
echo   1)Attack damage(Atk)
echo.
echo   2)Hit points(Hp)
echo.
echo.
echo.
echo   ---------
echo.
   set /p playermorevaluedstat=Choice:
      if %playermorevaluedstat%==1
         set playeratk=6
         set playerhp=25
      if %playermorevaluedstat%==2
         set playeratk=4
         set playerhp=30
      if NOT %playermorevaluedstat%==1 if NOT %playermorevaluedstat%==2 goto prestart2
cls
echo playeratk
echo playerhp
pause



I'm having trouble with the ':prestart2' section of my code. With the end of it, I tried to make it where if the variable wasn't equal to 1 or 2, which were the options, then it send the player back up to the start of the section again, and also, when it finishes checking, I'm trying to make it display the two variables 'playeratk' and 'playerhp' but instead it just closes out. I am really lost here and would appreciate the help!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Confusion with batch file

#2 Post by penpen » 22 Apr 2016 02:41

This may help you (untested):
if %playermorevaluedstat%==1 (
set playeratk=6
set playerhp=25
) else if %playermorevaluedstat%==2 (
set playeratk=4
set playerhp=30
else goto :prestart2
cls
echo playeratk=%playeratk%
echo playerhp=%playerhp%
pause


Sidenotes:
Although your code may not provoke an error, you should escape (preceeding ^) the parentheses, or encapsulate them in doublequotes ("encapsulated"),
because parentheses are special characters; test these lines:

Code: Select all

(echo A ^(escaped^) B)
(echo  C "(encapsulated in doublequotes)" D)
(echo E (unsafe) f)

You may use doublequotes with set:

Code: Select all

set /p "c=(Y/N): "

You may use set/A when assigning integer values only:

Code: Select all

set /A "playeratk=6", "playerhp=25"


penpen

Post Reply