Game code help?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Game code help?

#31 Post by Squashman » 19 Jun 2014 08:26

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

Yes there is a solution if you use your variables correctly and no it is not a trick question.

Maybe this will turn the light bulb on for you.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set b=0
   if a==a (
      set b=1
      echo %b%
      echo !b!
   )
pause

Output

Code: Select all

0
1
Press any key to continue . . .

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

Re: Game code help?

#32 Post by JWinslow23 » 19 Jun 2014 11:06

Everybody, I am using delayed expansion in the sense that all of the variables needed to be put in !!s are put in !!s. I am not that stupid.

Anyways, so my sliding code, after all of that, does not work.
Here it is (I think I may need a different method; what I'm using is pretty contrived anyways):

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
:startloop
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
if %errorlevel%==5 exit /b
:backtoloop
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%]!
echo !e!
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

Do we not see the !!s in the if blocks?

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

Re: Game code help?

#33 Post by Squashman » 19 Jun 2014 11:12

JWinslow23 wrote:Everybody, I am using delayed expansion in the sense that all of the variables needed to be put in !!s are put in !!s. I am not that stupid.

Do we not see the !!s in the if blocks?

YES and YES.

Look at all your previous code posts. Show me one previous code post of yours that used !e! as a variable.

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

Re: Game code help?

#34 Post by Aacini » 19 Jun 2014 11:16

JWinslow23 wrote:If somebody has a different way of doing this, let me know...


Didn't you reviewed my answer above? (about the middle of the page # 2). If that is not exactly what you want, I think it would be easier to modify that method until you get the desired result...

Antonio

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

Re: Game code help?

#35 Post by Squashman » 19 Jun 2014 11:17

How many times have we told you can't do this.

Code: Select all

set /a board[%l%-!g!]*=2
set /a board[!a!+!c!+4]=!e!-!d!

You can't do math in the variable name!

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

Re: Game code help?

#36 Post by JWinslow23 » 19 Jun 2014 11:46

I am sorta a newb at Batch, don't blame me for any mistakes I made. I'm fixing them as we speak.

Aacini, your code, neither in the way it is, nor in a modified form, works properly. The setup for arrays that I am doing are board[0], board[1], board[2] all the way to board[15]. Not board[x;y], as you specified.

I am rewriting the algorithm right now, and until I finish, there shall be no more discussion here. Mind you, I just started with Batch a few days ago. :|

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

Re: Game code help?

#37 Post by foxidrive » 19 Jun 2014 12:33

JWinslow23 wrote:Everybody, I am using delayed expansion in the sense that all of the variables needed to be put in !!s are put in !!s. I am not that stupid.

Do we not see the !!s in the if blocks?



You just changed them in your latest code. Why do you tell porky pies instead of being honest?

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

Re: Game code help?

#38 Post by JWinslow23 » 19 Jun 2014 12:43

foxidrive wrote:
JWinslow23 wrote:Everybody, I am using delayed expansion in the sense that all of the variables needed to be put in !!s are put in !!s. I am not that stupid.

Do we not see the !!s in the if blocks?



You just changed them in your latest code. Why do you tell porky pies instead of being honest?

Fine. You can try and argue with me about the placement of percent signs and exclamation points, or you could try and be productive. (sorry if I sound rude)

I think I can figure it out by myself now. As of now, this thread is not to be discussed anymore.

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

Re: Game code help?

#39 Post by Squashman » 19 Jun 2014 13:57

We were trying to be productive by pointing out the missing exclamations to you several times and your use of trying to do math inline with IF statements and to the left of the equals sign when using SET /a.

By saying that we can't see it after you changed your code is making us sound like we are crazy or stupid when all you had to do was look at your previous posts and our previous code suggestions and admit you were wrong.

We are not here to be the bad guys but you sure are making us feel that way.

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

Re: Game code help?

#40 Post by Aacini » 19 Jun 2014 14:35

@JWinslow23:

In this thread you have not explained what you want, just shown us a code segment. I don't know what 2048 nor "sliding code" are, but you posted this expected output:

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


So I assumed (perhaps incorrectly) that your ":movedown" routine must accumulate in last row the values of previous rows and reset they to zero after, so I wrote a code that do that. Also, I changed the letters in CHOICE command so X=movedown (because it is down in the keyboard), but this point is clearly marked in my code: ":label-3 EXIT", ":label-5 DOWN". If you want that S be down and X exit, just reorder the string in CHOICE command...

This is an output example of my program:

Code: Select all

 2 0 0 0
 4 0 0 0
 0 0 0 0
 0 0 0 0
[W,A,S,D,X]?X

 0 0 0 0
 0 0 0 0
 0 0 0 0
 6 0 0 0
[W,A,S,D,X]?S

-----------------

 2 0 0 0
 0 0 2 0
 0 0 0 0
 0 0 0 0
[W,A,S,D,X]?X

 0 0 0 0
 0 0 0 0
 0 0 0 0
 2 0 2 0
[W,A,S,D,X]?S

-----------------

 0 0 0 0
 2 0 0 0
 0 0 0 0
 0 0 0 2
