Game code help?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Game code help?

#76 Post by JWinslow23 » 24 Jun 2014 10:06

einstein, I am actually going to golf my code for a release soon (that means no more names like 00 and 000 that I used for some reason). Also, you forgot to put at the end when you exit:

Code: Select all

del "%temp%\x"
That is needed.

Also, I might take Aacini's advice about the moves, as his algorithm looks like it'd be smaller than mine in practice.

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

Re: Game code help?

#77 Post by JWinslow23 » 24 Jun 2014 20:04

Bump.
Pretty soon, I am going to release this game in full. I have savegames implemented, and I am almost done tweaking it to perfection. Thank you all for your support.

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

Re: Game code help?

#78 Post by Aacini » 24 Jun 2014 21:40

When I saw the color output example above, I wanted to give my 2048.bat output a better presentation. However, such thing is not possible with original Batch file commands, but only via third party programs, so I used my CursorPos.exe, ColorChar.exe and GetInput.exe auxiliary programs and deeply modified previous version of my program. The result is amazing!

Image


EDIT Jun-25-2014: I modified a few small details for a better presentation.

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 This version requires CursorPos.exe, ColorChar.exe and GetInput.exe auxiliary programs
rem Download they from: http://www.dostips.com/forum/viewtopic.php?f=3&t=5701&p=35407#p35407

rem Define the numbers used in output (5 lines each)
for %%m in (
"    |70|32x13|32x13|32x13|32x13|32x13"
"   2|F0|32x5 219x3 32x5     |32x7 219 32x5       |32x5 219x3 32x5|32x5 219 32x7       |32x5 219x3 32x5"
"   4|70|32x5 219 32 219 32x5|32x5 219 32 219 32x5|32x5 219x3 32x5|32x7 219 32x5       |32x7 219 32x5  "
"   8|C7|32x5 219x3 32x5     |32x5 219 32 219 32x5|32x5 219x3 32x5|32x5 219 32 219 32x5|32x5 219x3 32x5"
"  16|CF|32x4 219 32 219x3 32x4       |32x4 219 32 219 32x6         |32x4 219 32 219x3 32x4  |32x4 219 32 219 32 219 32x4  |32x4 219 32 219x3 32x4  "
"  32|47|32x3 219x3 32 219x3 32x3     |32x5 219 32x3 219 32x3       |32x3 219x3 32 219x3 32x3|32x5 219 32 219 32x5         |32x3 219x3 32 219x3 32x3"
"  64|4F|32x3 219x3 32 219 32 219 32x3|32x3 219 32x3 219 32 219 32x3|32x3 219x3 32 219x3 32x3|32x3 219 32 219 32x3 219 32x3|32x3 219x3 32x3 219 32x3"
" 128|27|32x2 219 32 219x3 32 219x3 32x2|32x2 219 32x3 219 32 219 32 219 32x2|32x2 219 32 219x3 32 219x3 32x2|32x2 219 32 219 32x3 219 32 219 32x2|32x2 219 32 219x3 32 219x3 32x2"
" 256|2F|32 219x3 32 219x3 32 219x3 32  |32x3 219 32 219 32x3 219 32x3       |32 219x3 32 219x3 32 219x3 32  |32 219 32x5 219 32 219 32 219 32    |32 219x3 32 219x3 32 219x3 32  "
" 512|E6|32x2 219x3 32 219 32 219x3 32x2|32x2 219 32x3 219 32x3 219 32x2     |32x2 219x3 32 219 32 219x3 32x2|32x4 219 32 219 32 219 32x4         |32x2 219x3 32 219 32 219x3 32x2"
"1024|E0|219 32 219x3 32 219x3 32 219 32 219|219 32 219 32 219 32x3 219 32 219 32 219|219 32 219 32 219 32 219x3 32 219x3|219 32 219 32 219 32 219 32x5 219|219 32 219x3 32 219x3 32x3 219"
           ) do (
   for /F "tokens=1-7 delims=|" %%a in (%%m) do (
      set "num[1,%%a]=/%%b %%c"
      set "num[2,%%a]=/%%b %%d"
      set "num[3,%%a]=/%%b %%e"
      set "num[4,%%a]=/%%b %%f"
      set "num[5,%%a]=/%%b %%g"
   )
)

rem Define the mapping indices tables
set j=0
rem               LEFT               UP               RIGHT             DOWN
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%

