2048 for Batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

2048 for Batch

#1 Post by JWinslow23 » 03 Jul 2014 19:09

After a ton of testing and tweaking, the first official release is here! :D
(modedit see here for the development thread viewtopic.php?f=3&t=5701 )

Image
At 5,248 bytes, this packs a lot of features in a relatively small package! (Sure, it's not 1KB, but it's featureful :P )
Use the WASD keys to move the tiles, the N key to start a new game, and the X key to exit. It saves automatically after every move, and it automatically loads your saved games (given the savefile, 2048JWinslow.dat, is in the same directory).

Please tell me what you think of it (sloppy coding style aside :P ).

Source code (v1.1):

Code: Select all

@echo off&setlocal enabledelayedexpansion&title 2048&mode con: cols=32 lines=46&color F0
:startgame
if exist "2048JWinslow.dat" (for /f "delims=" %%g in (2048JWinslow.dat)do %%g) else set/ascore=0,winstate=0,bestscore=0&for /l %%g in (0,1,15)do set board[%%g]=0
set/amoves=0,h=0&for /l %%g in (0,1,15)do if !board[%%g]!==0 set/ah+=1
if %h%==16 call :tilespawn&call :tilespawn
:startloop
if %score% gtr %bestscore% set bestscore=%score%
call :drawboard 0&call :savegame&choice /c wasdxn /n /m ""
if %errorlevel%==1 call :rotateclockwise&call :rotateclockwise&call :rotateclockwise&call :compress&call :merge&call :compress&call :compress&call :rotateclockwise
if %errorlevel%==2 call :compress&call :merge&call :compress&call :compress
if %errorlevel%==3 call :rotateclockwise&call :compress&call :merge&call :compress&call :compress&call :rotateclockwise&call :rotateclockwise&call :rotateclockwise
if %errorlevel%==4 call :rotateclockwise&call :rotateclockwise&call :compress&call :merge&call :compress&call :compress&call :rotateclockwise&call :rotateclockwise
if %errorlevel%==5 goto end
if %errorlevel%==6 (set/ascore=0,winstate=0&set moves=0&for /l %%g in (0,1,15)do set board[%%g]=0
call :tilespawn&call :tilespawn&goto startloop)
set/aboardchanged=0,tiles=0&for /l %%g in (0,1,15)do (if !board[%%g]! neq !tmpboard[%%g]! set boardchanged=1
if !board[%%g]! neq 0 set /a tiles+=1
if !board[%%g]!==2048 if %winstate%==0 set winstate=1)
if %boardchanged%==1 call :tilespawn&set/amoves+=1
if %winstate%==1 goto youwin
set/ayoulost=0,v=0
:1
set/av+=1
set/aw=%v%%%4,x=%v%/4,y=%v%-1,z=%v%-4
if %tiles%==15 (if %w% neq 0 if !board[%v%]!==!board[%y%]! set youlost=1
if %x% neq 0 if !board[%v%]!==!board[%z%]! set youlost=1)
if %tiles%==15 if %v% neq 15 goto 1
if %tiles%==15 if %boardchanged%==1 if %youlost%==0 goto youlost
goto startloop
:tilespawn
set/arandtile=%random%%%16
if !board[%randtile%]! gtr 0 goto tilespawn
set/aboard[%randtile%]=%random%%%10/9*2+2&exit /b
:compress
set x=-1
:2
set/aw=0,x+=1,y=-1&for /l %%g in (0,1,3) do set tmparray[%%g]=0
:3
set/ay+=1&set/az=%x%*4+!y!
if !board[%z%]! neq 0 set/atmparray[%w%]=!board[%z%]!&set/aw+=1
if %y% neq 3 goto 3
for /l %%g in (0,1,3)do set/az=%x%*4+%%g&set/aboard[!z!]=!tmparray[%%g]!
if %x% neq 3 goto 2
exit /b
:merge
set x=-1
:4
set/ax+=1,y=-1
:5
set/ay+=1&set/aw=%x%*4+!y!&set/az=!w!+1
if !board[%w%]!==!board[%z%]! set/aboard[%w%]*=2,score+=!board[%w%]!*2&set board[%z%]=0
if %y% neq 2 goto 5
if %x% neq 3 goto 4
exit /b
:rotateclockwise
set i=12
for /l %%g in (0,1,15)do set/atmparray[%%g]=board[!i!],i-=4&if !i! lss 0 set/ai+=17
for /l %%g in (0,1,15)do set/aboard[%%g]=!tmparray[%%g]!
exit /b
:youwin
set winstate=2&call :drawboard 1&call :savegame&choice /c cnx /n /m ""
if %errorlevel%==1 goto startloop
if %errorlevel%==2 goto startgame
if %errorlevel%==3 exit /b
:youlost
call :drawboard 2&for /l %%g in (0,1,15)do set board[%%g]=0
set/ascore=0,winstate=0&call :savegame&choice /c nx /n /m ""
if %errorlevel%==1 goto startgame
if %errorlevel%==2 exit /b
:drawboard
for /l %%g in (0,1,15)do (set "board2[%%g]=      !board[%%g]!"&if !board[%%g]!==0 set "board2[%%g]=      "
if %moves% gtr 0 if %%g==%randtile% set board2[%%g]=   + !board[%%g]!
set board2[%%g]=!board2[%%g]:~-6!)
cls&echo  ___     ___    _  _      ___&echo ^|__ \   / _ \  ^| ^|^| ^|    / _ \&echo    ) ^| ^| ^| ^| ^| ^| ^|^| ^|_  ^| (_) ^|&echo   / /  ^| ^| ^| ^| ^|__   _^|  ^> _ ^<&echo  / /_  ^| ^|_^| ^|    ^| ^|   ^| (_) ^|&echo ^|____^|  \___/     ^|_^|    \___/&echo(&echo Join the numbers and get to the&echo            2048 tile^^!&echo(&if %1==0 echo     (Press N for new game)&echo(
if %1==1 echo             You Win!&echo Press C to continue, N to reset
if %1==2 echo            Game Over&echo     Press N to try again...
echo  Score: %score%&echo  Best: %bestscore%&echo  +------+------+------+------+&echo  ^|%board2[0]%^|%board2[1]%^|%board2[2]%^|%board2[3]%^|&echo  +------+------+------+------+&echo  ^|%board2[4]%^|%board2[5]%^|%board2[6]%^|%board2[7]%^|&echo  +------+------+------+------+&echo  ^|%board2[8]%^|%board2[9]%^|%board2[10]%^|%board2[11]%^|&echo  +------+------+------+------+&echo  ^|%board2[12]%^|%board2[13]%^|%board2[14]%^|%board2[15]%^|&echo  +------+------+------+------+&echo(&echo HOW TO PLAY: Use the WASD keys&echo to move the tiles. When two&echo tiles with the same number&echo touch, they merge into one^^!&echo -------------------------------&echo NOTE: This is only a port of&echo the original 2048, found at&echo http://git.io/2048 . Official&echo apps for iOS and Android are&echo also available. Other versions&echo are derivatives or fakes, and&echo should be used with caution.&echo -------------------------------&echo Port coded by Josiah Winslow.&echo Tweaked and tested by the&echo DosTips.com community. 2048&echo created by Gabriele Cirulli.&echo Based on 1024 by Veewo Studio&echo and conceptually similar to&echo Threes by Asher Vollmer.&exit /b
:savegame
echo(>2048JWinslow.dat&for %%g in (score bestscore winstate)do echo set %%g=!%%g!>>2048JWinslow.dat
for /l %%g in (0,1,15)do set tmpboard[%%g]=!board[%%g]!&echo set board[%%g]=!board[%%g]!>>2048JWinslow.dat
exit /b
:end
Last edited by JWinslow23 on 05 Jul 2014 13:55, edited 1 time in total.

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

Re: 2048 for Batch

#2 Post by foxidrive » 03 Jul 2014 21:24

A bit of tweaking may be needed...

Image

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: 2048 for Batch

#3 Post by dbenham » 04 Jul 2014 07:33

You should reference the old thread when starting a new thread that is a derivative of the old. For example, this thread is a derivative of viewtopic.php?f=3&t=5701 where there was a lot of collaboration in development of this game.

It would also be a nice gesture to include a reference to the thread in your game credits. Acknowledging everyone that contributed by name might be a bit much, but including a link where the advice was given helps promote this site, and also indirectly gives credit to those that helped.


Dave Benham

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

Re: 2048 for Batch

#4 Post by JWinslow23 » 04 Jul 2014 11:54

foxidrive, what OS are you using? I don't know why that problem should be occurring.

dbenham, how about putting "Tested and tweaked by the DOSTips.com community" at the bottom?

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

Re: 2048 for Batch

#5 Post by ShadowThief » 04 Jul 2014 15:16

foxidrive wrote:A bit of tweaking may be needed...

http://i57.tinypic.com/ashuu.png


I am unable to reproduce your issue.
Image

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

Re: 2048 for Batch

#6 Post by foxidrive » 04 Jul 2014 22:35

JWinslow23 wrote:foxidrive, what OS are you using? I don't know why that problem should be occurring.


Windows 8.1 32 bit

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: 2048 for Batch

#7 Post by carlos » 05 Jul 2014 06:01

I have the same view of foxidrive on windows 8.
I fix it replacing:

Code: Select all

for /l %%g in (0,1,15)do (set board2[%%g]=      !board[%%g]!&if !board[%%g]!==0 set board2[%%g]=     

by

Code: Select all

for /l %%g in (0,1,15)do (set "board2[%%g]=      !board[%%g]!"&if !board[%%g]!==0 set "board2[%%g]=      "

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

Re: 2048 for Batch

#8 Post by foxidrive » 05 Jul 2014 06:23

carlos wrote:I have the same view of foxidrive on windows 8.
I fix it replacing:

Code: Select all

for /l %%g in (0,1,15)do (set board2[%%g]=      !board[%%g]!&if !board[%%g]!==0 set board2[%%g]=     

by

Code: Select all

for /l %%g in (0,1,15)do (set "board2[%%g]=      !board[%%g]!"&if !board[%%g]!==0 set "board2[%%g]=      "


Confirmed.


I play it by hitting random keys - got to 220 just now. :D

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

Re: 2048 for Batch

#9 Post by JWinslow23 » 05 Jul 2014 10:34

Quotes needed. Noted. :P

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: 2048 for Batch

#10 Post by carlos » 05 Jul 2014 11:48

But I added a one space more, because it need 6 and it have 5.

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

Re: 2048 for Batch

#11 Post by JWinslow23 » 05 Jul 2014 13:57

carlos: It had 6. I counted.

Also, bump.

I updated the game to include the few suggestions you made. No new pic, though.

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

Re: 2048 for Batch

#12 Post by foxidrive » 05 Jul 2014 21:36

JWinslow23 wrote:carlos: It had 6. I counted.


You counted wrong. I double checked your code - there were 5 spaces as carlos said.

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

Re: 2048 for Batch

#13 Post by JWinslow23 » 06 Jul 2014 10:00

For me, it had 6. Just sayin'.

But anyways, it's out and functional, so I don't care much anyways.

Post Reply