Page 1 of 6
Game code help?
Posted: 18 Jun 2014 12:07
by JWinslow23
I'm trying to debug this code, but for some reason, I don't know what the problem is.
This is code for a game I plan on making.
Code: Select all
@echo off
setlocal enabledelayedexpansion
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
:startloop
cls
for /l %%g in (0,1,15) do echo !board[%%g]!
choice /c wasdx /n /m ""
if %errorlevel%==1 rem UP
if %errorlevel%==2 rem LEFT
if %errorlevel%==3 goto movedown
if %errorlevel%==4 rem RIGHT
:backtoloop
call :tilespawn
if %errorlevel% neq 5 goto startloop
exit /b
:tilespawn
set /a randtile=%random%%%16
if !board[%randtile%]! gtr 0 goto tilespawn
set /a board[%randtile%]=%random%%%10/9*2+2
exit /b
:movedown
set a=3
set j=0
set h=-1
set k=12
set i=0
set g=-4
:movetile
set /a d=0
set /a c=%k%
set /a b=%c%
:1
if !board[%a%+%b%]! neq 0 (
set /a e=!board[%a%+%b%]!
set /a board[%a%+%b%]=0
if %e%==%d% (
set /a board[%a%+%b%-%g%]*=2
set /a d=!board[%a%+%b%-%g%]!
) else (
set /a c+=%g%
set /a board[%c%+%a%+4]=%e%-%d%
set /a d=%e%
)
)
set /a b+=%g%
if %i% neq %b%+%g% goto 1
set /a a+=%h%
if %j% neq %a%+%h% goto movetile
goto backtoloop
Whenever I press S, it displays "< was unexpected at this time" before quitting. I don't understand where the problem is.
When I get help with the :movetile section of code (which I believe has the erroneous code), I will implement the game further. Any help would be greatly appreciated. Ask me if you want more information about the code!
Re: Game code help?
Posted: 18 Jun 2014 12:23
by AiroNG
I don't know if it'll fix your current problem, but this part is wrong:
Code: Select all
choice /c wasdx /n /m ""
if %errorlevel%==1 rem UP
if %errorlevel%==2 rem LEFT
if %errorlevel%==3 goto movedown
if %errorlevel%==4 rem RIGHT
it should be like this:
Code: Select all
choice /c wasdx /n /m ""
if errorlevel 5 goto exit & rem If "x" is supposed to stand for quit
if errorlevel 4 rem RIGHT
if errorlevel 3 goto movedown
if errorlevel 2 rem LEFT
if errorlevel 1 rem UP
"choice" won't work on WinXP machines unless the user has manually installed/copied the choice function.
Re: Game code help?
Posted: 18 Jun 2014 12:35
by JWinslow23
Well, the choice function, for me, was the only way I knew how to have the user make a choice without set /p. :p
And no, that didn't fix my current problem.
(Oh, BTW, this is for 2048, and any help I can have for sliding code (what the movetile label is supposed to do) will help. )
Re: Game code help?
Posted: 18 Jun 2014 12:59
by AiroNG
Again i sadly can't deliver a solution but i found the erroneous code:
that is the point at which the system shows: "
( was unexpected at this time"
The reason is that "!board[%a%+%b%]!" doesn't contain a value.
To find out where things go awry use "@echo on" instead of "@echo off"
It helps alot:
Code: Select all
D:\>choice /c wasdx /n /m ""
S
D:\>if errorlevel 4 rem RIGHT
D:\>if errorlevel 3 goto movedown
D:\>set a=3
D:\>set j=0
D:\>set h=-1
D:\>set k=12
D:\>set i=0
D:\>set g=-4
D:\>set /a d=0
D:\>set /a c=12
D:\>set /a b=12
"(" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
D:\> if ==0 (
D:\>
Re: Game code help?
Posted: 18 Jun 2014 14:23
by JWinslow23
Oh.
Well, how to remedy that situation?
Re: Game code help?
Posted: 18 Jun 2014 16:04
by AiroNG
I was wrong about:
that is the point at which the system shows: "( was unexpected at this time"
The reason is that "!board[%a%+%b%]!" doesn't contain a value.
It's been a while since i dove into Batch...
my sincere apologies about that!
The error can be fixed by using parenthesis or quotes.
example from
ss64.com:
Code: Select all
IF (2) GEQ (15) echo "bigger"
IF "2" GEQ "15" echo "bigger"
That will get rid of the "( was unexpected at this time"-Error.
But leaves you with an endless (because %errorlevel% never gets set to 5) repetition of:
Code: Select all
[...snip...]D:\>if "!board[3+-1928]!" NEQ "0" (
set /a e=!board[3+-1928]!
set /a board[3+-1928]=0
if "" == "0" (
set /a board[3+-1928--4]*=2
set /a d=!board[3+-1928--4]!
) else (
set /a c+=-4
set /a board[-1928+3+4]=-0
set /a d=
)
)
Fehlender Operand
Fehlender Operator
Fehlender Operator
Fehlender Operand
D:\>set /a b+=-4
D:\>if "0" NEQ "-1932+-4" goto 1
[...snip...]
the issue of the empty variable(s) remain however.
"Fehlender" should translate into "missing" while "operator" and "operand" stay the same (i think).
I wont even try to find a solution to your problem until i'm better at batch again.
I'm really embarrassed about my previous mistake
Re: Game code help?
Posted: 18 Jun 2014 16:17
by JWinslow23
Did not work.
Someone, please help me? The same stuff I'm using worked with my tile spawning routine!
Re: Game code help?
Posted: 18 Jun 2014 17:09
by ShadowThief
AiroNG wrote:I don't know if it'll fix your current problem, but this part is wrong:
Code: Select all
choice /c wasdx /n /m ""
if %errorlevel%==1 rem UP
if %errorlevel%==2 rem LEFT
if %errorlevel%==3 goto movedown
if %errorlevel%==4 rem RIGHT
it should be like this:
Code: Select all
choice /c wasdx /n /m ""
if errorlevel 5 goto exit & rem If "x" is supposed to stand for quit
if errorlevel 4 rem RIGHT
if errorlevel 3 goto movedown
if errorlevel 2 rem LEFT
if errorlevel 1 rem UP
Both ways work; there actually is an environment variable called %errorlevel% that you can use to get around the fact that you would otherwise have to call the errorlevel checks in reverse order.
Re: Game code help?
Posted: 18 Jun 2014 17:21
by ShadowThief
Also, your code is breaking at the line if !board[%a%+%b%]! neq 0 ( because you can't do inline addition like that. You first have to set %a%+%b% to some variable and use that when calling the board arraylike.
Re: Game code help?
Posted: 18 Jun 2014 17:29
by JWinslow23
OK, tried that. Did not work.
Now 0 was unexpected at this time!
Code: Select all
@echo on
setlocal enabledelayedexpansion
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
:startloop
cls
for /l %%g in (0,1,15) do echo !board[%%g]!
choice /c wasdx /n /m ""
if %errorlevel%==1 rem UP
if %errorlevel%==2 rem LEFT
if %errorlevel%==3 goto movedown
if %errorlevel%==4 rem RIGHT
if %errorlevel%==5 exit /b
:backtoloop
call :tilespawn
exit /b
:tilespawn
set /a randtile=%random%%%16
if !board[%randtile%]! gtr 0 goto tilespawn
set /a board[%randtile%]=%random%%%10/9*2+2
exit /b
:movedown
set a=3
set j=0
set h=-1
set k=12
set i=0
set g=-4
:movetile
set /a d=0
set /a c=%k%
set /a b=%c%
:1
set /a l=%a%+%b%
if !board[%l%]! neq 0 (
set /a e=!board[%l%]!
set /a board[%l%]=0
if %e% equ %d% (
set /a m=%l%-%g%
set /a board[%m%]*=2
set /a d=!board[%m%]!
) else (
set /a c+=%g%
set /a n=%a%+%c%+4
set /a board[%n%]=%e%-%d%
set /a d=%e%
)
)
set /a b+=%g%
set /a o=%b%+%g%
if %i% neq %o% goto 1
set /a a+=%h%
set /a p=%a%+%h%
if %j% neq %p% goto movetile
goto backtoloop
Re: Game code help?
Posted: 18 Jun 2014 17:54
by ShadowThief
Now it's the if %e% equ %d% ( line. Batch doesn't seem to like that there's an if statement that uses uninitialized variables for some reason. Set e to something before the nested if statement.
Re: Game code help?
Posted: 18 Jun 2014 18:03
by JWinslow23
ShadowThief wrote:Now it's the if %e% equ %d% ( line. Batch doesn't seem to like that there's an if statement that uses uninitialized variables for some reason. Set e to something before the nested if statement.
I did. Did you not see "set /a e=!board[%l%]!" just two lines before?
Also, the earlier line seems like it's still the problem.
Re: Game code help?
Posted: 18 Jun 2014 18:07
by ShadowThief
JWinslow23 wrote:ShadowThief wrote:Now it's the if %e% equ %d% ( line. Batch doesn't seem to like that there's an if statement that uses uninitialized variables for some reason. Set e to something before the nested if statement.
I did. Did you not see "set /a e=!board[%l%]!" just two lines before?
I did. The code doesn't initially go inside the nested if because %l% is 15 and board[15] is initially 0. This means that no code inside the if statement gets run. Batch (for some reason) still expects %e% to have a value, but because no code inside the if statement gets run, %e% is never initialized. You have to set it outside of the if.
Re: Game code help?
Posted: 18 Jun 2014 18:09
by Squashman
JWinslow23 wrote:Did not work.
Someone, please help me? The same stuff I'm using worked with my tile spawning routine!
You have ECHO on. Can you not see all these issues being pointed out.
Re: Game code help?
Posted: 18 Jun 2014 18:24
by JWinslow23
Squashman wrote:JWinslow23 wrote:Did not work.
Someone, please help me? The same stuff I'm using worked with my tile spawning routine!
You have ECHO on. Can you not see all these issues being pointed out.
I see the issue, but I don't know how to fix it.
What I'm trying to do is execute multiple commands in the first IF, including the second IF. Does that not work, and if not, is there a workaround?
EDIT: The output after pressing S:
Code: Select all
C:\Users\Owner\Desktop\Batch Games>if 3 == 1 rem UP
C:\Users\Owner\Desktop\Batch Games>if 3 == 2 rem LEFT
C:\Users\Owner\Desktop\Batch Games>if 3 == 3 goto movedown
C:\Users\Owner\Desktop\Batch Games>set a=3
C:\Users\Owner\Desktop\Batch Games>set j=0
C:\Users\Owner\Desktop\Batch Games>set h=-1
C:\Users\Owner\Desktop\Batch Games>set k=12
C:\Users\Owner\Desktop\Batch Games>set i=0
C:\Users\Owner\Desktop\Batch Games>set g=-4
C:\Users\Owner\Desktop\Batch Games>set /a d=0
C:\Users\Owner\Desktop\Batch Games>set /a c=12
C:\Users\Owner\Desktop\Batch Games>set /a b=12
C:\Users\Owner\Desktop\Batch Games>set /a l=3+12
0 was unexpected at this time.
C:\Users\Owner\Desktop\Batch Games>if equ 0 (