cls
echo/
echo 2048 game, original version by Gabriele Cirulli
echo http://gabrielecirulli.github.io/2048/
echo/
echo This Windows batch file version by Antonio Perez Ayala
echo/
echo HOW TO PLAY:
echo - Use your arrow keys to move the tiles
echo   or click on the central tiles of the side to move
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 Del key to start a new game, or Esc key to exit
echo/

rem Load a previously saved game, if any
set "prevGame="
if exist 2048.dat (
   for %%a in (2048.dat) do echo A previous game exists saved on %%~Ta
   choice /M "Do you want to load it "
   if !errorlevel! equ 1 (
      for /F "delims=" %%a in (2048.dat) do set %%a
      del 2048.dat
      set prevGame=true
      echo Game loaded
   )
   echo/
) else (
   set score=0
)
pause

color F0
mode con cols=68 lines=36

:showHeader
cls
echo/
ColorChar /F0 32x38                                                                /8F " SCORE " /F0 32x8 /8F " BEST " 13 10
ColorChar /F0 32x3 219x5 32x2 219x5 32x2 219 32x3 219 32x2 219x5              32x9 /8F " 12345 " /F0 32x8 /8F "12345 " 13 10
ColorChar /F0 32x7 219 32x2 219 32x3 219 32x2 219 32x3 219 32x2 219 32x3 219                                           13 10
ColorChar /F0 32x3 219x5 32x2 219 32x3 219 32x2 219x5 32x2 219x5              32x7 /8F 32x10        /F0 32x6 /8F 32x8       13 10
ColorChar /F0 32x3 219 32x6 219 32x3 219 32x6 219 32x2 219 32x3 219           32x7 /8F " New Game " /F0 32x6 /8F "  Exit  " 13 10
ColorChar /F0 32x3 219x5 32x2 219x5 32x6 219 32x2 219x5                       32x7 /8F 32x10        /F0 32x6 /8F 32x8

if defined prevGame goto showMove

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

:mainLoop
if not defined anyHole (
   echo Not empty space left, end of this game.
   set /P "=Press Enter key to start a new game "
   set "prevGame="
   goto showHeader
)

call :tileSpawn

:showMove
if %maxTile% equ 2048 (
   echo Congratulations: YOU WIN
   pause
   color
   cls
   goto :EOF
)

set /A score+=points
if %score% gtr %bestScore% set bestScore=%score%
set "scoreOut=    %score%"
set "bestOut=    %bestScore%"
CursorPos 39 2
ColorChar /8F "%scoreOut:~-5% " /F0 32x8 /8F "%bestOut:~-5%" 13 10
CursorPos 0 9
ColorChar /F0 32x3 /08 219x62  13 10
for /L %%i in (1,1,4) do (
   for /L %%n in (1,1,5) do (
      set "numLine=/08 219x2"
      for /L %%j in (1,1,4) do for /F "delims=" %%k in ("!board[%%i,%%j]!") do (
         set "numLine=!numline! !num[%%n,%%k]! /08 219x2"
      )
      ColorChar /F0 32x3 !numLine!  13 10
   )
   ColorChar /F0 32x3 /08 219x62  13 10
)

:getKey  Left Up  Right Down End Esc     Left        Up          Right        Down         NewGame    Exit
GetInput 293  294 295   296  302 27  /M  0 16 17 26  20 7 47 14  50 16 67 26  20 28 47 35  36 4 45 6  53 4 60 6
set option=%errorlevel%
if %option% gtr 4 goto option-%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
goto newGame

