Game code help?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Game code help?

#16 Post by ShadowThief » 18 Jun 2014 18:25

JWinslow23 wrote:
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?

Add the line

Code: Select all

set e=-1
right before the

Code: Select all

if !board[%l%]! neq 0 (
and everything works

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

Re: Game code help?

#17 Post by JWinslow23 » 18 Jun 2014 19:24

ShadowThief wrote:Add the line

Code: Select all

set e=-1
right before the

Code: Select all

if !board[%l%]! neq 0 (
and everything works

But somehow, the final product does not work as expected.

If somebody has a different way of doing this, let me know. For now, here is the non-working code. I'll try to figure out the problem myself, but any help I have would be greatly appreciated.

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
echo !board[0]! !board[1]! !board[2]! !board[3]!
echo !board[4]! !board[5]! !board[6]! !board[7]!
echo !board[8]! !board[9]! !board[10]! !board[11]!
echo !board[12]! !board[13]! !board[14]! !board[15]!
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
if %errorlevel%==5 exit /b
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
set /a l=%a%+%b%
set e=-1
if !board[%l%]! neq 0 (
set /a e=!board[%l%]!
set /a board[%l%]=0
if %e% equ %d% (
set /a board[%l%-%g%]*=2
set /a d=!board[%l%-%g%]!
) else (
set /a c+=%g%
set /a board[%a%+%c%+4]=%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

Sample starting configuration (determined randomly):
0 2 0 0
0 0 0 0
0 0 0 4
0 0 0 0
Expected output:
0 0 0 0
0 0 0 0
0 0 0 0
0 2 0 4

Sample starting configuration:
0 0 2 0
0 0 0 0
0 0 0 0
0 0 2 0
Expected output:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 4 0

You should know what this is code for by now.

EDIT: I have found out the problem is that the IF blocks did not work as expected. When I go through them, the value of e never changes, despite there being a change to e right after the if is entered. Help?

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

Re: Game code help?

#18 Post by Squashman » 18 Jun 2014 20:55

you need to use delayed expansion. the variable e is inside a code block.

Still not understanding what you are trying to do with those board variables. Looks like you are trying to do math in the variable name.

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

Re: Game code help?

#19 Post by ShadowThief » 18 Jun 2014 21:10

I could have sworn he fixed that in a previous post, but I appear to be going crazy.

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Game code help?

#20 Post by Aacini » 18 Jun 2014 21:25

I don't like to read code trying to understand what it does, specially if the code is wrong, so I guess what the code is supposedly to do.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /L %%i in (0,1,3) do (
   for /L %%j in (0,1,3) do (
      set board[%%i;%%j]=0
   )
)
call :tilespawn
call :tilespawn

CLS
:startloop
REM cls
ECHO/
for /L %%i in (0,1,3) do (
   set "line="
   for /L %%j in (0,1,3) do set line=!line! !board[%%i;%%j]!
   echo !line!
)
choice /c wasdx
goto label-%errorlevel%

:label-1 UP
echo UP
goto startloop

:label-2 LEFT
echo LEFT
goto startloop

:label-3 EXIT
exit /b

:label-4 RIGHT
echo RIGHT
goto startloop

:label-5 DOWN
rem Accumulate in last row the values of previous rows
for /L %%j in (0,1,3) do (
   for /L %%i in (0,1,2) do set /A board[3;%%j]+=board[%%i;%%j], board[%%i;%%j]=0
)
goto startloop


:tilespawn
set /A randI=%random% %% 4, randJ=!random! %% 4
if "!board[%randI%;%randJ%]!" neq "0" goto tilespawn
set /A "board[%randI%;%randJ%]=%random% %% 10/9*2+2"
exit /B

I think it would be easier if the OP explains what he wants!

Antonio

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

Re: Game code help?

#21 Post by ShadowThief » 18 Jun 2014 21:38

Aacini wrote:I think it would be easier if the OP explains what he wants!

Antonio


OP wants to create a batch version of http://gabrielecirulli.github.io/2048/

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Game code help?

#22 Post by Aacini » 18 Jun 2014 21:52

JWinslow23 wrote: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. )


Right, but I did NOT knew what "2048" is! (until you posted the link above)

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Game code help?

#23 Post by foxidrive » 18 Jun 2014 23:26

EDIT: I posted while still reading page 1.

You can see two problems you have here

Remove the set b=0 and you will see an error.
The other issue is that the echo command will never fire, because it needs delayed expansion implemented.

Your code suffers in both these ways.

Code: Select all

@echo off
set b=0
   if a==a (
      set b=1
      if %b% EQU 1 echo Win
   )
pause

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

Re: Game code help?

#24 Post by JWinslow23 » 18 Jun 2014 23:43

Everyone, I am trying to code 2048. Everyone, "setlocal enabledelayedexpansion" is the second line of the code. Everyone, is there a solution for my setting variables and changing them with delayed expansion set?
(Trick question: Yes)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Game code help?

#25 Post by foxidrive » 18 Jun 2014 23:53

This code still has the issue: viewtopic.php?p=35260#p35260

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

Re: Game code help?

#26 Post by JWinslow23 » 18 Jun 2014 23:59

OK, apparently, there's something wrong with the algorithm itself. I need help changing it.

Can someone make another sliding subroutine to code in batch?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Game code help?

#27 Post by foxidrive » 19 Jun 2014 00:07

I told you - you didn't implement delayed expansion. That requires more than including a line that has setlocal enabledelayedexpansion


You'd rather be 'smart' in your reply than think about what was provided.

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

Re: Game code help?

#28 Post by ShadowThief » 19 Jun 2014 00:19

JWinslow23 wrote:Everyone, I am trying to code 2048. Everyone, "setlocal enabledelayedexpansion" is the second line of the code. Everyone, is there a solution for my setting variables and changing them with delayed expansion set?
(Trick question: Yes)

change all instances of %e% to !e!

(This is what we mean when we say "use delayed expansion")

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

Re: Game code help?

#29 Post by Squashman » 19 Jun 2014 04:11

ShadowThief wrote:
JWinslow23 wrote:Everyone, I am trying to code 2048. Everyone, "setlocal enabledelayedexpansion" is the second line of the code. Everyone, is there a solution for my setting variables and changing them with delayed expansion set?
(Trick question: Yes)

change all instances of %e% to !e!

(This is what we mean when we say "use delayed expansion")

I thought I made that point clear as mud considering he is using delayed expansion for other variables in that nested code block.

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

Re: Game code help?

#30 Post by ShadowThief » 19 Jun 2014 04:19

Squashman wrote:
ShadowThief wrote:
JWinslow23 wrote:Everyone, I am trying to code 2048. Everyone, "setlocal enabledelayedexpansion" is the second line of the code. Everyone, is there a solution for my setting variables and changing them with delayed expansion set?
(Trick question: Yes)

change all instances of %e% to !e!

(This is what we mean when we say "use delayed expansion")

I thought I made that point clear as mud considering he is using delayed expansion for other variables in that nested code block.

And then he posted "is there a solution for my setting variables and changing them with delayed expansion." I got the impression that I was going to have to be as specific as possible to be understood.

Post Reply