Home > OS >  Problem with using choice command - batch
Problem with using choice command - batch

Time:08-27

i am trying to make character ("o") be able to enter both of the buldings, i've tried everything:

  • detecting %pos% so when its 5 it goes to :fishing store
  • adding "e" to choices so if i press e and %pos% equals 5 them goto :fishing store

(none of them worked, beacause i am doing something wrong and i don't know what)

ill be greatfull if someone will respond quickly

here is the code:

@echo off

title outside
mode 45,10
set/a pos=11

set "y7=[fishing]     [ fanta's ]"
set "y6=[ store ]     [  house  ]"
set "y5=H-------H     H---------H"
set "y4=H_II____H     H_II______H"
set "y3=H___[ ]_H__o__H___[ ]___H__________"
set "y2=::::::::::::::::::::::::H\/\/H\/\/H"
set "y1=::::::::::::::::::::::::H/\/\H/\/\H~~~~~~~~~~"

echo %y7%
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%

goto movement

:movement
choice /c ad /n
if %errorlevel% equ 1 goto left
if %errorlevel% equ 2 goto right

:left
if %pos% gtr 1 set /a pos-=1
goto position

:right
if %pos% lss 32 set /a pos =1
goto position

:position
if %pos% EQU 1 set "y3=o___[ ]_H_____H___[ ]___H__________" 
if %pos% EQU 2 set "y3=Ho__[ ]_H_____H___[ ]___H__________" 
if %pos% EQU 3 set "y3=H_o_[ ]_H_____H___[ ]___H__________"
if %pos% EQU 4 set "y3=H__o[ ]_H_____H___[ ]___H__________" 
if %pos% EQU 5 set "y3=H___[o]_H_____H___[ ]___H__________" 
if %pos% EQU 6 set "y3=H___[ ]oH_____H___[ ]___H__________" 
if %pos% EQU 7 set "y3=H___[ ]_o_____H___[ ]___H__________" 
if %pos% EQU 8 set "y3=H___[ ]_Ho____H___[ ]___H__________" 
if %pos% EQU 9 set "y3=H___[ ]_H_o___H___[ ]___H__________"
if %pos% EQU 10 set "y3=H___[ ]_H__o__H___[ ]___H__________" 
if %pos% EQU 11 set "y3=H___[ ]_H___o_H___[ ]___H__________"
if %pos% EQU 12 set "y3=H___[ ]_H____oH___[ ]___H__________" 
if %pos% EQU 13 set "y3=H___[ ]_H_____o___[ ]___H__________" 
if %pos% EQU 14 set "y3=H___[ ]_H_____Ho__[ ]___H__________"
if %pos% EQU 15 set "y3=H___[ ]_H_____H_o_[ ]___H__________"
if %pos% EQU 16 set "y3=H___[ ]_H_____H__o[ ]___H__________"
if %pos% EQU 17 set "y3=H___[ ]_H_____H___[o]___H__________" 
if %pos% EQU 18 set "y3=H___[ ]_H_____H___[ ]o__H__________" 
if %pos% EQU 19 set "y3=H___[ ]_H_____H___[ ]_o_H__________"
if %pos% EQU 20 set "y3=H___[ ]_H_____H___[ ]__oH__________"
if %pos% EQU 21 set "y3=H___[ ]_H_____H___[ ]___o__________" 
if %pos% EQU 22 set "y3=H___[ ]_H_____H___[ ]___Ho_________" 
if %pos% EQU 23 set "y3=H___[ ]_H_____H___[ ]___H_o________" 
if %pos% EQU 24 set "y3=H___[ ]_H_____H___[ ]___H__o_______" 
if %pos% EQU 25 set "y3=H___[ ]_H_____H___[ ]___H___o______" 
if %pos% EQU 26 set "y3=H___[ ]_H_____H___[ ]___H____o_____" 
if %pos% EQU 27 set "y3=H___[ ]_H_____H___[ ]___H_____o____" 
if %pos% EQU 28 set "y3=H___[ ]_H_____H___[ ]___H______o___" 
if %pos% EQU 29 set "y3=H___[ ]_H_____H___[ ]___H_______o__" 
if %pos% EQU 30 set "y3=H___[ ]_H_____H___[ ]___H________o_"
if %pos% EQU 31 set "y3=H___[ ]_H_____H___[ ]___H_________o"

:fanta's house
cls
echo you are at fanta's house
pause

:fishing store
cls
echo you are at fishing store 
pause

:print
cls
echo %y7% 
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%
if %pos% EQU 5 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fishing store
if %pos% EQU 17 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fanta's house
if %errorlevel% equ 2 goto :left
if %errorlevel% equ 3 goto :right
goto movement

CodePudding user response:

Firstly, make sure your file extension is .cmd and not .bat

Then something like this should help guide you. Just replace your print section with this:

:print
cls
echo %y7% 
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%
if %pos% EQU 5 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fishingstore
if %errorlevel% equ 2 goto :left
if %errorlevel% equ 3 goto :right
if %pos% EQU 17 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fantashouse
if %errorlevel% equ 2 goto :left
if %errorlevel% equ 3 goto :right
goto movement

As you seemed to have changed your original code, try this exact full version:

@echo off

title outside
mode 45,10
set/a pos=11

set "y7=[fishing]     [ fanta's ]"
set "y6=[ store ]     [  house  ]"
set "y5=H-------H     H---------H"
set "y4=H_II____H     H_II______H"
set "y3=H___[ ]_H__o__H___[ ]___H__________"
set "y2=::::::::::::::::::::::::H\/\/H\/\/H"
set "y1=::::::::::::::::::::::::H/\/\H/\/\H~~~~~~~~~~"

echo %y7%
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%

goto movement

:movement
choice /c ad /n
if %errorlevel% equ 1 goto left
if %errorlevel% equ 2 goto right

:left
if %pos% gtr 1 set /a pos-=1
goto position

:right
if %pos% lss 32 set /a pos =1
goto position

:position
if %pos% EQU 1 set "y3=o___[ ]_H_____H___[ ]___H__________" 
if %pos% EQU 2 set "y3=Ho__[ ]_H_____H___[ ]___H__________" 
if %pos% EQU 3 set "y3=H_o_[ ]_H_____H___[ ]___H__________"
if %pos% EQU 4 set "y3=H__o[ ]_H_____H___[ ]___H__________" 
if %pos% EQU 5 set "y3=H___[o]_H_____H___[ ]___H__________"
if %pos% EQU 6 set "y3=H___[ ]oH_____H___[ ]___H__________" 
if %pos% EQU 7 set "y3=H___[ ]_o_____H___[ ]___H__________" 
if %pos% EQU 8 set "y3=H___[ ]_Ho____H___[ ]___H__________" 
if %pos% EQU 9 set "y3=H___[ ]_H_o___H___[ ]___H__________"
if %pos% EQU 10 set "y3=H___[ ]_H__o__H___[ ]___H__________" 
if %pos% EQU 11 set "y3=H___[ ]_H___o_H___[ ]___H__________"
if %pos% EQU 12 set "y3=H___[ ]_H____oH___[ ]___H__________" 
if %pos% EQU 13 set "y3=H___[ ]_H_____o___[ ]___H__________" 
if %pos% EQU 14 set "y3=H___[ ]_H_____Ho__[ ]___H__________"
if %pos% EQU 15 set "y3=H___[ ]_H_____H_o_[ ]___H__________"
if %pos% EQU 16 set "y3=H___[ ]_H_____H__o[ ]___H__________"
if %pos% EQU 17 set "y3=H___[ ]_H_____H___[o]___H__________" 
if %pos% EQU 18 set "y3=H___[ ]_H_____H___[ ]o__H__________" 
if %pos% EQU 19 set "y3=H___[ ]_H_____H___[ ]_o_H__________"
if %pos% EQU 20 set "y3=H___[ ]_H_____H___[ ]__oH__________"
if %pos% EQU 21 set "y3=H___[ ]_H_____H___[ ]___o__________" 
if %pos% EQU 22 set "y3=H___[ ]_H_____H___[ ]___Ho_________" 
if %pos% EQU 23 set "y3=H___[ ]_H_____H___[ ]___H_o________" 
if %pos% EQU 24 set "y3=H___[ ]_H_____H___[ ]___H__o_______" 
if %pos% EQU 25 set "y3=H___[ ]_H_____H___[ ]___H___o______" 
if %pos% EQU 26 set "y3=H___[ ]_H_____H___[ ]___H____o_____" 
if %pos% EQU 27 set "y3=H___[ ]_H_____H___[ ]___H_____o____" 
if %pos% EQU 28 set "y3=H___[ ]_H_____H___[ ]___H______o___" 
if %pos% EQU 29 set "y3=H___[ ]_H_____H___[ ]___H_______o__" 
if %pos% EQU 30 set "y3=H___[ ]_H_____H___[ ]___H________o_"
if %pos% EQU 31 set "y3=H___[ ]_H_____H___[ ]___H_________o"
:print
cls
echo %y7% 
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%
if %pos% EQU 5 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fishingstore
if %pos% EQU 17 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fantashouse
if %errorlevel% equ 2 goto :left
if %errorlevel% equ 3 goto :right
goto movement
goto :EOF

:fantashouse
cls
echo you are at fanta's house
choice /c ce /m "Continue or exit?"
if %errorlevel% equ 2 goto :print
pause
goto :EOF

:fishingstore
cls
echo you are at fishing store 
choice /c ce /m "Continue or exit?"
if %errorlevel% equ 2 goto :print
pause
goto :EOF

CodePudding user response:

@ECHO OFF
SETLOCAL

title outside
rem mode 45,10
set/a pos=11

:position
set "y3=H___[ ]_H_____H___[ ]___H__________"

SET /a y7=pos 1
CALL SET "y3=%%y3:~0,%pos%%%o%%y3:~%y7%%%"

set "y7=[fishing]     [ fanta's ]"
set "y6=[ store ]     [  house  ]"
set "y5=H-------H     H---------H"
set "y4=H_II____H     H_II______H"
set "y2=::::::::::::::::::::::::H\/\/H\/\/H"
set "y1=::::::::::::::::::::::::H/\/\H/\/\H~~~~~~~~~~"

cls
echo %y7%
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%

:: goto movement

:movement
SET "choices=ad"
IF %pos% equ 5 SET "choices=ade"
IF %pos% equ 19 SET "choices=ade"
choice /c %choices% /n
if %errorlevel% equ 1 goto left
if %errorlevel% equ 2 goto right
IF %pos% equ 19 GOTO fanta's
IF %pos% equ 5 GOTO fishing

:left
if %pos% geq 1 set /a pos-=1
goto position

:right
if %pos% lss 32 set /a pos =1
goto position


:fanta's house
cls
echo you are at fanta's house
pause
goto position

:fishing store
cls
echo you are at fishing store
pause
goto position


GOTO :EOF

A somewhat re-written version.

Since all that changes on the map is the position of o, place the o on the appropriate position on the line y3.

the first character in a string is "character 0" (hence, it's legitimate for pos to reach 0 - and gtr becomes geq in left:). Batch can substring using the syntax var:~start,length where start is the starting character number (or character-count from end, if negative) and length is number of characters (if negative, number of characters to not include).

So, we need to select that part of y3 from the start to pos, then the o and then the remainder of y3 starting at pos 1.

Hence - calculate pos 1 as batch can't apply a calculation in the substring formula and put the result in a temporary variable. I chose y7 since it's about to be reset anyway. and then use the parser to complete the extraction.

The parser examines CALL SET "y3=%%y3:~0,%pos%%%o%%y3:~%y7%%%" and replaces, left-to-right (let's assume pos=11, hence y7=12) %% with % and %var% with var's value since the set is being called:

set "y3=% y3:~0, 11 % o % y3:~ 12 %" (spaces inserted to show parser's steps)
set "y3=%y3:~0,11%o%y3:~12%" (as executed)

which places the o appropriately, depending on pos.

Our next step is whether o is at position 5 or 19 (not 17), when we need to allow e as well as a and d in the choice. Simply append the e in these circumstances, and choice will yield errorlevel 1 for a, 2 for d and 3 for e (but only if e is one of the choices).

Then - well, if choice has returned errorlevel 1 or 2, go left/right and if it's neither of those, then it must be 3, so e was pressed and we can look at pos to see which of the two routines to jump to.

Note that a label can't contain spaces, so if you tried fanta's house fanta's shop and fanta's tennis court then only the label fanta's would be interpreted (it's unusual, but not invalid, to use ' in a label). cmd would search for the next label fanta's following the goto as its destination, which might give the illusion of working correctly - but that's a trap...

  • Related