:option-6 Exit
choice /M "-> Do you want to save this game "
if %errorlevel% equ 1 (
   set points=0
   for %%a in (score points maxTile) do echo %%a=!%%a!
   set board[
) > 2048.dat

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

To create the three auxiliary programs required, run the following Batch file just one time:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem 2048-Support.bat: create CursorPos.exe, ColorChar.exe and GetInput.exe auxiliary programs
rem All support programs developed by Antonio Perez Ayala

for %%a in (CursorPos.exe ColorChar.exe GetInput.exe) do (
   echo Extracting %%a, please wait . . .
   call :ExtractBinaryFile %%a
   echo %%a file created
   echo/
)

goto :EOF


rem Extract Binary File from hexadecimal digits placed in a "resource" in this .bat file

:ExtractBinaryFile filename.ext[.cab]
setlocal EnableDelayedExpansion
set "start="
set "end="
for /F "tokens=1,3 delims=:=>" %%a in ('findstr /N /B "</*resource" "%~F0"') do (
   if not defined start (
      if "%%~b" equ "%~1" set start=%%a
   ) else if not defined end set end=%%a
)
(for /F "skip=%start% tokens=1* delims=:" %%a in ('findstr /N "^" "%~F0"') do (
   if "%%a" == "%end%" goto decodeHexFile
   echo %%b
)) > "%~1.hex"
:decodeHexFile

rem Modified code based on :genchr subroutine
type nul > t.tmp
(for /F "usebackq" %%a in ("%~1.hex") do (
   set input=%%a
   set i=0
   for /L %%I in (0,2,120) do for %%i in (!i!) do if "!input:~%%i,1!" neq "" (
      set hex=!input:~%%i,2!
      set /A i+=2
      if "!hex:~0,1!" neq "[" (
         set /A chr=0x!hex!
         if not exist !chr!.chr call :genchr !chr!
         type !chr!.chr
      ) else (
         for /L %%J in (1,1,5) do for %%i in (!i!) do if "!input:~%%i,1!" neq "]" (
            set "hex=!hex!!input:~%%i,1!"
            set /A i+=1
         )
         if not exist 0.chr call :genchr 0
         for /L %%J in (1,1,!hex:~1!) do type 0.chr
         set /A i+=1
      )
   )
)) > "%~1"
del *.chr
del t.tmp temp.tmp
del "%~1.hex"

rem Expand created file if extension is .cab
set "filename=%~1"
if /I "%filename:~-4%" equ ".cab" (
   expand "%filename%" "%filename:~0,-4%" > NUL
   del "%filename%"
)

exit /B


:genchr
REM This code creates one single byte. Parameter: int
REM Teamwork of carlos, penpen, aGerman, dbenham
REM Tested under Win2000, XP, Win7, Win8
set "options=/d compress=off /d reserveperdatablocksize=26"
if %~1 neq 26 (
   makecab %options% /d reserveperfoldersize=%~1 t.tmp %~1.chr > nul
   type %~1.chr | ( (for /l %%N in (1,1,38) do pause)>nul & findstr "^" > temp.tmp )
   >nul copy /y temp.tmp /a %~1.chr /b
) else (
   copy /y nul + nul /a 26.chr /a >nul
)
exit /B


<resource id="CursorPos.exe">
4d5a900003[3]04[3]ffff[2]b8[7]40[35]b0[3]0e1fba0e00b409cd21b8014ccd21546869732070726f6772616d2063616e6e6f74206265207275
6e20696e20444f53206d6f64652e0d0d0a24[7]5527d9c81146b79b1146b79b1146b79b9f59a49b1946b79bed66a59b1346b79b526963681146b79b
[8]5045[2]4c0102[2]5f1153[8]e0000f010b01050c0002[3]02[7]10[3]10[3]20[4]40[2]10[3]02[2]04[7]04[8]30[3]02[6]03[5]10[2]10[4]
10[2]10[6]10[11]1820[2]28[84]20[2]18[27]2e74657874[3]3c01[3]10[3]02[3]02[14]20[2]602e7264617461[2]d0[4]20[3]02[3]04[14]
40[2]40[8]e806[3]50e81301[2]558bec83c4e06af5e81201[2]8945fc8d45e650ff75fce8fd[3]8b45ea8945e0e8be[3]e8dd[3]803e00746b803e
3d750646e8cd[3]668b4de0e85c[3]8945eae8bc[3]803e00743e803e2c750646e8ac[3]668b4de2e83b[3]668945ec668b45ea6683f8007d0a6603
45e666ff4deceb0e663b45e67c0c662b45e666ff45ec668945ea8b5dea53ff75fce889[3]8b45e0c9c333c032db33d28a164680fa2b740880fa2d75
0980cb0280cb018a164680fa30720f80fa39770a80ea306bc00a03c2ebe9f6c301740bf6c302740366f7d86603c14ec3cccce841[3]8bf08a06463c
2275098a06463c2275f9eb0c8a06463c20740484c075f54ec38a06463c2074f94ec3ccff2510204000ff2500204000ff2504204000ff2508204000ff
250c2040[197]6620[2]8420[2]9420[2]b020[2]5820[6]4020[10]c220[3]20[22]6620[2]8420[2]9420[2]b020[2]5820[6]9b00457869745072
6f6365737300f500476574436f6e736f6c6553637265656e427566666572496e666f[2]6a0147657453746448616e646c65[2]6d02536574436f6e73
6f6c65437572736f72506f736974696f6e[2]e600476574436f6d6d616e644c696e6541006b65726e656c33322e646c6c[306]
</resource>


<resource id="ColorChar.exe">
4d5a900003[3]04[3]ffff[2]b8[7]40[35]b0[3]0e1fba0e00b409cd21b8014ccd21546869732070726f6772616d2063616e6e6f74206265207275
6e20696e20444f53206d6f64652e0d0d0a24[7]55b5b8fd11d4d6ae11d4d6ae11d4d6ae9fcbc5ae18d4d6aeedf4c4ae13d4d6ae5269636811d4d6ae
[8]5045[2]4c010300ff23cd52[8]e0000f010b01050c0004[3]04[7]10[3]10[3]20[4]40[2]10[3]02[2]04[7]04[8]40[3]04[6]03[5]10[2]10
[4]10[2]10[6]10[11]1c20[2]28[84]20[2]1c[27]2e74657874[3]a202[3]10[3]04[3]04[14]20[2]602e7264617461[2]ea[4]20[3]02[3]08[14]
40[2]402e64617461[3]20[4]30[3]02[3]0a[14]40[2]c0[480]e806[3]50e87302[2]558bec81c4f4dffffffc6af5e86e02[2]8945fc6800304000
ff75fce85802[2]8dbdfcdfffff89bdf8dfffffe81302[2]e83202[2]8a064684c00f846001[2]3c2f75672bbdf8dfffff74168d85f4dfffff5057ff
b5f8dfffffff75fce88b01[2]8bbdf8dfffff8a064684c00f842d01[2]2c303c0976082c073c0f76022c208a264684e40f841401[2]80fc20741286
c42c303c0976082c073c0f76022c20d51066a316304000eb853c2275208a064684c00f84e6[3]3c227405880747ebec8a06463c2274f4e961ffffff
3c300f82c9[3]3c390f87c1[3]e8e3[3]8ae080ff2a740a80ff58740580ff7875278adc468a06463c300f829d[3]3c390f8795[3]e8b7[3]0fb7c88a
c3f3aae914ffffff80fc20737780fc09751f8b0d0430400003cf2b8df8dfffff83e10783e908f7d9b020f3aae9ebfeffff2bbdf8dfffff7418508d85
f4dfffff5057ffb5f8dfffffff75fce889[3]588bbdf8dfffff88276a008d85f4dfffff506a01ffb5f8dfffffff75fce8fd[3]6800304000ff75fce8
e4[3]e998feffff882747e990feffff2bbdf8dfffff74168d85f4dfffff5057ffb5f8dfffffff75fce82f[3]33c0c9c3e8[4]6683e00f66b90a008a
3e4680ff30721480ff39770f66f7e180ef30660fb6d76603c2ebe44ec3558bec6a00ff7514ff7510ff750cff7508e880[3]8b4d10578b7d0c66a116
304000f366ab8b0d04304000ff751451ff7510ff750cff7508e85e[3]5f6800304000ff7508e83e[3]c9c21000[6xcc]e847[3]8bf08a06463c2275
098a06463c2275f9eb0c8a06463c20740484c075f54ec38a06463c2074f94ec3ccff2514204000ff2500204000ff2504204000ff2508204000ff250c
204000ff25102040[351]6e20[2]8c20[2]9c20[2]ac20[2]ca20[2]6020[6]4420[10]dc20[3]20[22]6e20[2]8c20[2]9c20[2]ac20[2]ca20[2]
6020[6]9b004578697450726f6365737300f500476574436f6e736f6c6553637265656e427566666572496e666f[2]6a0147657453746448616e646c
65[2]ee025772697465436f6e736f6c654100f2025772697465436f6e736f6c654f757470757441747472696275746500e600476574436f6d6d616e
644c696e6541006b65726e656c33322e646c6c[302]07[489]
</resource>


<resource id="GetInput.exe">
4d5a900003[3]04[3]ffff[2]b8[7]40[35]b0[3]0e1fba0e00b409cd21b8014ccd21546869732070726f6772616d2063616e6e6f74206265207275
6e20696e20444f53206d6f64652e0d0d0a24[7]55917b8611f015d511f015d511f015d59fef06d51bf015d5edd007d513f015d55269636811f015d5
[8]5045[2]4c01030033821753[8]e0000f010b01050c0004[3]04[7]10[3]10[3]20[4]40[2]10[3]02[2]04[7]04[8]40[3]04[6]03[5]10[2]10
[4]10[2]10[6]10[11]2020[2]28[84]20[2]20[27]2e74657874[3]5803[3]10[3]04[3]04[14]20[2]602e7264617461[2]f4[4]20[3]02[3]08[14]
40[2]402e64617461[3]10[4]30[3]02[3]0a[14]40[2]c0[480]558bec81c4e0fafffffc8dbde0feffffe8eb02[2]e80a03[2]8a064684c00f84f5
[3]3c2f754b8a06463c5474043c74752ae8eb02[2]8a0646e88c02[2]66a300304000e8d802[2]8a064684c00f84c3[3]3c2f75198a06463c497404
3c69757cfe0502304000e8b302[2]8a06463c22753d32e48a1d023040008a06463c2274150adb740a3c617c063c7a7f022c206689074747ebe48bc7
8d9de0feffff2bc3d1e8a303304000e87202[2]8a06463c3072103c39770ce80b02[2]6689074747ebe48bd78d9de0feffff2bd3d1ea891503304000
3c2f753b8a06463c4d74043c6d75308dbde0faffffe82f02[2]8a06460ac0740ce8cc01[2]6689074747ebe88bc78d9de0faffff2bc3c1e803a30730
40006af6e81902[2]8945fc8d45f850ff75fce80402[2]8b45f80d90[3]83e0bc50ff75fce80202[2]66833d00304000ff741e6a0066ff3500304000
ff75fce8ed01[2]0bc0b8[4]0f854e01[2]8d45e0506a018d45e450ff75fce8c201[2]66837de4010f85c7[3]837de80074b50fb645f2660bc07447
6683f809750df745f410[3]7404b01feb7d6683f87a7f77f745f402[3]7515803d023040[2]74656683f8617c5f6683e820eb596605a000663d0001
7c4f6683e820eb49668b45ee6683f8210f8c57ffffff6683f8287e246683f82d0f8c47ffffff6683f82e7e146683f8700f8c37ffffff6683f87b0f8f
2dffffff66050001f745f410[3]7404660500018b0d03304000660bc90f8483[3]8dbde0fefffff266af0f85fefeffffa103304000662bc1eb6a6683
7de4020f85e9fefffff745ec03[3]0f84dcfeffff8b45e8f745ec01[3]750380cc80f7d88b0d0730400067e338f7d88bd8c1eb108dbde0faffff663b
077212663b5f02720c663b47047706663b5f06760a83c708e2e4e994feffffa107304000662bc166408bd8ff75f8ff75fce87f[3]53e861[3]6683e0
0f66b90a008a3e4680ff30721480ff39770f66f7e180ef30660fb6d76603c2ebe44ec3[13xcc]e84d[3]8bf08a06463c2275098a06463c2275f9eb0c
8a06463c20740484c075f54ec38a06463c2074f94ec3ccff2518204000ff2500204000ff2504204000ff2508204000ff250c204000ff2510204000ff
25142040[169]7620[2]8820[2]9820[2]ac20[2]be20[2]d420[2]6820[6]4820[10]e620[3]20[22]7620[2]8820[2]9820[2]ac20[2]be20[2]d4
20[2]6820[6]9b004578697450726f6365737300f200476574436f6e736f6c654d6f6465[2]6a0147657453746448616e646c65[2]34025265616443
6f6e736f6c65496e70757441006e02536574436f6e736f6c654d6f6465[2]e80257616974466f7253696e676c654f626a65637400e600476574436f
6d6d616e644c696e6541006b65726e656c33322e646c6c[270]ffff[510]
</resource>


Enjoy! :D

Antonio
Last edited by Aacini on 25 Jun 2014 23:12, edited 1 time in total.

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

Re: Game code help?

#79 Post by JWinslow23 » 25 Jun 2014 09:49

Well, Aacini, this IS impressive... :shock:
However, I don't really like to use utilities if I don't have to. I like plain vanilla.
But this is pretty fun to play!

Also, now I have to change my data file's name We use the same .dat file, and stuff crashes. :?
Release coming within a day or two!

EDIT: Does anyone have a way to make this smaller?

Code: Select all

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  +------+------+------+------+

I tried every combination of for loops I could possibly think of, and nothing to shorten it is working. Help? :?

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

Re: Game code help?

#80 Post by Aacini » 25 Jun 2014 17:13

JWinslow23 wrote:Does anyone have a way to make this smaller?

Code: Select all

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  +------+------+------+------+

I tried every combination of for loops I could possibly think of, and nothing to shorten it is working. Help? :?

Code: Select all

echo  +------+------+------+------+
for /L %%i in (0,4,12) do (
   set /A j=%%i+3
   set "line=|"
   for /L %%j in (%%i,1,!j!) do set "line=!line!!board2[%%j]!|"
   echo !line!
   echo  +------+------+------+------+
)


Antonio

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

Re: Game code help?

#81 Post by einstein1969 » 25 Jun 2014 17:51

JWinslow23 wrote:Well, Aacini, this IS impressive... :shock:
However, I don't really like to use utilities if I don't have to. I like plain vanilla.
But this is pretty fun to play!

Also, now I have to change my data file's name We use the same .dat file, and stuff crashes. :?
Release coming within a day or two!

EDIT: Does anyone have a way to make this smaller?

Code: Select all

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  +------+------+------+------+

I tried every combination of for loops I could possibly think of, and nothing to shorten it is working. Help? :?



Plain Vanilla Mod.
Image
Have to finish... but is possible without utility 8)

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 Mods "plain vanilla" by einstein1969

