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?

#46 Post by JWinslow23 » 19 Jun 2014 22:44

Easy to find through a Google search.

And I do have a form of Aspberger's. It's in the autism spectrum. I even don't understand most of the details.

You know, I thought this would be a simple get-code-and-be-done, but this has now turned to get-suggestions, get-cruel-remarks-about-a-misunderstanding, rewrite-the-code, repeat-ad-nauseum. To quote Seth McFarlane in "A Million Ways To Die In The West", "THAT WENT SOUTH SO FAST!"

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

Re: Game code help?

#47 Post by Squashman » 20 Jun 2014 08:34


Anyone who has been a NIX based shell scripting programmer and a batch programmer will probably always say that most NIX shells and the additional utilities installed on NIX systems are far more powerful then CMD.exe and the additional executables that Microsoft provides with Windows.

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

Re: Game code help?

#48 Post by dbenham » 20 Jun 2014 08:58

Squashman wrote:

Anyone who has been a NIX based shell scripting programmer and a batch programmer will probably always say that most NIX shells and the additional utilities installed on NIX systems are far more powerful then CMD.exe and the additional executables that Microsoft provides with Windows.

I think you can safely remove the word "probably" from that sentance. :wink:

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

Re: Game code help?

#49 Post by JWinslow23 » 20 Jun 2014 09:38

* sentence :P

And now, there should be some code comparable to the Batch programming language.

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

Re: Game code help?

#50 Post by Squashman » 20 Jun 2014 10:19

JWinslow23 wrote:* sentence :P

And now, there should be some code comparable to the Batch programming language.

I was a Linux admin for close to 5 years and there are plenty of scripts that I have never been able to natively port to a Windows Batch file. Usually need to install UnixUtils on Windows to make them work.

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

Re: Game code help?

#51 Post by JWinslow23 » 20 Jun 2014 10:41

Squashman wrote:
JWinslow23 wrote:* sentence :P

And now, there should be some code comparable to the Batch programming language.

I was a Linux admin for close to 5 years and there are plenty of scripts that I have never been able to natively port to a Windows Batch file. Usually need to install UnixUtils on Windows to make them work.


There have been more complicated games that have been ported to vanilla Batch. And besides, shouldn't this be simple enough to port?

EDIT: I have found an algorithm that I believe is simple enough to code in Batch to do what I want. I will be done with it soon enough if my assumptions and code are correct.

EDIT 2: Part 1 of 3 completed.

EDIT 3: I did it! I made a working left slide!

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
set board[1]=4
set board[2]=2
: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 call :moveleft
if %errorlevel%==3 rem DOWN
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

:moveleft
call :compress
call :merge
call :compress
exit /b

:compress
set x=-1
:1
set /a x+=1
for /l %%g in (0,1,3) do set tmparray[%%g]=0
set w=0
set y=-1
:2
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 2
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 1
exit /b

:merge
set x=-1
:3
set /a x+=1
set y=-1
:4
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
if %y% neq 2 goto 4
if %x% neq 3 goto 3
exit /b

Soon, I will make a slide for all 4 directions, and that part will finally be done! I will then work on it with little to no further assistance.

EDIT 4: It's a bit hackish, but it works!

Code: Select all

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

:moveleft
call :compress
call :merge
call :compress
exit /b

:compress
set x=-1
:1
set /a x+=1
for /l %%g in (0,1,3) do set tmparray[%%g]=0
set w=0
set y=-1
:2
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 2
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 1
exit /b

:merge
set x=-1
:3
set /a x+=1
set y=-1
:4
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
if %y% neq 2 goto 4
if %x% neq 3 goto 3
exit /b

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

I would like some help making the code smaller and faster, but I have a solution.

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

Re: Game code help?

#52 Post by einstein1969 » 20 Jun 2014 13:14

I don't read the full thread...

I initiated to trasform the bash in cmd. It's simple

Code: Select all

@echo off & setlocal EnableDelayedExpansion

rem from 3 to 9
set board_size=5

rem power of 2
set target=2048

set /a fields_total=board_size*board_size
set /a index_max=board_size-1

for i in $(_seq 0 $fields_total); do board[$i]="0"; done

set /a let pieces=0
call :generate_piece

first_round=$last_added

