2D Array simulation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
iiersnitxa
Posts: 2
Joined: 03 Nov 2020 06:02

2D Array simulation

#1 Post by iiersnitxa » 03 Nov 2020 06:09

hello, you all...
i have a .txt with a specific standard,
00 01 02...
10 11 12...
.
.
90 91 92...
and i want to read a specific 'byte', as if I were searching in a 2d array...
i found a way to skip some lines with a FOR, but I couldnt find a way to jump to the byte I need in that line.
any help?
I want to get a position X,Y and search in the .txt the line %x% and the token %y%.
thanx in advance...
regards from Brasil,

T3RRY
Posts: 97
Joined: 06 May 2020 10:14

Re: 2D Array simulation

#2 Post by T3RRY » 03 Nov 2020 09:20

This can be accomplishd fairly easily using some macro's to define arrays and return variables structured in a way that allows reference to line y value x:

Code: Select all

@Echo off
rem /* Increments line count ; assigns line to indexed variable */
Set "Defline=Set /A "Str{i}+=0 + 1"& Set Str[!Str{i}!]="
rem /* iterates over each token seperated by a standard delimiter present in the varible defined for the Y line ; returns the Xth token encountered */
Set "gettkn=Set "tkn{i}=0"&For %%n in (1 2)Do If %%n==2 ((For /F "Tokens=1,2 Delims={}" %%x in ("![Y;X]!") Do (Set "{Y}=%%x" & Set "{X}=%%y"))&For %%i in (!{Y}!)Do For %%v in (!Str[%%~i]!) Do (Set /A "tkn{i}+=0 + 1" & If !tkn{i}! EQU !{X}! (Set "rtrn{!{Y}!;!{X}!}=%%~v")))Else Set [Y;X]="

Setlocal EnableExtensions EnableDelayedExpansion
rem /* define example lines for "y;x" coordinate indexing */
%Defline%"one a" "one b" "one c"
%Defline%"two a" "two b" "two c"