rem This version NOT requires CursorPos.exe, ColorChar.exe and GetInput.exe auxiliary programs

rem Define the numbers used in output (5 lines each)

rem for %%m in (
rem "    |70|32x13|32x13|32x13|32x13|32x13"

rem "   2|F0|32x5 219x3 32x5     |32x7 219 32x5       |32x5 219x3 32x5|32x5 219 32x7       |32x5 219x3 32x5"
rem "   4|70|32x5 219 32 219 32x5|32x5 219 32 219 32x5|32x5 219x3 32x5|32x7 219 32x5       |32x7 219 32x5  "
rem "   8|C7|32x5 219x3 32x5     |32x5 219 32 219 32x5|32x5 219x3 32x5|32x5 219 32 219 32x5|32x5 219x3 32x5"

rem "  16|CF|32x4 219 32 219x3 32x4       |32x4 219 32 219 32x6         |32x4 219 32 219x3 32x4  |32x4 219 32 219 32 219 32x4  |32x4 219 32 219x3 32x4  "
rem "  32|47|32x3 219x3 32 219x3 32x3     |32x5 219 32x3 219 32x3       |32x3 219x3 32 219x3 32x3|32x5 219 32 219 32x5         |32x3 219x3 32 219x3 32x3"
rem "  64|4F|32x3 219x3 32 219 32 219 32x3|32x3 219 32x3 219 32 219 32x3|32x3 219x3 32 219x3 32x3|32x3 219 32 219 32x3 219 32x3|32x3 219x3 32x3 219 32x3"

rem " 128|27|32x2 219 32 219x3 32 219x3 32x2|32x2 219 32x3 219 32 219 32 219 32x2|32x2 219 32 219x3 32 219x3 32x2|32x2 219 32 219 32x3 219 32 219 32x2|32x2 219 32 219x3 32 219x3 32x2"
rem " 256|2F|32 219x3 32 219x3 32 219x3 32  |32x3 219 32 219 32x3 219 32x3       |32 219x3 32 219x3 32 219x3 32  |32 219 32x5 219 32 219 32 219 32    |32 219x3 32 219x3 32 219x3 32  "
rem " 512|E6|32x2 219x3 32 219 32 219x3 32x2|32x2 219 32x3 219 32x3 219 32x2     |32x2 219x3 32 219 32 219x3 32x2|32x4 219 32 219 32 219 32x4         |32x2 219x3 32 219 32 219x3 32x2"

rem "1024|E0|219 32 219x3 32 219x3 32 219 32 219|219 32 219 32 219 32x3 219 32 219 32 219|219 32 219 32 219 32 219x3 32 219x3|219 32 219 32 219 32 219 32x5 219|219 32 219x3 32 219x3 32x3 219"

rem            ) do (
rem    for /F "tokens=1-7 delims=|" %%a in (%%m) do (
      rem set "num[1,%%a]=/%%b %%c"
      rem set "num[2,%%a]=/%%b %%d"
      rem set "num[3,%%a]=/%%b %%e"
      rem set "num[4,%%a]=/%%b %%f"
      rem set "num[5,%%a]=/%%b %%g"

rem    )
rem )

 rem "    |70|32x13|32x13|32x13|32x13|32x13"
      set "num[1,    ]=7 14"
      set "num[2,    ]=7 14"
      set "num[3,    ]=7 14"
      set "num[4,    ]=7 14"
      set "num[5,    ]=7 14"
