Question

First off, I read everybody else's questions that were similar, but none of the given solutions worked. This is also one of my first batch files. The idea behind it is that I press [D] to move the block X to the right, and [A] to move it left. Here is the script.

@echo off
:start
set z=%q%
if "%z%"=="" set z=5
choice /c ad
set x=%errorlevel%
if %x%==a goto a
if %x%==d goto b
:sett
set /a q="%z% %y% 1"
if %q%==1 goto 1
if %q%==2 goto 2
if %q%==3 goto 3
if %q%==4 goto 4
if %q%==5 goto 5
if %q%==6 goto 6
if %q%==7 goto 7
if %q%==8 goto 8
if %q%==9 goto 9
if %q%==10 set q=1 && goto 1
pause
:a
set y="-"
goto sett
:b
set y="+"
goto sett
:1
echo.X
goto start
:2
echo..X
goto start
:3
echo...X
goto start 
:4
echo....X
goto start
:5
echo.....X
goto start 
:6
echo......X
goto start
:7
echo.......X
goto start
:8
echo........X 
goto start
:9
echo.........X
goto start

I think (gathered from other people's posts), that the problem is either in the

set /a q="%z% %y% 1"

or

set y="-"

or

set y="+"

I know there are probably much easier ways to accomplish this, but this is how I learn. The hard way.

Was it helpful?

Solution

The problem is likely a combination.

The syntax set "var=string" sets tha value of var to string, whereas set var="string" sets the value of var to "string".

The first form is used to ensure that any trailing spaces on the line are not included the value assigned. The second includes quotes in the value assigned.

hence your set /a q... command would be resolved to

set /a q="5 "+" 1"

The solution is to use

set "y=+"

and

set /a q=%z% %y% 1

OTHER TIPS

In addition to Magoo's answer - You have misunderstood ErrorLevel and choice

choice /c ad
set x=%errorlevel%
if %x%==a goto a
if %x%==d goto b

The ErrorLevel variable will only ever expand to a number. In the case of the choice command, it will expand to 1 through however many possible values you have provided. Expample

choice /c abc

If the user presses the a key, then %errorlevel% will expand to 1. Whereas if they press b, it will expand to 2, and so on.

So therefore both of your if checks are failing and you are going straight to :sett.

You should instead check if %x% equals 1 or 2.

choice /c ad
set x=%errorlevel%
if %x%==1 goto a
if %x%==2 goto b

a similar concept of doing the exact same thing ..... hope it helps .... got this code some place else

@setlocal enableextensions enabledelayedexpansion
@echo off
title movement
color 0a
set length=
set height= a
:controls
cls
echo Use WASD to move your character ([]).
echo.
for %%a in ( %height% ) do echo.
echo %length%[]
choice /c wasd /n
if %errorlevel% equ 1 call:up
if %errorlevel% equ 2 call:left
if %errorlevel% equ 3 call:down
if %errorlevel% equ 4 call:right
:left
setlength=!length:~0,-1!
goto controls
:right
set length=%length%
goto controls
:up
set height=!height:~0,-2!
goto controls
:down
set height=%height% a
goto control
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top