rem /* display all */
rem /* If Y and X values are valid - gettkn defines Y;X rtrn variable to var rtrn{Yi;Xi} - [ or varname{Yi;Xi} if supplied at expansion via substring modification ]*/
For %%Y in (1 2)Do For %%X in (1 2 3 4)Do %gettkn:rtrn=YourVarName%{%%Y}{%%X}
Set YourVarName{

rem /* find / test target */
echo/test true 2;2=two b
2> nul Set YourVarName{2;2 | findstr.exe /LIC:"{2;2}" 2> nul | findstr.exe /LIC:"two b" > nul 2> nul && Echo true || Echo false
echo/test true 2;2=two c
2> nul Set YourVarName{2;2 | findstr.exe /LIC:"{2;2}" 2> nul | findstr.exe /LIC:"two c" > nul 2> nul && Echo true || Echo false
Echo/test true 1;4
2> nul Set YourVarName{1;4 | findstr.exe /LIC:"{1;4}" > nul 2> nul && Echo true || Echo false
Endlocal
if you want to easily pluck certain values from lines:

Code: Select all

For %%a in (1 2)Do For %%b in (1 3)Do %gettkn%{%%a}{%%b}
To build an "array" from a file:

Code: Select all

For /F "Delims=" %%L in (filename.txt)Do If not "%%L" == "" (
 %Defline%%%L
)
note, if defining lines from a file, the manner the above is constructed will treat any standard delimiter not enclosed in doublequotes [" "][";"][","] etc as indicating a new token when getting the x value.
I want to get a position X,Y and search in the .txt the line %x% and the token %y%.
In regards to the above, Y should relate to the vertical coordinate [ line ] and X the horizontal coordinate [ token ].

I'm curious as to the usage situation though - care to share?
Last edited by T3RRY on 04 Nov 2020 08:26, edited 1 time in total.

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

Re: 2D Array simulation

#3 Post by Aacini » 03 Nov 2020 19:50

You can directly do that Using SETX command to get tokens from a file via X,Y coordinates of the desired token...

Antonio

T3RRY
Posts: 97
Joined: 06 May 2020 10:14

Re: 2D Array simulation

#4 Post by T3RRY » 04 Nov 2020 09:04

A very interesting method - unfortunately I found it to be noticeably slower than loading a file into a set of associated variables and accessing the nTh token. May be more efficient for larger files - I haven't tested it to that extent.

iiersnitxa
Posts: 2
Joined: 03 Nov 2020 06:02

Re: 2D Array simulation

#5 Post by iiersnitxa » 05 Nov 2020 13:24

I'm curious as to the usage situation though - care to share?

hi,
i'm studying a few things and stumbled in a problem... i was trying to transleta a simple game to .bat language, and all my trouble comes with arrays...
the best way, or at least the simplest way i found to simulate the array was populating a .txt file with the values I need and manipulate the .txt file to update the settings... got it?

ex.: in the minesweeper function to count the neighbours of a given cell... it's not possible to do such thin easy in .bat...

i have a noticeable knowledge in coding, and i'm a enthusiast of .bat, i try to translate simple games for fun and learning...

regards,

T3RRY
Posts: 97
Joined: 06 May 2020 10:14

Re: 2D Array simulation

#6 Post by T3RRY » 05 Nov 2020 16:25

I've done a few such games myself - Connect 4, Conways game of life, Tetris, and my own game, Ampersand Hunters. Typically speaking, a source file is not necessary. Often, the simplest approach is to assign a state value to the Y;X coordinates, then use a pair of nested for /L loop to test the state of neigbouring cells and a second 2D array to transfer updated values once the iteration is complete.

All said, there's many ways you can approach this, however I'll share and try to break down a bit the type of approach I favor for dealing with games that care about Y;X positioning, using the tetris I made last weekend as an example - From my recent post here: viewtopic.php?f=3&t=3259&start=15#p63210
[fuller featured version: https://drive.google.com/file/d/1iLm9Rs ... sp=sharing ]

The approach is only valid for windows 10, as I prefer VT codes to position the cursor directly to the desired Y;X coordinate, with easy color changes an added bonus.

Code: Select all

rem ------------- /* test completion of line for each y coord for last piece set in place */
 For /L %%i in (!ymin! 1 !ymax!)Do (
  Set "#of10=0"
  For /L %%c in (2 1 11)Do If Not "!}%%i;%%c!" == "" Set /A "#of10=!#of10!+1"
  If "!#of10!" == "10" (
   Set /A "match=!match!+1"
   Set /A "Score+=(150 * !match!)"
   Set /A "lineabove=%%i-1"
   For /L %%n in (0 1 !lineabove!) Do (
    Set /A "offset=%%n+1"
rem -------------------------- /* transfer updated coord values after iteration complete */
    For %%o in (!offset!)Do For /L %%x in (2 1 11) Do If not "!}%%n;%%x!" == "" (
     Set "n}%%o;%%x=!}%%n;%%x:[%%n;=[%%o;!"
    ) Else (
     Set "n}%%o;%%x="
   ))
   For /L %%Y in (1 1 %%i) Do For /L %%X in (2 1 11) Do Set "}%%Y;%%X=!n}%%Y;%%X!"
   CLS
 ))
rem ----------------------------- /* Update display values for each cell to display file */
 >"%TEMP%\tetris.~tmp" (For /F "Tokens=2* Delims==" %%G in ('Set "}"')Do Echo/%%G)
The above section is the component of the game responsible for determining if a lines been matched and actioning the appropriate game state changes.
ymin and ymax are calculated from the current tetrominos Y coordinate values - The For /L Y coordinate iteration only needs to happen for these lines, triggered when a tetramino collides with a non empty cell below it.
The gamefield is 10 cells wide, offset by 1 from a starting X axis of 1 - So a for /L loop iterating over integers from [2] in steps of [1] stopping at [11] (2,1,11) is used to test the state of each X cell for the current Y axis - 10 non empty cells flagged by the count variable #of10 triggers the If condition declaring a match.
Lines are shifted down from Y=1 to the current tetramino Y coordinate being assessed [%%i] by shifting the definition of each Y;X cell in the offset line to the Y coordinate benath it (in conjunction with modifying the definition that contains the display coordinates and attributes); with the new value being assigned to the temporary n}Y;X 2D array so the original array values aren't modified before the iteration has finished assessing all cells. once the iteration has been completed for the current tetramino Y coordinate, the line:

Code: Select all

   For /L %%Y in (1 1 %%i) Do For /L %%X in (2 1 11) Do Set "}%%Y;%%X=!n}%%Y;%%X!"
 
does the job of updating the original array with the new values


by prefixing all the variables used to display cells and game info, a one piece for loop can be used to update the file used to display fixed screen elements

Code: Select all

 >"%TEMP%\tetris.~tmp" (For /F "Tokens=2* Delims==" %%G in ('Set "}"')Do Echo/%%G)
Generally speaking, it's going to be faster, and easier, to generate /assess / update display elements using For loops - Not necessarily For /L.

Post Reply