rem "   2|F0|32x5 219x3 32x5     |32x7 219 32x5       |32x5 219x3 32x5|32x5 219 32x7       |32x5 219x3 32x5"
      set "num[1,   2]=f 6 0 4 f 6"
      set "num[2,   2]=f 8 0 2 f 6"
      set "num[3,   2]=f 6 0 4 f 6"
      set "num[4,   2]=f 6 0 2 f 8"
      set "num[5,   2]=f 6 0 4 f 6"
rem "   4|70|32x5 219 32 219 32x5|32x5 219 32 219 32x5|32x5 219x3 32x5|32x7 219 32x5       |32x7 219 32x5  "
      set "num[1,   4]=f 6 8 2 f 2 8 2 f 6"
      set "num[2,   4]=f 6 8 2 f 2 8 2 f 6"
      set "num[3,   4]=f 6 8 4 f 6"
      set "num[4,   4]=f 8 8 2 f 6"
      set "num[5,   4]=f 8 8 2 f 6"
rem "   8|C7|32x5 219x3 32x5     |32x5 219 32 219 32x5|32x5 219x3 32x5|32x5 219 32 219 32x5|32x5 219x3 32x5"
      set "num[1,   8]=c 6 7 4 c 6"
      set "num[2,   8]=c 6 7 2 c 2 7 2 c 6"
      set "num[3,   8]=c 6 7 4 c 6"
      set "num[4,   8]=c 6 7 2 c 2 7 2 c 6"
      set "num[5,   8]=c 6 7 4 c 6"



