Game code help?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Game code help?

#1 Post by JWinslow23 » 18 Jun 2014 12:07

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!

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: Game code help?

#2 Post by AiroNG » 18 Jun 2014 12:23

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.

JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Re: Game code help?

#3 Post by JWinslow23 » 18 Jun 2014 12:35

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. )

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: Game code help?

#4 Post by AiroNG » 18 Jun 2014 12:59

Again i sadly can't deliver a solution but i found the erroneous code:

Code: Select all

if !board[%a%+%b%]! neq 0 (


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:\>

JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Re: Game code help?

#5 Post by JWinslow23 » 18 Jun 2014 14:23

Oh. :?

Well, how to remedy that situation?

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: Game code help?

#6 Post by AiroNG » 18 Jun 2014 16:04

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... :oops:
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 :(

JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Re: Game code help?

#7 Post by JWinslow23 » 18 Jun 2014 16:17

Did not work. :(

Someone, please help me? The same stuff I'm using worked with my tile spawning routine!

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Game code help?

#8 Post by ShadowThief » 18 Jun 2014 17:09

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.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Game code help?

#9 Post by ShadowThief » 18 Jun 2014 17:21

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.

JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Re: Game code help?

#10 Post by JWinslow23 » 18 Jun 2014 17:29

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

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Game code help?

#11 Post by ShadowThief » 18 Jun 2014 17:54

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.

JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Re: Game code help?

#12 Post by JWinslow23 » 18 Jun 2014 18:03

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.
Last edited by JWinslow23 on 18 Jun 2014 18:09, edited 1 time in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Game code help?

#13 Post by ShadowThief » 18 Jun 2014 18:07

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Game code help?

#14 Post by Squashman » 18 Jun 2014 18:09

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.

JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Re: Game code help?

#15 Post by JWinslow23 » 18 Jun 2014 18:24

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 (
Last edited by JWinslow23 on 18 Jun 2014 18:44, edited 1 time in total.

Post Reply