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.