rem Define the mapping indices tables
set j=0
rem               LEFT               UP               RIGHT             DOWN
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%

cls
echo/
echo 2048 game, original version by Gabriele Cirulli
echo http://gabrielecirulli.github.io/2048/
echo/
echo This Windows batch file version by Antonio Perez Ayala
echo with mods "plain vanilla" by eistein1969
echo/
echo HOW TO PLAY:
echo - Use keys ASDW to move the tiles
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 key to start a new game, or X key to exit
echo/

rem Load a previously saved game, if any
set "prevGame="
if exist 2048.dat (
   for %%a in (2048.dat) do echo A previous game exists saved on %%~Ta
   choice /M "Do you want to load it "
   if !errorlevel! equ 1 (
      for /F "delims=" %%a in (2048.dat) do set %%a
      del 2048.dat
      set prevGame=true
      echo Game loaded
   )
   echo/
) else (
   set score=0
)
pause

rem for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"

pushd %temp%
For /L %%c in (1,1,70) do set "$f=!$f!ÿ"
For /L %%l in (1,1,70) do <nul set /p .=%BS%> "!$f:~0,%%l!"
popd

:showHeader
color F8
mode con cols=68 lines=36
cls
echo/

rem ColorChar /F0 32x38                                                                /8F " SCORE " /F0 32x8 /8F " BEST " 13 10

