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: Highscore/board saving?

#61 Post by Squashman » 21 Jun 2014 12:45

JWinslow23 wrote:How would I save a list of values as a highscore? And how would I recall them and store them into variables? This is pretty much the last thing I need.

write them to a text file and read it in when you need to.

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

Re: Highscore/board saving?

#62 Post by JWinslow23 » 21 Jun 2014 14:01

Squashman wrote:
JWinslow23 wrote:How would I save a list of values as a highscore? And how would I recall them and store them into variables? This is pretty much the last thing I need.

write them to a text file and read it in when you need to.

OK, write with echo and read with...

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

Re: Game code help?

#63 Post by Squashman » 21 Jun 2014 18:32

Dpends on how you want to use it. Couple of options.
If you want to display the contents of a file on the screen:
type myfile.txt

If you want to read the first line of a file:
set /p line1=<myfile.txt

If you want to read multiple lines using set /p
< myfile.txt (
set /p line1=
set /p line2=
set /p line3=
)

Read one line at a time with a FOR /F
for /f "delims=" %%G in (myfile.txt) do echo %%G

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

Re: Game code help?

#64 Post by JWinslow23 » 21 Jun 2014 20:12

Squashman wrote:Dpends on how you want to use it. Couple of options.
If you want to display the contents of a file on the screen:
type myfile.txt

If you want to read the first line of a file:
set /p line1=<myfile.txt

If you want to read multiple lines using set /p
< myfile.txt (
set /p line1=
set /p line2=
set /p line3=
)

Read one line at a time with a FOR /F
for /f "delims=" %%G in (myfile.txt) do echo %%G

OK. I want to store numbers on the lines to variables. How would I do that?

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Game code help?

#65 Post by Yury » 22 Jun 2014 04:23

JWinslow23 wrote:I want to store numbers on the lines to variables. How would I do that?



Code: Select all

@echo off

rem The storage of lines as values of variables which names contain numbers of these lines:
for /f %%i in ('"<"myfile.txt" find /c /v """') do set n=%%i
<"myfile.txt" (
for /l %%i in (1 1 %n%) do (
 set /p line%%i=& if not defined line%%i set line%%i=^
%
%
 )
)
set line

rem The example of using:
>"new.txt" (
for %%i in (1 5 15) do for /f "delims=" %%j in ('call echo "%%line%%i%%"') do echo.%%~j
)

pause>nul
exit /b

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

Re: Game code help?

#66 Post by dbenham » 22 Jun 2014 07:15

If you have a bunch of variables you want to save and restore, then the simplest thing to do is give all of your save variables a unique prefix. I'll use # for this example.

Code: Select all

:: Clear existing # variables during initialization
for /f "delims==" %%V in ('set #') do set "%%V="

:: Define variables as needed
set #score=....
set #moves=....
set #etc...
....

:: Save # variables to a text file
set # >save.txt

:: Load the variables
for /f "delims=" %%V in (save.txt) do set "%%V"


Dave Benham

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

Re: Game code help?

#67 Post by JWinslow23 » 22 Jun 2014 10:14

Yury wrote:
JWinslow23 wrote:I want to store numbers on the lines to variables. How would I do that?



Code: Select all

@echo off

rem The storage of lines as values of variables which names contain numbers of these lines:
for /f %%i in ('"<"myfile.txt" find /c /v """') do set n=%%i
<"myfile.txt" (
for /l %%i in (1 1 %n%) do (
 set /p line%%i=& if not defined line%%i set line%%i=^
%
%
 )
)
set line

rem The example of using:
>"new.txt" (
for %%i in (1 5 15) do for /f "delims=" %%j in ('call echo "%%line%%i%%"') do echo.%%~j
)

pause>nul
exit /b

And how to not echo the variables?

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Game code help?

#68 Post by Yury » 22 Jun 2014 10:44

JWinslow23 wrote:And how to not echo the variables?


Code: Select all

:set line

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Game code help?

#69 Post by penpen » 22 Jun 2014 13:48

Have you seen my above post for optimizing the (AWSD) moves?
http://www.dostips.com/forum/viewtopic.php?p=35337#p35337

It is "moving and merging" in one step for all directions (without the tile spawn step), so there is no need to use multiple functions.

Does anybody have a way to check if you lost? You lose if you can't merge any tiles together.
As far, as i know this game, this is not correct; loss condition: No tile could be added after the move.
This position

Code: Select all

4 0 0 0
0 0 0 0
0 0 0 0
0 0 0 2
is no lost game, although you cannot merge tiles.
And this (valid) game position disallows to add a tile prior to the merge (move) step:

Code: Select all

4 4 2 4
4 2 4 2
2 4 2 4
4 2 4 2


So you could hold indices of 'zero' entries (after the move/merge step):
- if there is no entry: loss,
- else randomly choose one of them, so you don't have to loop within :tilespawn function.

At last, if you've won, you have the option, to continue playing (so you might get 4096 - i didn't got more, but it might be also possible).

penpen

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

Re: Game code help?

#70 Post by JWinslow23 » 23 Jun 2014 09:43

I saw the optimization, but all it looked like was simply moving up, then left, then down, then right, indefinitely. Sorry, but I'd like to control where I move, thanks.

