The original has been duplicated, forget it, just correct some typos...
Explanation of Batch Push-Square Source Code:
1. Use of Compact Format for Data Definition
First, put the variable definition set into a variable: (Note that the delimiter is unique, here using the ";" sign)
set a=a=i75;b=i79;str=abc
So the value of variable a is: a=i75;b=i79;str=abc
Use variable value replacement during preprocessing:
set %a:;=&set %
Equivalent to the following statements being executed:
set a=i75&set b=i79&set str=abc
Or:
set a=i75
set b=i79
set str=abc
If there are many variables to define, comprehensively considering from the perspective of occupied space and readability, this method has obvious advantages.
2. Implementation Algorithm of Push-Square Program
1). Read the level data into the array: lev(n)
Change the value of n, which is to change the level, here n is %lev%
2). Restore the current level map data and store it in the two-dimensional array: r(n).(m)
(1) Collect the variable names r(n).(m) with the value "☆" into variable ke, and store the number of "☆" into variable ji, and convert them to "★" at the same time; Note: ke saves the variable name, not the variable value.
%ke% expansion later is the real-time value of all two-dimensional variables with the source value "☆"; as long as %ke% is equal to !ji! indicates that the level is passed
(2) Store the two-dimensional variable names as an array: r(n) by row, Note: it is the variable name
%r(n)% expansion later is the real-time value of all two-dimensional arrays in row n
3). Variable ebuf is used as a display buffer, all content to be displayed is stored in ebuf, and all is displayed at once with echo !ebuf! to achieve fast screen refresh.
Because after expansion of for variable %%a, the! number variables in the value will be expanded again, so:
for /l %%a in (1,1,!r!) do for %%b in ("!r%%a!") do set ebuf=!ebuf! %%~b!cr!
The above sequentially stores the expanded values of the variable name array r(n) from row 1 to!r! into the display buffer ebuf, and adds the newline character!cr!
4). First define the operations for up, down, left and right:
"c1=n-;c2=m-;c3=n+;c4=m+"
In this way, the following code can be used:
set/a sm=m,sn=n,!c%in%!=1,bm=m,bn=n,!c%in%!=1
Respectively obtain the source point: sm.sn, step one: bm.bn, step two: m.n, the values of the three coordinates after pressing up/down/left/right
for /f "tokens=1,2" %%a in ("!r%bn%.%bm%! !r%bn%.%bm%!!r%n%.%m%!") do (
Take out the values of the three coordinates and spread them out as: (step one => %%a) (step one + step two =>%%b) For example: %%a=▓ %%b=▓∷
***Various movable combination types have been variables before***
***The undefined ones are considered as non-movable combinations***
for %%c in ("!%%a!!%%b!") do (
%%a %%b is re-expanded and grouped to %%c, connected with the previous example:!▓! => empty,!▓∷! =>▓-∷
Then: ▓-∷ => %%c
for /f "tokens=1,2 delims=-" %%1 in (%%c) do (
Spread out as: %%1=▓, %%2=∷
Next is the if judgment, and according to various situations, the values of the three points and a "big" standing! source! point are readjusted
Note: There is one point, if the value of %%c is empty, such as %%b=▓■, then this for will be idle, that is, even the following if statement will not be executed, making the following set/a m=sm,n=sn be executed, restoring m,n, that is, cannot move
3. Compression and Restoration of Level Data
1). Compression, because full-width is double-byte, using single character conversion can be reduced by half, if ▓=a, zf=▓, then: set zf=!%af%! can be converted back
If there is not only one, and there are consecutive ones, then a number is added after the letter to indicate the number of times, such as: a4, if it exceeds 9 times, then another group is started, such as: a11, then split into a9a2
2). Restoration, reference: the advantage of starting with 囧,
for /f "tokens=2* delims=囧" %%a in (%~nx0) do (set lev!n!=%%a&set/a n+=1)
In this way, when reading, the reading of other non-data lines can be subtracted, and the speed is accelerated
Similarly, convert the characters back, and then use for /l counting to repeat to convert all
if "!tn:%%2=!" neq "!tn!" (set k=%%2) else (set k=1)
for /l %%k in (1,1,!k!) do (
The above!k! is the number of consecutive characters
4. Get rid of choice.exe and no flickering when refreshing the screen
That is to use debug.exe instead, but this is usually built into the system, so third-party...
debug <%~nx0>nul
Call itself as pipeline input, the first line is wrong, only two lines are really useful
e 100 cd 16 86 c4 b4 02 cd 10 b4 4C CD 21 Write code
g Execute
Here, key reading is performed, the current cursor position is set to the first line, first column, and the key data is used as the return code to: %errorlevel%
Definition: "a=i83=10;i1=7;i72=1;i80=3;i75=2;i77=4;i73=8;i81=9;i57=5;i79=6"
Execute: set in=!i%errorlevel%!
You can get the corresponding key according to the definition, if it is not defined, it is empty
Usually when writing a batch processing to return to the first line to display, only cls is used, and then display, obviously the speed is also very fast, but still feel a little flickering.
Directly reset the cursor to the first line, first column, without clearing the screen, and when displaying again, it is on the original basis, so the feeling of flickering is less
If you want to see the assembly source code, it is very simple, remove >nul after debug <%~nx0>nul, and then add a line u between the e line and the g line
5. Player record saving and reading
Here, the record is stored in the first line of the batch processing,
1). Reading
set/p yg=< %~nx0 >nul
Take the first line to variable yg
set yg=!yg:~13,52!
set lev=%yg:@=&rem %
Because the current level record and the passed level record are separated by @, in this way, only the part before @ is given to lev, and the part after @ is commented out by rem
set yg= !yg:* =!
Take the passed data, and keep that there must be spaces before and after
In this way, the data of the current level and the passed levels has been read into the variable
[ Last edited by netbenton on 2009-10-5 at 15:46 ]