set /p ".=%BS%                                    " <nul
rem ColorChar /F0 32x3 219x5 32x2 219x5 32x2 219 32x3 219 32x2 219x5              32x9 /8F " 12345 " /F0 32x8 /8F "12345 " 13 10
call :ColorChar_ F 3 0 5 F 3 0 5 f 3 0 2 f 3 0 2 f 3 0 5 f 2 /n

set /p ".=%BS%                                    " <nul
rem ColorChar /F0 32x7 219 32x2 219 32x3 219 32x2 219 32x3 219 32x2 219 32x3 219                                           13 10
call :ColorChar_ f 6 0 2 f 3 0 2 f 3 0 2 f 3 0 2 f 3 0 2 f 3 0 2 f 3 0 2 f 2 /n

set /p ".=%BS%                                    " <nul
rem ColorChar /F0 32x3 219x5 32x2 219 32x3 219 32x2 219x5 32x2 219x5              32x7 /8F 32x10        /F0 32x6 /8F 32x8       13 10
call :ColorChar_ f 3 0 5 f 3 0 2 f 3 0 2 f 3 0 5 f 3 0 5 f 2 /n

set /p ".=%BS%                                    " <nul
rem ColorChar /F0 32x3 219 32x6 219 32x3 219 32x6 219 32x2 219 32x3 219           32x7 /8F " New Game " /F0 32x6 /8F "  Exit  " 13 10
call :ColorChar_ f 3 0 2 f 6 0 2 f 3 0 2 f 6 0 2 f 3 0 2 f 3 0 2 f 2 /n