[W,A,S,D,X]?X

 0 0 0 0
 0 0 0 0
 0 0 0 0
 2 0 0 2
[W,A,S,D,X]?S


The board is a two-dimensional arrangement of numbers, so the "natural" way to implement it is via a two-dimensional array. Of course, you may manage it via a one-dimensional array and perform conversions of the required two-dimensional indices into a single index that map the equivalent position, but this method is more difficult to implement...

Antonio

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

Re: Game code help?

#41 Post by JWinslow23 » 19 Jun 2014 19:30

Aacini wrote:@JWinslow23:

In this thread you have not explained what you want, just shown us a code segment. I don't know what 2048 nor "sliding code" are, but you posted this expected output:

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


So I assumed (perhaps incorrectly) that your ":movedown" routine must accumulate in last row the values of previous rows and reset they to zero after, so I wrote a code that do that. Also, I changed the letters in CHOICE command so X=movedown (because it is down in the keyboard), but this point is clearly marked in my code: ":label-3 EXIT", ":label-5 DOWN". If you want that S be down and X exit, just reorder the string in CHOICE command...

This is an output example of my program:

Code: Select all

 2 0 0 0
 4 0 0 0
 0 0 0 0
 0 0 0 0
[W,A,S,D,X]?X

 0 0 0 0
 0 0 0 0
 0 0 0 0
 6 0 0 0
[W,A,S,D,X]?S

-----------------

 2 0 0 0
 0 0 2 0
 0 0 0 0
 0 0 0 0
[W,A,S,D,X]?X

 0 0 0 0
 0 0 0 0
 0 0 0 0
 2 0 2 0
[W,A,S,D,X]?S

-----------------

 0 0 0 0
 2 0 0 0
 0 0 0 0
 0 0 0 2
[W,A,S,D,X]?X

 0 0 0 0
 0 0 0 0
 0 0 0 0
 2 0 0 2
[W,A,S,D,X]?S


The board is a two-dimensional arrangement of numbers, so the "natural" way to implement it is via a two-dimensional array. Of course, you may manage it via a one-dimensional array and perform conversions of the required two-dimensional indices into a single index that map the equivalent position, but this method is more difficult to implement...

Antonio

I implemented it on a TI-84+ with a one-dimensional array. Why not in Batch? In fact, my original code was an attempt at directly converting the code from Axe (a programming language that compiles directly to z80 machine code) to Batch. And it's been done by many others! (URLs withheld due to there being too many to allow in one post)

And "2048" is a game in which you have to slide tiles in order to get to a 2048 tile (and only like tiles are allowed to merge). "Sliding code" is just my way of saying "code which performs the tile slide".

Also:
0 0 4 0
0 0 0 0
0 0 0 0
0 0 2 0
turns to
0 0 0 0
0 0 0 0
0 0 4 0
0 0 2 0

, and
0 2 0 0
0 2 0 0
0 2 0 0
0 2 0 0
turns to
0 0 0 0
0 0 0 0
0 4 0 0
0 4 0 0

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

Re: Game code help?

#42 Post by Squashman » 19 Jun 2014 20:05

Are you confusing BASIC with Batch (.bat)?

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

Re: Game code help?

#43 Post by JWinslow23 » 19 Jun 2014 20:27

They're actually pretty similar. They are both limited capability, high-level interpreted programming languages that generally have a limited area of functionality, and require hackish workarounds in the form of assembly utilities or well-programmed, highly efficient algorithms in order to be considered a programming language worth using, and even begin to compare to a mainstream language such as C++ or Flash. :P

But yeah, I've also seen it done in the Linux terminal (actually the inspiration for this project), so what's a port from OS to OS? I'd imagine it wouldn't be hard. I mean, another Windows version is on the same page!

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

Re: Game code help?

#44 Post by foxidrive » 19 Jun 2014 21:03

JWinslow23 wrote:
foxidrive wrote:You just changed them in your latest code. Why do you tell porky pies instead of being honest?

Fine. You can try and argue with me about the placement of percent signs and exclamation points, or you could try and be productive. (sorry if I sound rude)


JWinslow23, do you have a reading comprehension disability?

That would explain your unrelated comments to me and why you failed to understand all the hints and solutions you received.

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

Re: Game code help?

#45 Post by ShadowThief » 19 Jun 2014 21:20

JWinslow23 wrote:They're actually pretty similar. They are both limited capability, high-level interpreted programming languages that generally have a limited area of functionality, and require hackish workarounds in the form of assembly utilities or well-programmed, highly efficient algorithms in order to be considered a programming language worth using, and even begin to compare to a mainstream language such as C++ or Flash. :P

But yeah, I've also seen it done in the Linux terminal (actually the inspiration for this project), so what's a port from OS to OS? I'd imagine it wouldn't be hard. I mean, another Windows version is on the same page!

Yeah, but that's Python, which is SUPER advanced compared to batch (and can also be run on Windows). Now if you could find a 2048 port in bash or kornshell, that would be comparable and we could probably make a port happen.

Post Reply