call :generate_piece

:loop
  call :print_board
  call :key_react

  let change && generate_piece
  if !change! neq 0 call :generate_piece

  set /a first_round=-1

  rem let pieces==fields_total && {
  if !piece! equ !field_total! (
   call :check_moves

   rem let moves==0 && end_game 0 #lose the game
   if !moves! equ 0 call :end_game 0
  )

goto :loop


:end_game
  call :print_board
  echo GAME OVER
  echo Your score:!score!
  echo(
  if %1 neq 0 (
    echo Congratulation you have achieved !target!
  ) else echo You have lost, better luck next time.
exit /b

:key_react
  set change=0
 
  rem legge un tasto con xcopy

  if /I !key!==A call :apply_push left
  if /I !key!==S call :apply_push down
  if /I !key!==D call :apply_push right
  if /I !key!==W call :apply_push up
exit /b

:check_moves
 set moves=0
 call :apply_push up fake
 call :apply_push down fake
 call :apply_push left fake
 call :apply_push right fake
exit /b

:apply_push
  for /L %%i in (0,1,!index_max!) do (
    for /L %%j in (0,1,!index_max!) do (
      set flag_skip=0
      set /a increment_max=index_max-%%j
      for /L %%k in (1,1,!increment_max!) do (
        if !flag_skip! equ 0 call :push_pieces %%i %%j %%k %1 %2
      )
    )
  )
exit /b


# perform push operation between two pieces
# inputs:
# $1 - push position, for horizontal push this is row, for vertical column
# $2 - recipient piece, this will hold result if moving or joining
# $3 - originator piece, after moving or joining this will be left empty
# $4 - direction of push, can be either "up", "down", "left" or "right"
# $5 - if anything is passed, do not perform the push, only update number
# of valid moves
# $board - original state of the game board
# outputs:
# $change - indicates if the board was changed this round
# $flag_skip - indicates that recipient piece cannot be modified further
# $board - new state of the game board
:push_pieces

  if /I "%4"=="UP" (

  ) else (
    if /I "%4"=="DOWN" (
    ) else (
      if /I "%4"=="LEFT" (

      ) else (
        if /I "%4"=="RIGHT" (

        )
      )
    )
  )

exit /b



This is not finished...

einstein1969

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

Re: Game code help?

#53 Post by Aacini » 20 Jun 2014 13:57

Perhaps someone that do know 2048 game could describe the "sliding rules" for each direction, please? I really want NOT to spent time playing with it just to get this data! :| Thanks...

Antonio

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

Re: Game code help?

#54 Post by JWinslow23 » 20 Jun 2014 14:14

OK, Aacini. Now that I have figured it out, I'll tell you as you wish...
This is an example for a down slide (this can be adapted to every direction):
1. Move tiles downwards without merging them.
For example,
0 0 2 0
4 4 2 2
0 0 2 2
2 4 2 0
turns to
0 0 2 0
0 0 2 0
4 4 2 2
2 4 2 2
2. For every tile expect for the top row, have the tiles directly above every tile double the value of the tested tile if they are equal.
For example:
0 0 2 0
0 0 2 0
4 4 2 2
2 4 2 2
turns to
0 0 0 0
0 0 4 0
4 0 0 0
2 8 4 4
3. Repeat step 1.
So
0 0 0 0
0 0 4 0
4 0 0 0
2 8 4 4
turns to
0 0 0 0
0 0 0 0
4 0 4 0
2 8 4 4
4. Done.

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

Re: Game code help?

#55 Post by penpen » 20 Jun 2014 14:59

This may help you (abstracted from the directions, using indirect block data structure):

Code: Select all

@echo off
setlocal enableDelayedExpansion
call :initialize

:: the next line sets a specific board setup (all 2)
echo specific board setup
for /l %%g in (0,1,15) do set "board[%%g]=2"

call :plot
:: simulated key input (W A S D)
for %%K in (W A S D) do (
   echo key pressed: %%K
   call :move %%K
   call :plot
)


endlocal
exit /b 0



:plot
   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]!
   echo(
   exit /B 0

:: %~1 direction in {W, A, S, D} (not checked)
:move
   
   for %%A in (!block[%~1]!) do for /F "tokens=1-4 delims= " %%B in (%%A) do (
      set /A "index=%%B", "value=!board[%%B]!", "start=%%B+%%C"

      for /L %%a in (!start!, %%C, %%D) do (
         if "!value!" == "0" (
            for %%i in ("!index!") do set /A "board[%%i]=value=!board[%%a]!", "board[%%a]=0"
         ) else if "!value!" == "!board[%%a]!" (
            for %%i in ("!index!") do set /A "board[%%i]*=2", "value=board[%%a]=0", "index+=%%C"
         ) else (
            set /A "index+=%%C"
            for %%i in ("!index!") do set /A "value=board[%%i]"
         )
      )
   )
   exit /B 0


:: keys
::  W
:: ASD
::
:: board (indices)
::  0  1  2  3
::  4  5  6  7
::  8  9 10 11
:: 12 13 14 15
::
:: blocks in WASD directions
:initialize
   for /l %%g in (0,1,15) do set "board[%%g]=0"
   set "block[W]="0 4 12" "1 4 13" "2 4 14" "3 4 15""
   set "block[A]="0 1 3" "4 1 7" "8 1 11" "12 1 15""
   set "block[s]="12 -4 0" "13 -4 1" "14 -4 2" "15 -4 3""
   set "block[D]="3 -1 0" "7 -1 4" "11 -1 8" "15 -1 12""

   exit /b 0

penpen

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

Re: Game code help?

#56 Post by JWinslow23 » 20 Jun 2014 15:01

Well, I have almost everything...

Code: Select all

@echo off
setlocal enabledelayedexpansion
:startgame
set score=0
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
:startloop
cls
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=     
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]:~-6%^|%board2[1]:~-6%^|%board2[2]:~-6%^|%board2[3]:~-6%^|
echo +------+------+------+------+
echo ^|%board2[4]:~-6%^|%board2[5]:~-6%^|%board2[6]:~-6%^|%board2[7]:~-6%^|
echo +------+------+------+------+
echo ^|%board2[8]:~-6%^|%board2[9]:~-6%^|%board2[10]:~-6%^|%board2[11]:~-6%^|
echo +------+------+------+------+
echo ^|%board2[12]:~-6%^|%board2[13]:~-6%^|%board2[14]:~-6%^|%board2[15]:~-6%^|
echo +------+------+------+------+
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 :moveleft & call :rotateclockwise
if %errorlevel%==2 call :moveleft
if %errorlevel%==3 call :rotateclockwise & call :moveleft & call :rotateclockwise & call :rotateclockwise & call :rotateclockwise
if %errorlevel%==4 call :rotateclockwise & call :rotateclockwise & call :moveleft & call :rotateclockwise & call :rotateclockwise
if %errorlevel%==5 exit /b
if %errorlevel%==6 goto startgame
:backtoloop
set boardchanged=0
for /l %%g in (0,1,15) do if !board[%%g]! neq !tmpboard[%%g]! set boardchanged=1
if %boardchanged%==1 call :tilespawn
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

:moveleft
call :compress
call :merge
call :compress
exit /b

:compress
set x=-1
:1
set /a x+=1
for /l %%g in (0,1,3) do set tmparray[%%g]=0
set w=0
set y=-1
:2
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 2
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 1
exit /b

:merge
set x=-1
:3
set /a x+=1
set y=-1
:4
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 /a score+=!board[%w%]! & set board[%z%]=0
if %y% neq 2 goto 4
if %x% neq 3 goto 3
exit /b

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

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

Re: Game code help?

#57 Post by JWinslow23 » 20 Jun 2014 16:28

Does anybody have a way to check if you lost? You lose if you can't merge any tiles together.

Here is the code so far (put loss check where it says):

Code: Select all

@echo off
setlocal enabledelayedexpansion
:startgame
set score=0
for /l %%g in (0,1,15) do set board[%%g]=0
call :tilespawn
call :tilespawn
for /l %%g in (0,1,15) do set board[%%g]=2
:startloop
cls
for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=     
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]:~-6%^|%board2[1]:~-6%^|%board2[2]:~-6%^|%board2[3]:~-6%^|
echo  +------+------+------+------+
echo  ^|%board2[4]:~-6%^|%board2[5]:~-6%^|%board2[6]:~-6%^|%board2[7]:~-6%^|
echo  +------+------+------+------+
echo  ^|%board2[8]:~-6%^|%board2[9]:~-6%^|%board2[10]:~-6%^|%board2[11]:~-6%^|
echo  +------+------+------+------+
echo  ^|%board2[12]:~-6%^|%board2[13]:~-6%^|%board2[14]:~-6%^|%board2[15]:~-6%^|
echo  +------+------+------+------+
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 :moveleft & call :rotateclockwise
if %errorlevel%==2 call :moveleft
if %errorlevel%==3 call :rotateclockwise & call :moveleft & call :rotateclockwise & call :rotateclockwise & call :rotateclockwise
if %errorlevel%==4 call :rotateclockwise & call :rotateclockwise & call :moveleft & call :rotateclockwise & call :rotateclockwise
if %errorlevel%==5 exit /b
if %errorlevel%==6 goto startgame
:backtoloop
set boardchanged=0
for /l %%g in (0,1,15) do if !board[%%g]! neq !tmpboard[%%g]! set boardchanged=1
if %boardchanged%==1 call :tilespawn