Also, I have a losscheck already. (What I meant was, if a move results in 15 tiles, then you can't merge anything after the new tile spawns, you lost.) It's in the code:

Code: Select all

@echo off
title 2048
mode con: cols=32 lines=43
setlocal enabledelayedexpansion
color F0
:startgame
set score=0
set winstate=0
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
:startloop
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!&if !board[%%g]!==0 set board2[%%g]=     
set h=-1
:0
set /a h+=1
set board2[%h%]=!board2[%h%]:~-6!
if %h% neq 15 goto 0
cls
echo  ___     ___    _  _      ___
echo ^|__ \   / _ \  ^| ^|^| ^|    / _ \
echo    ) ^| ^| ^| ^| ^| ^| ^|^| ^|_  ^| (_) ^|
echo   / /  ^| ^| ^| ^| ^|__   _^|  ^> _ ^<
echo  / /_  ^| ^|_^| ^|    ^| ^|   ^| (_) ^|
echo ^|____^|  \___/     ^|_^|    \___/
echo(
echo Join the numbers and get to the
echo            2048 tile^^!
echo(
echo     (Press N for new game)
echo(
echo  Score: %score%
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 by Josiah Winslow. 2048
echo created by Gabriele Cirulli.
echo Based on 1024 by Veewo Studio
echo and conceptually similar to
echo Threes by Asher Vollmer.
for /l %%g in (0,1,15) do set tmpboard[%%g]=!board[%%g]!
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 goto startgame
set boardchanged=0
set 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
if %winstate%==1 goto youwin
set youlost=0
set v=0
:1
set /a v+=1
set /a w=%v%%%4
set /a x=%v%/4
set /a y=%v%-1
set /a z=%v%-4
if %tiles%==15 if %w% neq 0 if !board[%v%]!==!board[%y%]! set youlost=1
if %tiles%==15 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 %youlost%==0 goto youlost
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
 
:compress
set x=-1
:2
set /a x+=1
for /l %%g in (0,1,3) do set tmparray[%%g]=0
set w=0
set y=-1
:3
set /a y+=1
set /a z=%x%*4+%y%
if !board[%z%]! neq 0 set /a tmparray[%w%]=!board[%z%]! & set /a w+=1
if %y% neq 3 goto 3
for /l %%g in (0,1,3) do set /a z=%x%*4+%%g & set /a board[!z!]=!tmparray[%%g]!
if %x% neq 3 goto 2
exit /b
 
:merge
set x=-1
:4
set /a x+=1
set y=-1
:5
set /a y+=1
set /a w=%x%*4+%y%
set /a z=%w%+1
if !board[%w%]!==!board[%z%]! set /a board[%w%]*=2 & set board[%z%]=0 & set /a score+=!board[%w%]!
if %y% neq 2 goto 5
if %x% neq 3 goto 4
exit /b
 
:rotateclockwise
set i=0
for %%j in (12 8 4 0 13 9 5 1 14 10 6 2 15 11 7 3) do (
   set /A tmparray[!i!]=board[%%j], i+=1
)
for /l %%g in (0,1,15) do set /a board[%%g]=!tmparray[%%g]!
exit /b
 
:youwin
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!&if !board[%%g]!==0 set board2[%%g]=     
set h=-1
:00
set /a h+=1
set board2[%h%]=!board2[%h%]:~-6!
if %h% neq 15 goto 00
cls
echo  ___     ___    _  _      ___
echo ^|__ \   / _ \  ^| ^|^| ^|    / _ \
echo    ) ^| ^| ^| ^| ^| ^| ^|^| ^|_  ^| (_) ^|
echo   / /  ^| ^| ^| ^| ^|__   _^|  ^> _ ^<
echo  / /_  ^| ^|_^| ^|    ^| ^|   ^| (_) ^|
echo ^|____^|  \___/     ^|_^|    \___/
echo(
echo Join the numbers and get to the
echo            2048 tile^^!
echo(
echo             You Win!
echo Press C to continue, N to reset
echo  Score: %score%
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 by Josiah Winslow. 2048
echo created by Gabriele Cirulli.
echo Based on 1024 by Veewo Studio
echo and conceptually similar to
echo Threes by Asher Vollmer.
set winstate=2
choice /c cnx /n /m ""
if %errorlevel%==1 goto startloop
if %errorlevel%==2 goto startgame
if %errorlevel%==3 exit /b
 
:youlost
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=     
set h=-1
:000
set /a h+=1
set board2[%h%]=!board2[%h%]:~-6!
if %h% neq 15 goto 000
cls
echo  ___     ___    _  _      ___
echo ^|__ \   / _ \  ^| ^|^| ^|    / _ \
echo    ) ^| ^| ^| ^| ^| ^| ^|^| ^|_  ^| (_) ^|
echo   / /  ^| ^| ^| ^| ^|__   _^|  ^> _ ^<
echo  / /_  ^| ^|_^| ^|    ^| ^|   ^| (_) ^|
echo ^|____^|  \___/     ^|_^|    \___/
echo(
echo Join the numbers and get to the
echo            2048 tile^^!
echo(
echo            Game Over
echo     Press N to try again...
echo  Score: %score%
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 by Josiah Winslow. 2048
echo created by Gabriele Cirulli.
echo Based on 1024 by Veewo Studio
echo and conceptually similar to
echo Threes by Asher Vollmer.
choice /c nx /n /m ""
if %errorlevel%==1 goto startgame
if %errorlevel%==2 exit /b

Optimizations of any kind would be appreciated.

I also made a version in color (a bit less graphical, to make it at a playable speed) (color routine from somewhere on stackoverflow.com):

Code: Select all

@echo off
setlocal enabledelayedexpansion
title 2048 Colorized
mode con: cols=34 lines=13
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
<nul >"%temp%\x" set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%.%DEL%"
:startgame
set score=0
set winstate=0
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
:startloop
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=     
set h=-1
:0
set /a h+=1
set board2[%h%]=!board2[%h%]:~-6!
if %h% neq 15 goto 0
set h=-1
:00
set /a h+=1
if !board[%h%]!==0 set board3[%h%]=07
if !board[%h%]!==2 set board3[%h%]=f0
if !board[%h%]!==4 set board3[%h%]=70
if !board[%h%]!==8 set board3[%h%]=c7
if !board[%h%]!==16 set board3[%h%]=cf
if !board[%h%]!==32 set board3[%h%]=47
if !board[%h%]!==64 set board3[%h%]=4f
if !board[%h%]!==128 set board3[%h%]=67
if !board[%h%]!==256 set board3[%h%]=6f
if !board[%h%]!==512 set board3[%h%]=e8
if !board[%h%]!==1024 set board3[%h%]=e8
if !board[%h%]!==2048 set board3[%h%]=e0
if %h% neq 15 goto 00
cls
echo Score: %score%
echo(
call :colorPrintVar %board3[0]% board2[0]
call :colorPrint 07 "|"
call :colorPrintVar %board3[1]% board2[1]
call :colorPrint 07 "|"
call :colorPrintVar %board3[2]% board2[2]
call :colorPrint 07 "|"
call :colorPrintVar %board3[3]% board2[3] /n
echo ------+------+------+------
call :colorPrintVar %board3[4]% board2[4]
call :colorPrint 07 "|"
call :colorPrintVar %board3[5]% board2[5]
call :colorPrint 07 "|"
call :colorPrintVar %board3[6]% board2[6]
call :colorPrint 07 "|"
call :colorPrintVar %board3[7]% board2[7] /n
echo ------+------+------+------
call :colorPrintVar %board3[8]% board2[8]
call :colorPrint 07 "|"
call :colorPrintVar %board3[9]% board2[9]
call :colorPrint 07 "|"
call :colorPrintVar %board3[10]% board2[10]
call :colorPrint 07 "|"
call :colorPrintVar %board3[11]% board2[11] /n
echo ------+------+------+------
call :colorPrintVar %board3[12]% board2[12]
call :colorPrint 07 "|"
call :colorPrintVar %board3[13]% board2[13]
call :colorPrint 07 "|"
call :colorPrintVar %board3[14]% board2[14]
call :colorPrint 07 "|"
call :colorPrintVar %board3[15]% board2[15] /n
for /l %%g in (0,1,15) do set tmpboard[%%g]=!board[%%g]!
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 goto startgame
set boardchanged=0
set 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
if %winstate%==1 goto youwin
set youlost=0
set v=0
:1
set /a v+=1
set /a w=%v%%%4
set /a x=%v%/4
set /a y=%v%-1
set /a z=%v%-4
if %tiles%==15 if %w% neq 0 if !board[%v%]!==!board[%y%]! set youlost=1
if %tiles%==15 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 %youlost%==0 goto youlost
goto startloop
:youlost
echo GAME OVER!
choice /c nx /n /m "Press N to play again..."
if %errorlevel%==1 goto startgame
if %errorlevel%==2 goto end
:youwin
echo YOU WIN!
echo Press C to continue
choice /c cnx /n /m "Press N to play again"
if %errorlevel%==1 goto startloop
if %errorlevel%==2 goto startgame
if %errorlevel%==3 goto end
 
:tilespawn
set /a randtile=%random%%%16
if !board[%randtile%]! gtr 0 goto tilespawn
set /a board[%randtile%]=%random%%%10/9*2+2
exit /b
 
:compress
set x=-1
:2
set /a x+=1
for /l %%g in (0,1,3) do set tmparray[%%g]=0
set w=0
set y=-1
:3
set /a y+=1
set /a z=%x%*4+%y%
if !board[%z%]! neq 0 set /a tmparray[%w%]=!board[%z%]! & set /a w+=1
if %y% neq 3 goto 3
for /l %%g in (0,1,3) do set /a z=%x%*4+%%g & set /a board[!z!]=!tmparray[%%g]!
if %x% neq 3 goto 2
exit /b
 
:merge
set x=-1
:4
set /a x+=1
set y=-1
:5
set /a y+=1
set /a w=%x%*4+%y%
set /a z=%w%+1
if !board[%w%]!==!board[%z%]! set /a board[%w%]*=2 & set board[%z%]=0 & set /a score+=!board[%w%]!
if %y% neq 2 goto 5
if %x% neq 3 goto 4
exit /b
 
:rotateclockwise
set i=0
for %%j in (12 8 4 0 13 9 5 1 14 10 6 2 15 11 7 3) do (
   set /A tmparray[!i!]=board[%%j], i+=1
)
for /l %%g in (0,1,15) do set /a board[%%g]=!tmparray[%%g]!
exit /b
 
:colorPrint Color  Str  [/n]
setlocal
set "str=%~2"
call :colorPrintVar %1 str %3
exit /b
 
:colorPrintVar  Color  StrVar  [/n]
if not defined %~2 exit /b
set "str=a%DEL%!%~2:\=a%DEL%\..\%DEL%%DEL%%DEL%!"
set "str=!str:/=a%DEL%/..\%DEL%%DEL%%DEL%!"
set "str=!str:"=\"!"
pushd "%temp%"
findstr /p /A:%1 "." "!str!\..\x" nul
if /i "%~3"=="/n" echo(
exit /b
 
:end
del "%temp%\x"

Tell me what you think!

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Game code help?

#71 Post by einstein1969 » 23 Jun 2014 12:16

Nice!

A little adjust...

Code: Select all

@echo off
setlocal enabledelayedexpansion
title 2048 Colorized
mode con: cols=31 lines=13
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
:startgame
set score=0
set winstate=0
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
:startloop
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=     
set h=-1
:0
set /a h+=1
set board2[%h%]=!board2[%h%]:~-6!
if %h% neq 15 goto 0
set h=-1
:00
set /a h+=1
if !board[%h%]!==0 set board3[%h%]=07
if !board[%h%]!==2 set board3[%h%]=f0
if !board[%h%]!==4 set board3[%h%]=70
if !board[%h%]!==8 set board3[%h%]=c7
if !board[%h%]!==16 set board3[%h%]=cf
if !board[%h%]!==32 set board3[%h%]=47
if !board[%h%]!==64 set board3[%h%]=4f
if !board[%h%]!==128 set board3[%h%]=67
if !board[%h%]!==256 set board3[%h%]=6f
if !board[%h%]!==512 set board3[%h%]=e8
if !board[%h%]!==1024 set board3[%h%]=e8
if !board[%h%]!==2048 set board3[%h%]=e0
if %h% neq 15 goto 00
cls
echo Score: %score%
echo( ÚÄÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÄ¿
set /p ".=.%DEL% |"<nul
call :colorPrintVar %board3[0]% board2[0]
set /p ".= |"<nul
call :colorPrintVar %board3[1]% board2[1]
set /p ".= |"<nul
call :colorPrintVar %board3[2]% board2[2]
set /p ".= |"<nul
call :colorPrintVar %board3[3]% board2[3]
echo(^|
echo  ÃÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄ´
set /p ".=.%DEL% |"<nul
call :colorPrintVar %board3[4]% board2[4]
set /p ".=|"<nul
call :colorPrintVar %board3[5]% board2[5]
set /p ".=|"<nul
call :colorPrintVar %board3[6]% board2[6]
set /p ".=|"<nul
call :colorPrintVar %board3[7]% board2[7]
echo(^|
echo  ÃÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄ´
set /p ".=.%DEL% |"<nul
call :colorPrintVar %board3[8]% board2[8]
set /p ".=|"<nul
call :colorPrintVar %board3[9]% board2[9]
set /p ".=|"<nul
call :colorPrintVar %board3[10]% board2[10]
set /p ".=|"<nul
call :colorPrintVar %board3[11]% board2[11]
echo(^|
echo  ÃÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄ´
set /p ".=.%DEL% |"<nul
call :colorPrintVar %board3[12]% board2[12]
set /p ".=|"<nul
call :colorPrintVar %board3[13]% board2[13]
set /p ".=|"<nul
call :colorPrintVar %board3[14]% board2[14]
set /p ".=|"<nul
call :colorPrintVar %board3[15]% board2[15]
echo(^|
echo( ÀÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÙ
set/p ".=ASWD=MOVE X=EXIT" <nul
for /l %%g in (0,1,15) do set tmpboard[%%g]=!board[%%g]!
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 goto startgame
set boardchanged=0
set 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
if %winstate%==1 goto youwin
set youlost=0
set v=0
:1
set /a v+=1
set /a w=%v%%%4
set /a x=%v%/4
set /a y=%v%-1
set /a z=%v%-4
if %tiles%==15 if %w% neq 0 if !board[%v%]!==!board[%y%]! set youlost=1
if %tiles%==15 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 %youlost%==0 goto youlost
goto startloop
:youlost
echo GAME OVER!
choice /c nx /n /m "Press N to play again..."
if %errorlevel%==1 goto startgame
if %errorlevel%==2 goto end
:youwin
echo YOU WIN!
echo Press C to continue
choice /c cnx /n /m "Press N to play again"
if %errorlevel%==1 goto startloop
if %errorlevel%==2 goto startgame
if %errorlevel%==3 goto end
 
:tilespawn
set /a randtile=%random%%%16
if !board[%randtile%]! gtr 0 goto tilespawn
set /a board[%randtile%]=%random%%%10/9*2+2
exit /b
 
:compress
set x=-1
:2
set /a x+=1
for /l %%g in (0,1,3) do set tmparray[%%g]=0
set w=0
set y=-1
:3
set /a y+=1
set /a z=%x%*4+%y%
if !board[%z%]! neq 0 set /a tmparray[%w%]=!board[%z%]! & set /a w+=1
if %y% neq 3 goto 3
for /l %%g in (0,1,3) do set /a z=%x%*4+%%g & set /a board[!z!]=!tmparray[%%g]!
if %x% neq 3 goto 2
exit /b
 
:merge
set x=-1
:4
set /a x+=1
set y=-1
:5
set /a y+=1
set /a w=%x%*4+%y%
set /a z=%w%+1
if !board[%w%]!==!board[%z%]! set /a board[%w%]*=2 & set board[%z%]=0 & set /a score+=!board[%w%]!
if %y% neq 2 goto 5
if %x% neq 3 goto 4
exit /b
 
:rotateclockwise
set i=0
for %%j in (12 8 4 0 13 9 5 1 14 10 6 2 15 11 7 3) do (
   set /A tmparray[!i!]=board[%%j], i+=1
)
for /l %%g in (0,1,15) do set /a board[%%g]=!tmparray[%%g]!
exit /b
 
:colorPrintVar  Color  StrVar  [/n]
if not defined %~2 exit /b
set str=!%2!
set str=!str: =ÿ!
pushd "%temp%"
set /p ".=%DEL%" >"!str!" <nul
findstr /A:%1 "." "%str%" nul
if /i "%~3"=="/n" echo(
popd
exit /b
 
:end


Image

How to win?

einstein1969

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

Re: Game code help?

#72 Post by JWinslow23 » 23 Jun 2014 12:47

Oops, my mistake:

Code: Select all

@echo off
setlocal enabledelayedexpansion
title 2048 Colorized
mode con: cols=34 lines=13
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
<nul >"%temp%\x" set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%.%DEL%"
:startgame
set score=0
set winstate=0
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
:startloop
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=     
set h=-1
:0
set /a h+=1
set board2[%h%]=!board2[%h%]:~-6!
if %h% neq 15 goto 0
set h=-1
:00
set /a h+=1
if !board[%h%]!==0 set board3[%h%]=07
if !board[%h%]!==2 set board3[%h%]=f0
if !board[%h%]!==4 set board3[%h%]=70
if !board[%h%]!==8 set board3[%h%]=c7
if !board[%h%]!==16 set board3[%h%]=cf
if !board[%h%]!==32 set board3[%h%]=47
if !board[%h%]!==64 set board3[%h%]=4f
if !board[%h%]!==128 set board3[%h%]=67
if !board[%h%]!==256 set board3[%h%]=6f
if !board[%h%]!==512 set board3[%h%]=e8
if !board[%h%]!==1024 set board3[%h%]=e8
if !board[%h%]!==2048 set board3[%h%]=e0
if %h% neq 15 goto 00
cls
echo Score: %score%
echo(
call :colorPrintVar %board3[0]% board2[0]
call :colorPrint 07 "|"
call :colorPrintVar %board3[1]% board2[1]
call :colorPrint 07 "|"
call :colorPrintVar %board3[2]% board2[2]
call :colorPrint 07 "|"
call :colorPrintVar %board3[3]% board2[3] /n
echo ------+------+------+------
call :colorPrintVar %board3[4]% board2[4]
call :colorPrint 07 "|"
call :colorPrintVar %board3[5]% board2[5]
call :colorPrint 07 "|"
call :colorPrintVar %board3[6]% board2[6]
call :colorPrint 07 "|"
call :colorPrintVar %board3[7]% board2[7] /n
echo ------+------+------+------
call :colorPrintVar %board3[8]% board2[8]
call :colorPrint 07 "|"
call :colorPrintVar %board3[9]% board2[9]
call :colorPrint 07 "|"
call :colorPrintVar %board3[10]% board2[10]
call :colorPrint 07 "|"
call :colorPrintVar %board3[11]% board2[11] /n
echo ------+------+------+------
call :colorPrintVar %board3[12]% board2[12]
call :colorPrint 07 "|"
call :colorPrintVar %board3[13]% board2[13]
call :colorPrint 07 "|"
call :colorPrintVar %board3[14]% board2[14]
call :colorPrint 07 "|"
call :colorPrintVar %board3[15]% board2[15] /n
for /l %%g in (0,1,15) do set tmpboard[%%g]=!board[%%g]!
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 goto startgame
set boardchanged=0
set 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
if %winstate%==1 goto youwin
set youlost=0
set v=0
:1
set /a v+=1
set /a w=%v%%%4
set /a x=%v%/4
set /a y=%v%-1
set /a z=%v%-4
if %tiles%==15 if %w% neq 0 if !board[%v%]!==!board[%y%]! set youlost=1
if %tiles%==15 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 %youlost%==0 goto youlost
goto startloop
:youlost
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=     
set h=-1
:0
set /a h+=1
set board2[%h%]=!board2[%h%]:~-6!
if %h% neq 15 goto 0
set h=-1
:00
set /a h+=1
if !board[%h%]!==0 set board3[%h%]=07
if !board[%h%]!==2 set board3[%h%]=f0
if !board[%h%]!==4 set board3[%h%]=70
if !board[%h%]!==8 set board3[%h%]=c7
if !board[%h%]!==16 set board3[%h%]=cf
if !board[%h%]!==32 set board3[%h%]=47
if !board[%h%]!==64 set board3[%h%]=4f
if !board[%h%]!==128 set board3[%h%]=67
if !board[%h%]!==256 set board3[%h%]=6f
if !board[%h%]!==512 set board3[%h%]=e8
if !board[%h%]!==1024 set board3[%h%]=e8
if !board[%h%]!==2048 set board3[%h%]=e0
if %h% neq 15 goto 00
cls
echo Score: %score%
echo(
call :colorPrintVar %board3[0]% board2[0]
call :colorPrint 07 "|"
call :colorPrintVar %board3[1]% board2[1]
call :colorPrint 07 "|"
call :colorPrintVar %board3[2]% board2[2]
call :colorPrint 07 "|"
call :colorPrintVar %board3[3]% board2[3] /n
echo ------+------+------+------
call :colorPrintVar %board3[4]% board2[4]
call :colorPrint 07 "|"
call :colorPrintVar %board3[5]% board2[5]
call :colorPrint 07 "|"
call :colorPrintVar %board3[6]% board2[6]
call :colorPrint 07 "|"
call :colorPrintVar %board3[7]% board2[7] /n
echo ------+------+------+------
call :colorPrintVar %board3[8]% board2[8]
call :colorPrint 07 "|"
call :colorPrintVar %board3[9]% board2[9]
call :colorPrint 07 "|"
call :colorPrintVar %board3[10]% board2[10]
call :colorPrint 07 "|"
call :colorPrintVar %board3[11]% board2[11] /n
echo ------+------+------+------
call :colorPrintVar %board3[12]% board2[12]
call :colorPrint 07 "|"
call :colorPrintVar %board3[13]% board2[13]
call :colorPrint 07 "|"
call :colorPrintVar %board3[14]% board2[14]
call :colorPrint 07 "|"
call :colorPrintVar %board3[15]% board2[15] /n
echo GAME OVER!
choice /c nx /n /m "Press N to play again..."
if %errorlevel%==1 goto startgame
if %errorlevel%==2 goto end
:youwin
set winstate=2
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=     
set h=-1
:0
set /a h+=1
set board2[%h%]=!board2[%h%]:~-6!
if %h% neq 15 goto 0
set h=-1
:00
set /a h+=1
if !board[%h%]!==0 set board3[%h%]=07
if !board[%h%]!==2 set board3[%h%]=f0
if !board[%h%]!==4 set board3[%h%]=70
if !board[%h%]!==8 set board3[%h%]=c7
if !board[%h%]!==16 set board3[%h%]=cf
if !board[%h%]!==32 set board3[%h%]=47
if !board[%h%]!==64 set board3[%h%]=4f
if !board[%h%]!==128 set board3[%h%]=67
if !board[%h%]!==256 set board3[%h%]=6f
if !board[%h%]!==512 set board3[%h%]=e8
if !board[%h%]!==1024 set board3[%h%]=e8
if !board[%h%]!==2048 set board3[%h%]=e0
if %h% neq 15 goto 00
cls
echo Score: %score%
echo(
call :colorPrintVar %board3[0]% board2[0]
call :colorPrint 07 "|"
call :colorPrintVar %board3[1]% board2[1]
call :colorPrint 07 "|"
call :colorPrintVar %board3[2]% board2[2]
call :colorPrint 07 "|"
call :colorPrintVar %board3[3]% board2[3] /n
echo ------+------+------+------
call :colorPrintVar %board3[4]% board2[4]
call :colorPrint 07 "|"
call :colorPrintVar %board3[5]% board2[5]
call :colorPrint 07 "|"
call :colorPrintVar %board3[6]% board2[6]
call :colorPrint 07 "|"
call :colorPrintVar %board3[7]% board2[7] /n
echo ------+------+------+------
call :colorPrintVar %board3[8]% board2[8]
call :colorPrint 07 "|"
call :colorPrintVar %board3[9]% board2[9]
call :colorPrint 07 "|"
call :colorPrintVar %board3[10]% board2[10]
call :colorPrint 07 "|"
call :colorPrintVar %board3[11]% board2[11] /n
echo ------+------+------+------
call :colorPrintVar %board3[12]% board2[12]
call :colorPrint 07 "|"
call :colorPrintVar %board3[13]% board2[13]
call :colorPrint 07 "|"
call :colorPrintVar %board3[14]% board2[14]
call :colorPrint 07 "|"
call :colorPrintVar %board3[15]% board2[15] /n
echo YOU WIN!
echo Press C to continue
choice /c cnx /n /m "Press N to play again"
if %errorlevel%==1 goto startloop
if %errorlevel%==2 goto startgame
if %errorlevel%==3 goto end

:tilespawn
set /a randtile=%random%%%16
if !board[%randtile%]! gtr 0 goto tilespawn
set /a board[%randtile%]=%random%%%10/9*2+2
exit /b

:compress
set x=-1
:2
set /a x+=1
for /l %%g in (0,1,3) do set tmparray[%%g]=0
set w=0
set y=-1
:3
set /a y+=1
set /a z=%x%*4+%y%
if !board[%z%]! neq 0 set /a tmparray[%w%]=!board[%z%]! & set /a w+=1
if %y% neq 3 goto 3
for /l %%g in (0,1,3) do set /a z=%x%*4+%%g & set /a board[!z!]=!tmparray[%%g]!
if %x% neq 3 goto 2
exit /b

:merge
set x=-1
:4
set /a x+=1
set y=-1
:5
set /a y+=1
set /a w=%x%*4+%y%
set /a z=%w%+1
if !board[%w%]!==!board[%z%]! set /a board[%w%]*=2 & set board[%z%]=0 & set /a score+=!board[%w%]!
if %y% neq 2 goto 5
if %x% neq 3 goto 4
exit /b

:rotateclockwise
set i=0
for %%j in (12 8 4 0 13 9 5 1 14 10 6 2 15 11 7 3) do (
   set /A tmparray[!i!]=board[%%j], i+=1
)
for /l %%g in (0,1,15) do set /a board[%%g]=!tmparray[%%g]!
exit /b

:colorPrint Color  Str  [/n]
setlocal
set "str=%~2"
call :colorPrintVar %1 str %3
exit /b

:colorPrintVar  Color  StrVar  [/n]
if not defined %~2 exit /b
set "str=a%DEL%!%~2:\=a%DEL%\..\%DEL%%DEL%%DEL%!"
set "str=!str:/=a%DEL%/..\%DEL%%DEL%%DEL%!"
set "str=!str:"=\"!"
pushd "%temp%"
findstr /p /A:%1 "." "!str!\..\x" nul
if /i "%~3"=="/n" echo(
exit /b

:end
del "%temp%\x"

Fix a little glitch.

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

Re: Game code help?

#73 Post by Squashman » 24 Jun 2014 06:11

something seems to be missing from this last set of code.

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Game code help?

#74 Post by einstein1969 » 24 Jun 2014 08:16

You use labels 3 times... :!: (example :00 )

I have correct and applied mods for better graphics and small code.

Code: Select all

@echo off
setlocal enabledelayedexpansion
title 2048 Colorized
mode con: cols=31 lines=13
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
:startgame
set score=0
set winstate=0
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
:startloop
call :Q
set/p ".=ASWD=MOVE X=EXIT" <nul
for /l %%g in (0,1,15) do set tmpboard[%%g]=!board[%%g]!
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 goto startgame
set boardchanged=0
set 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
if %winstate%==1 goto youwin
set youlost=0
set v=0
:1
set /a v+=1
set /a w=%v%%%4
set /a x=%v%/4
set /a y=%v%-1
set /a z=%v%-4
if %tiles%==15 if %w% neq 0 if !board[%v%]!==!board[%y%]! set youlost=1
if %tiles%==15 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 %youlost%==0 goto youlost
goto startloop
:youlost
call :Q
echo GAME OVER!
choice /c nx /n /m "Press N to play again..."
if %errorlevel%==1 goto startgame
if %errorlevel%==2 goto end
:youwin
set winstate=2
call :Q
echo YOU WIN!
echo Press C to continue
choice /c cnx /n /m "Press N to play again"
if %errorlevel%==1 goto startloop
if %errorlevel%==2 goto startgame
if %errorlevel%==3 goto end

:tilespawn
set /a randtile=%random%%%16
if !board[%randtile%]! gtr 0 goto tilespawn
set /a board[%randtile%]=%random%%%10/9*2+2
exit /b

:compress
set x=-1
:2
set /a x+=1
for /l %%g in (0,1,3) do set tmparray[%%g]=0
set w=0
set y=-1
:3
set /a y+=1
set /a z=%x%*4+%y%
if !board[%z%]! neq 0 set /a tmparray[%w%]=!board[%z%]! & set /a w+=1
if %y% neq 3 goto 3
for /l %%g in (0,1,3) do set /a z=%x%*4+%%g & set /a board[!z!]=!tmparray[%%g]!
if %x% neq 3 goto 2
exit /b

:merge
set x=-1
:4
set /a x+=1
set y=-1
:5
set /a y+=1
set /a w=%x%*4+%y%
set /a z=%w%+1
if !board[%w%]!==!board[%z%]! set /a board[%w%]*=2 & set board[%z%]=0 & set /a score+=!board[%w%]!
if %y% neq 2 goto 5
if %x% neq 3 goto 4
exit /b

:rotateclockwise
set i=0
for %%j in (12 8 4 0 13 9 5 1 14 10 6 2 15 11 7 3) do (
   set /A tmparray[!i!]=board[%%j], i+=1
)
for /l %%g in (0,1,15) do set /a board[%%g]=!tmparray[%%g]!
exit /b

:colorPrintVar  Color  StrVar  [/n]
if not defined %~2 exit /b
set str=!%2!
set str=!str: =ÿ!
pushd "%temp%"
set /p ".=%DEL%" >"!str!" <nul
findstr /A:%1 "." "%str%" nul
if /i "%~3"=="/n" echo(
popd
exit /b

:end
exit/b

:Q
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=     
set h=-1
:0
set /a h+=1
set board2[%h%]=!board2[%h%]:~-6!
if %h% neq 15 goto 0
set h=-1
:00
set /a h+=1
if !board[%h%]!==0 set board3[%h%]=07
if !board[%h%]!==2 set board3[%h%]=f0
if !board[%h%]!==4 set board3[%h%]=70
if !board[%h%]!==8 set board3[%h%]=c7
if !board[%h%]!==16 set board3[%h%]=cf
if !board[%h%]!==32 set board3[%h%]=47
if !board[%h%]!==64 set board3[%h%]=4f
if !board[%h%]!==128 set board3[%h%]=67
if !board[%h%]!==256 set board3[%h%]=6f
if !board[%h%]!==512 set board3[%h%]=e8
if !board[%h%]!==1024 set board3[%h%]=e8
if !board[%h%]!==2048 set board3[%h%]=e0
if %h% neq 15 goto 00
cls
echo Score: %score%
echo( ÚÄÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÄ¿
set /p ".=.%DEL% |"<nul
call :colorPrintVar %board3[0]% board2[0]
set /p ".= |"<nul
call :colorPrintVar %board3[1]% board2[1]
set /p ".= |"<nul
call :colorPrintVar %board3[2]% board2[2]
set /p ".= |"<nul
call :colorPrintVar %board3[3]% board2[3]
echo(^|
echo  ÃÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄ´
set /p ".=.%DEL% |"<nul
call :colorPrintVar %board3[4]% board2[4]
set /p ".=|"<nul
call :colorPrintVar %board3[5]% board2[5]
set /p ".=|"<nul
call :colorPrintVar %board3[6]% board2[6]
set /p ".=|"<nul
call :colorPrintVar %board3[7]% board2[7]
echo(^|
echo  ÃÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄ´
set /p ".=.%DEL% |"<nul
call :colorPrintVar %board3[8]% board2[8]
set /p ".=|"<nul
call :colorPrintVar %board3[9]% board2[9]
set /p ".=|"<nul
call :colorPrintVar %board3[10]% board2[10]
set /p ".=|"<nul
call :colorPrintVar %board3[11]% board2[11]
echo(^|
echo  ÃÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄ´
set /p ".=.%DEL% |"<nul
call :colorPrintVar %board3[12]% board2[12]
set /p ".=|"<nul
call :colorPrintVar %board3[13]% board2[13]
set /p ".=|"<nul
call :colorPrintVar %board3[14]% board2[14]
set /p ".=|"<nul
call :colorPrintVar %board3[15]% board2[15]
echo(^|
echo( ÀÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÙ
exit /b


Now work well but I have not win :( for chck the win!

einstein1969

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

Re: Game code help?

#75 Post by Aacini » 24 Jun 2014 09:02

I borrowed penpen's idea of using arrays of indices for the different directions and just one routine for move and merge tiles; the resulting code is much smaller! I also added Best Score management.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem 2048 game: http://gabrielecirulli.github.io/2048/
rem Windows Batch file version by Antonio Perez Ayala

rem Define the mapping indices tables
set /A i=0, j=0
for %%a in (      LEFT               UP               RIGHT             DOWN       ) do set /A i+=1 & set dir[!i!]=%%a
for %%m in ("1,1|1,2|1,3|1,4   1,1|2,1|3,1|4,1   1,4|1,3|1,2|1,1   4,1|3,1|2,1|1,1"
            "2,1|2,2|2,3|2,4   1,2|2,2|3,2|4,2   2,4|2,3|2,2|2,1   4,2|3,2|2,2|1,2"
            "3,1|3,2|3,3|3,4   1,3|2,3|3,3|4,3   3,4|3,3|3,2|3,1   4,3|3,3|2,3|1,3"
            "4,1|4,2|4,3|4,4   1,4|2,4|3,4|4,4   4,4|4,3|4,2|4,1   4,4|3,4|2,4|1,4") do (
   set /A j+=1
   for /F "tokens=1-4" %%a in (%%m) do (
      set "map[1,!j!]=%%a"
      set "map[2,!j!]=%%b"
      set "map[3,!j!]=%%c"
      set "map[4,!j!]=%%d"
   )
)

call :getBestScore
set lastBestScore=%bestScore%

rem Load a previously saved game, if any
if not exist 2048.dat goto newGame
for %%a in (2048.dat) do echo A previous game exists saved on %%~Ta
choice /M "Do you want to load it "
if %errorlevel% neq 1 goto newGame
for /F "delims=" %%a in (2048.dat) do set %%a
del 2048.dat
set "newVal="
color 1F
cls
goto showMove

:newGame
color 1F
cls
echo/
echo 2 0 4 8    2 0 4 8    2 0 4 8    2 0 4 8    2 0 4 8    2 0 4 8
echo/
echo - Move the tiles with: A=Left, W=Up, D=Right, S=Down
echo - When two tiles with same number touch, they merge into one
echo - Join the numbers and get to the 2048 TILE
echo/
echo Press N for new game or X for exit
echo/

for /L %%i in (1,1,4) do (
   for /L %%j in (1,1,4) do (
      set "board[%%i,%%j]=    "
   )
)
call :tileSpawn
set "board[%newI%,%newJ%]=   %newVal%"
call :tileSpawn
set "board[%newI%,%newJ%]=   %newVal%"
set /A move=-1, score=0, points=0, maxTile=0
goto showMove

:mainLoop
if not defined anyHole echo/& echo Not empty space left, end of game... & goto saveBestScore
call :tileSpawn
:showMove
echo/
set /A move+=1, prevScore=score, score+=points
if %score% gtr %bestScore% set bestScore=%score%
echo +----+----+----+----+    Move: %move%, Score: %prevScore%+%points% = %score%, Best: %bestScore%
for /L %%i in (1,1,4) do (
   set "row=|"
   for /L %%j in (1,1,4) do set "row=!row!!board[%%i,%%j]!|"
   echo !row!
   if %%i neq 4 echo +----+----+----+----+
)
set /P "=+----+----+----+----+    " < NUL
if defined newVal set "board[%newI%,%newJ%]=   %newVal%"
if %maxTile% equ 2048 echo/& echo Congratulations: YOU WON & goto saveBestScore
:getKey
choice /C AWDSNX /M "Press key: "
set option=%errorlevel%
echo/
if %option% gtr 4 goto option-%option%

echo Moving !dir[%option%]!

rem Assemble equivalent horizontal rows for move to left
for /L %%i in (1,1,4) do (
   for /F "tokens=1-4 delims=|" %%a in ("!map[%option%,%%i]!") do (
      set "row[%%i]=|!board[%%a]!|!board[%%b]!|!board[%%c]!|!board[%%d]!|"
   )
)

rem Move and merge tiles to left
call :moveLeft

rem Return moved tiles to their original positions in board
for /L %%i in (1,1,4) do (
   for /F "tokens=1-8 delims=|" %%a in ("!map[%option%,%%i]!!row[%%i]!") do (
      set "board[%%a]=%%e" & set "board[%%b]=%%f" & set "board[%%c]=%%g" & set "board[%%d]=%%h"
   )
)
goto mainLoop

:option-5 New game
echo/
choice /M "NEW GAME, are you sure "
if %errorlevel% equ 1 goto newGame
goto getKey

:option-6 Exit
echo/
choice /M "EXIT, are you sure "
if %errorlevel% neq 1 goto getKey
echo/
choice /M "-> Do you want to save this game "
if %errorlevel% neq 1 goto saveBestScore
(
   set /A move-=1, points=0
   for %%a in (move score points maxTile) do echo %%a=!%%a!
   set board[
) > 2048.dat
:saveBestScore
color
cls
if %bestScore% leq %lastBestScore% goto :EOF
setlocal DisableDelayedExpansion
(for /F "usebackq delims=" %%a in ("%~F0") do (
   set "line=%%a"
   setlocal EnableDelayedExpansion   
   echo !line!
   if "!line!" equ ":getBestScore" goto continue
   endlocal
)) > "%~PN0.tmp"
:continue
echo set bestScore=%bestScore%>> "%~PN0.tmp"
del "%~F0" & ren "%~PN0.tmp" "%~NX0" & goto :EOF


:moveLeft
set points=0
set "anyHole="
for /L %%i in (1,1,4) do (
   set "row=!row[%%i]!"
   set "d="
   rem Eliminate empty places and move tiles to left
   for /F "tokens=1-4 delims=|" %%a in ("!row:|    =!") do (
      set "a=%%a" & set "b=%%b" & set "c=%%c" & set "d=%%d"
      rem Merge tiles with same number
      if defined a (
         if !a! equ !b! (
            set /A "a+=b, points+=a" & set "b=!c!" & set "c=!d!" & set "d="
            if !a! gtr !maxTile! set maxTile=!a!
         )
         set "a=   !a!" & set "row=|!a:~-4!|"
      ) else set "row=|    |"
      if defined b (
         if !b! equ !c! (
            set /A "b+=c, points+=b" & set "c=!d!" & set "d="
            if !b! gtr !maxTile! set maxTile=!b!
         )
         set "b=   !b!" & set "row=!row!!b:~-4!|"
      ) else set "row=!row!    |"
      if defined c (
         if !c! equ !d! (
            set /A "c+=d, points+=c" & set "d="
            if !c! gtr !maxTile! set maxTile=!c!
         )
         set "c=   !c!" & set "row=!row!!c:~-4!|"
      ) else set "row=!row!    |"
      if defined d (
         set "row=!row!!d!|"
      ) else set "row=!row!    |"
   )
   if not defined d set anyHole=true
   set "row[%%i]=!row!"
)
exit /B


:tileSpawn
set /A newI=%random% %% 4 + 1, newJ=!random! %% 4 + 1
if "!board[%newI%,%newJ%]!" neq "    " goto tileSpawn
set /A newVal=%random% %% 10/9*2 + 2
set "board[%newI%,%newJ%]= + %newVal%"
exit /B


rem To reset the best score, modify the value below
:getBestScore
set bestScore=0


Antonio

Post Reply