set /p ".=%BS%                                    " <nul
rem ColorChar /F0 32x3 219x5 32x2 219x5 32x6 219 32x2 219x5                       32x7 /8F 32x10        /F0 32x6 /8F 32x8
call :ColorChar_ f 3 0 5 f 3 0 5 f 6 0 2 f 3 0 5 f 2 /n

echo(&echo(&echo(

rem ColorChar /F0 32x3 /08 219x62  13 10
call :ColorChar_ f 4 8 62 /n
for /L %%l in (1,1,4) do (
  echo(&echo(&echo(&echo(&echo(
  call :ColorChar_ f 4 8 62 /n
)

if defined prevGame goto showMove

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

:mainLoop
if not defined anyHole (
   echo Not empty space left, end of game
   pause
   color
   cls
   goto :EOF
)

call :tileSpawn

:showMove
if %maxTile% equ 2048 (
   echo Congratulations: YOU WIN
   pause
   color
   cls
   goto :EOF
)

set /A score+=points
if %score% gtr %bestScore% set bestScore=%score%
set "scoreOut=    %score%"
set "bestOut=    %bestScore%"

rem CursorPos 39 2
mode 68,36

rem ColorChar /8F "%scoreOut:~-5% " /F0 32x8 /8F "%bestOut:~-5%" 13 10
echo(
echo(   ÛÛÛÛÛÛÛÛÛÛÛ ÛÛÛÛÛÛÛÛÛÛÛ
echo(   Û  SCORE  Û Û   BEST  Û
echo(   Û  %scoreOut:~-5%  Û Û  %bestOut:~-5%  Û
echo(   ÛÛÛÛÛÛÛÛÛÛÛ ÛÛÛÛÛÛÛÛÛÛÛ
echo(
echo(
echo(   N:New Game  X:Exit                 PLAIN VANILLA Mod
echo(

rem CursorPos 0 9

rem ColorChar /F0 32x3 /08 219x62  13 10
echo(

for /L %%i in (1,1,4) do (
   for /L %%n in (1,1,5) do (
      rem set "numLine=/08 219x2"
      set "numLine=8 3"
      for /L %%j in (1,1,4) do for /F "delims=" %%k in ("!board[%%i,%%j]!") do (
         rem set "numLine=!numline! !num[%%n,%%k]! /08 219x2"
         set "numLine=!numline! !num[%%n,%%k]! 8 3"
      )
      rem ColorChar /F0 32x3 !numLine!  13 10
      call :ColorChar_ f 4 !numLine! f 2 /n
   )
   rem ColorChar /F0 32x3 /08 219x62  13 10
   echo(
)

:getKey  Left Up  Right Down End Esc     Left      Up        Right       Down        NewGame    Exit

rem GetInput 293  294 295   296  302 27  /M  0 9 2 33  0 7 67 8  65 9 67 33  0 34 67 35  36 4 45 6  53 4 60 6
rem set option=%errorlevel%

choice /c awdsnx /n /m ""
set option=%errorlevel%

if %option% gtr 4 goto option-%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
goto newGame

:option-6 Exit
choice /M "-> Do you want to save this game "
if %errorlevel% equ 1 (
   set points=0
   for %%a in (score points maxTile) do echo %%a=!%%a!
   set board[
) > 2048.dat

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

:ColorChar_ bg_color len [/n]
( pushd %temp% )
:cc_
(
 
  set /a "len=%2-1"
  if %1 equ 0 (set fg=1) else set fg=%1
  if !len! geq 1 for %%. in (!len!) do  findstr /v /a:%1!fg! /R "^$" "!$f:~0,%%.!" nul
  if /i "%~3"=="/n" echo(
  if not "%~3"=="" (
    shift
    shift
    goto :cc_
  )
  popd
goto :eof )


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


einstein1969

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

Re: Game code help?

#82 Post by JWinslow23 » 25 Jun 2014 18:44

:shock:

Well, I'm gonna release soon. It's coming along pretty smoothly (5 KB so far ;) ).

Post Reply