::PUT LOSING CODE HERE
::MAKE IT GOTO youlost IF YOU LOST

goto startloop
exit /b

:youlost
echo  ___     ___    _  _      ___
echo ^|__ \   / _ \  ^| ^|^| ^|    / _ \
echo    ) ^| ^| ^| ^| ^| ^| ^|^| ^|_  ^| (_) ^|
echo   / /  ^| ^| ^| ^| ^|__   _^|  ^> _ ^<
echo  / /_  ^| ^|_^| ^|    ^| ^|   ^| (_) ^|
echo ^|____^|  \___/     ^|_^|    \___/
echo(
echo Join the numbers and get to the
echo            2048 tile^^!
echo(
echo            Game Over
echo(
echo  Score: %score%
echo  +------+------+------+------+
echo  ^|%board2[0]:~-6%^|%board2[1]:~-6%^|%board2[2]:~-6%^|%board2[3]:~-6%^|
echo  +------+------+------+------+
echo  ^|%board2[4]:~-6%^|%board2[5]:~-6%^|%board2[6]:~-6%^|%board2[7]:~-6%^|
echo  +------+------+------+------+
echo  ^|%board2[8]:~-6%^|%board2[9]:~-6%^|%board2[10]:~-6%^|%board2[11]:~-6%^|
echo  +------+------+------+------+
echo  ^|%board2[12]:~-6%^|%board2[13]:~-6%^|%board2[14]:~-6%^|%board2[15]:~-6%^|
echo  +------+------+------+------+
echo(
echo Press any key to try again...
pause >nul
goto startgame

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

:moveleft
call :compress
call :merge
call :compress
call :compress
exit /b

:compress
set x=-1
:1
set /a x+=1
for /l %%g in (0,1,3) do set tmparray[%%g]=0
set w=0
set y=-1
:2
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 2
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 1
exit /b

:merge
set x=-1
:3
set /a x+=1
set y=-1
:4
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 4
if %x% neq 3 goto 3
exit /b

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

I can't, for the life of me, put in a losscheck without it not working or changing something.

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

Re: Game code help?

#58 Post by Aacini » 20 Jun 2014 18:02

You may perform this segment:

Code: Select all

:rotateclockwise
set /a tmparray[0]=!board[12]!
set /a tmparray[1]=!board[8]!
set /a tmparray[2]=!board[4]!
set /a tmparray[3]=!board[0]!
set /a tmparray[4]=!board[13]!
set /a tmparray[5]=!board[9]!
set /a tmparray[6]=!board[5]!
set /a tmparray[7]=!board[1]!
set /a tmparray[8]=!board[14]!
set /a tmparray[9]=!board[10]!
set /a tmparray[10]=!board[6]!
set /a tmparray[11]=!board[2]!
set /a tmparray[12]=!board[15]!
set /a tmparray[13]=!board[11]!
set /a tmparray[14]=!board[7]!
set /a tmparray[15]=!board[3]!

... this way:

Code: Select all

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


Antonio

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

Re: Game code help?

#59 Post by Aacini » 20 Jun 2014 18:03

I think I got it :!:

EDIT - Jun/21/2014: I improved the program in several points.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

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

set i=0
for %%a in (UP DOWN LEFT RIGHT) do (
   set /A i+=1
   set dir[!i!]=%%a
)

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 continue 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 start

: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: W=Up, S=Down, A=Left, D=Right
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, maxNum=0
goto start

:startLoop
if not defined anyHole echo/& echo Not empty space left, you lost... & goto :EOF
call :tileSpawn
:start
echo/
set /A move+=1, prevScore=score, score+=points
echo +----+----+----+----+    Move: %move%, Score: %prevScore%+%points% = %score%
for /L %%i in (1,1,4) do (
   set "line=|"
   for /L %%j in (1,1,4) do set "line=!line!!board[%%i,%%j]!|"
   echo !line!
   if %%i neq 4 echo +----+----+----+----+
)
set /P "=+----+----+----+----+    " < NUL
if defined newVal set "board[%newI%,%newJ%]=   %newVal%"
if %maxNum% equ 2048 echo/& echo Congratulations: YOU WON & goto :EOF
:getKey
choice /C WSADNX /M "Press key: "
set option=%errorlevel%
echo/
if %option% leq 4 echo Moving !dir[%option%]!
goto option-%option%

:option-1 Up
call :getVer "1,2,3,4"
call :moveLeft
call :putVer  1,2,3,4
goto startLoop

:option-2 Down
call :getVer "4,3,2,1"
call :moveLeft
call :putVer  4,3,2,1
goto startLoop

:getVer order
for /L %%j in (1,1,4) do (
   set "row[%%j]=|"
   for %%i in (%~1) do (
      set "row[%%j]=!row[%%j]!!board[%%i,%%j]!|"
   )
)
exit /B

:putVer order
for /L %%j in (1,1,4) do (
   for /F "tokens=1-4 delims=|" %%a in ("!row[%%j]!") do (
      set "board[%1,%%j]=%%a" & set "board[%2,%%j]=%%b" & set "board[%3,%%j]=%%c" & set "board[%4,%%j]=%%d"
   )
)
exit /B

:option-3 Left
call :getHor "1,2,3,4"
call :moveLeft
call :putHor  1,2,3,4
goto startLoop

:option-4 Right
call :getHor "4,3,2,1"
call :moveLeft
call :putHor  4,3,2,1
goto startLoop

:getHor order
for /L %%i in (1,1,4) do (
   set "row[%%i]=|"
   for %%j in (%~1) do (
      set "row[%%i]=!row[%%i]!!board[%%i,%%j]!|"
   )
)
exit /B

:putHor order
for /L %%i in (1,1,4) do (
   for /F "tokens=1-4 delims=|" %%a in ("!row[%%i]!") do (
      set "board[%%i,%1]=%%a" & set "board[%%i,%2]=%%b" & set "board[%%i,%3]=%%c" & set "board[%%i,%4]=%%d"
   )
)
exit /B

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

:option-6 Exit
echo/
choice /M "Are you sure "
if %errorlevel% neq 1 goto getKey
color
cls
choice /M "Do you want to save this game "
if %errorlevel% neq 1 exit /B
(
set /A move-=1, points=0
for %%a in (move score points maxNum) do echo %%a=!%%a!
for /L %%i in (1,1,4) do (
   for /L %%j in (1,1,4) do (
      echo "board[%%i,%%j]=!board[%%i,%%j]!"
   )
)
) > 2048.dat
echo Game saved.
exit /B


:moveLeft
set points=0
set "anyHole="
for /L %%i in (1,1,4) do (
   set "row=!row[%%i]!"
   set "d="
   for /F "tokens=1-4 delims=|" %%a in ("!row:|    =!") do (
      set "a=%%a" & set "b=%%b" & set "c=%%c" & set "d=%%d"
      if defined a (
         if !a! equ !b! (
            set /A "a+=b, points+=a" & set "b=!c!" & set "c=!d!" & set "d="
            if !a! gtr !maxNum! set maxNum=!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 !maxNum! set maxNum=!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 !maxNum! set maxNum=!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


EDIT: I modified this program, see above...

Antonio

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

Highscore/board saving?

#60 Post by JWinslow23 » 21 Jun 2014 11:35

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.

Post Reply