Unable to set Varibale based on choice cmd.

November 23, 2012 at 00:04:14
Specs: Windows 7

Hey all,
This is my first time posting here. I'm new to batch programming.
I am using the choice cmd to check for key hits, w, s, d
w and s move your character up and down, and d should fire a gun.
however, when i try to set the adjacent tile to an x (a bullet) it doesn't do anything.
here is my code:
_____________________________________________________________
@echo off
setlocal enabledelayedexpansion

:SETUP
set P=1
set E=1
set A1=o
set A2=Z
set A3=Z
set A4=Z
set A5=Z
set A6=o
set B1=M
set B2=.
set B3=.
set B4=.
set B5=.
set B6=M
set C1=M
set C2=.
set C3=.
set C4=.
set C5=.
set C6=M
set D1=M
set D2=.
set D3=.
set D4=.
set D5=.
set D6=M
set E1=o
set E2=Z
set E3=Z
set E4=Z
set E5=Z
set E6=o

goto GAME


:GAME
if %P% equ 1 (
set B2=@
set C2=.
set D2=.
)
if %P% equ 2 (
set B2=.
set C2=@
set D2=.
)
if %P% equ 3 (
set B2=.
set C2=.
set D2=@
)
:SHOOT

cls
echo %A1%%A2%%A3%%A4%%A5%%A6%
echo %B1%%B2%%B3%%B4%%B5%%B6%
echo %C1%%C2%%C3%%C4%%C5%%C6%
echo %D1%%D2%%D3%%D4%%D5%%D6%
echo %E1%%E2%%E3%%E4%%E5%%E6%
echo %P%

choice /c wsdq /n /m :
if %errorlevel% equ 4 ( exit )

if %errorlevel% equ 3 (
if %P% equ 1 (
set B2=x
echo SHOOT
)
if %P% equ 2 (
set C2=x
)
if %P% equ 3 (
set D2=x
)
)

if %errorlevel% equ 2 (
if %P% equ 1 (
set P=2
)
if %P% equ 2 (
set P=3
)
)
if %errorlevel% equ 1 (
if %P% equ 3 (
set P=2
)
if %P% equ 2 (
set P=1
)
)


ping localhost -n 1 >nul
goto GAME
______________________________________________

how can I update the B2 variable to show an x? for me it just stays a .

Thanks!


See More: Unable to set Varibale based on choice cmd.

Report •


#1
November 23, 2012 at 01:10:04

ํYou can eliminate the clutter if the if equ with something like this:

:: ===== script starts here ===============
::
:: relith.bat 2012-11-23 16:01:14.35
@echo off & setLocal enableDELAYedeXpansioN

choice /c:wsdq
call :!errorlevel!
goto :eof

:1
echo one
goto :eof

:2
echo two
goto :eof

:3
echo three
goto :eof

:4

::====== script ends here =================

==============================
M2 http://golden-triangle.com


Report •

#2
November 23, 2012 at 12:55:33

You were resetting B2 back to default at top of loop. I changed a few things starting after line 35:
set E6=o
set P1=B2
set P2=C2
set P3=D2
set z=@

:GAME
set B2=.
set C2=.
set D2=.
:: this could also work: set !P%P%!=%z%
if %P% equ 1 set B2=%z%
if %P% equ 2 set C2=%z%
if %P% equ 3 set D2=%z%
::reset z to "default"
set z=@
:SHOOT

cls
echo %A1%%A2%%A3%%A4%%A5%%A6%
echo %B1%%B2%%B3%%B4%%B5%%B6%
echo %C1%%C2%%C3%%C4%%C5%%C6%
echo %D1%%D2%%D3%%D4%%D5%%D6%
echo %E1%%E2%%E3%%E4%%E5%%E6%
echo %P%

choice /c:wsdq /n :
goto :%errorlevel%
goto :game

:4
exit /b

:3 shoot: D
set z=x
goto :timer

:2 move down: S
if %P% lss 3 set /a P+=1
goto :timer

:1 move up: W
if %P% gtr 1 set /a P-=1
goto :timer

:timer
ping localhost -n 1 >nul
goto GAME


Report •

#3
November 27, 2012 at 09:00:39

Thanks guys, my program is all fixed now and working the way I intended it to.
Thanks for the help!

Report •

Related Solutions


Ask Question