China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 19:04
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to display a dynamically changing nine-grid using batch processing? View 6,093 Replies 31
Original Poster Posted 2007-01-14 01:29 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
I believe many people know the magic square.



Nine in the top, one in the bottom, three on the left, seven on the right, two and four on the shoulders, six and eight on the feet, and the sum of three numbers in horizontal, vertical, and diagonal directions is all 15.

Suddenly I thought, can we use batch processing to display the magic square? It's not just a simple echo.

We need to list the magic square pattern, with one number in each grid, and the numbers in the grids change every few seconds. For example:

Magic square           
4 9 2
3 5 7
8 1 6

Change to
2 9 4
7 5 3
6 1 8

Then change to
6 1 8
7 5 3
2 9 4

Then change to
8 1 6
3 5 7
4 9 2

Then change to
6 7 2
1 5 9
8 3 4

Then change to
8 3 4
1 5 9
6 7 2

And so on. The requirement is to list all possibilities. Remember it's not just listing, but showing one kind each time.


[ Last edited by ccwan on 2007-1-14 at 01:31 AM ]
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 2 Posted 2007-01-14 01:40 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
2 4 6 8
1 3 7 9
5
The format of the River Chart is prescribed
Floor 3 Posted 2007-01-14 01:43 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
It's not about discussing the River Chart and Luo Book, just meet the requirements of the first floor.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 4 Posted 2007-01-14 02:14 ·  中国 广东 广州 番禺区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Nine palaces and eight trigrams, it's obvious that there are only eight results
Floor 5 Posted 2007-01-14 03:08 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
This is difficult.
Floor 6 Posted 2007-01-14 03:50 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
This difficulty is indeed great, it's not easy to find the pattern
我今后在论坛的目标就是做个超级坏人!!!
Floor 7 Posted 2007-01-14 05:23 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The demonstration code is as follows:

@echo off
title Nine-square grid
mode con cols=40 lines=20
set n1=0
setlocal enabledelayedexpansion

:begin
cls
set n5=5
set str= 1 2 3 4 6 7 8 9
set /a n1+=1
if %n1% gtr 9 exit
call :intercept %n1%
set /a n9=15-%n1%-%n5%
call :intercept %n9%
for %%i in (%str%) do (
set flag=
set n2=%%i
set str_tmp=%str%
call :intercept !n2!
set /a n8=15-!n2!-!n5!
set /a n3=15-!n1!-!n2!
set /a n7=15-!n8!-!n9!
set /a n4=15-!n1!-!n7!
set /a n6=15-!n3!-!n9!
for %%j in (!n3! !n4! !n6! !n7! !n8!) do call :intercept %%j
if not "!str!"=="" set flag=1
set /a sum1=!n3!+!n5!+!n7!
set /a sum2=!n4!+!n5!+!n6!
if !sum1! equ 15 if !sum1! equ !sum2! if not defined flag call :result
set str=!str_tmp!
)
goto begin

:intercept
set str=!str: %1=!
goto :eof

:result
cls
echo.
echo.
echo.
echo ┌──┬──┬──┐
echo │ %n1% │ %n2% │ %n3% │
echo ├──┼──┼──┤
echo │ %n4% │ %n5% │ %n6% │
echo ├──┼──┼──┤
echo │ %n7% │ %n8% │ %n9% │
echo └──┴──┴──┘
echo.
echo ________________________________________
echo.
echo Press any key to display the next one
pause>nul
goto :eof

There was a misspelled English word, corrected it: (By the way, the display interface was beautified.

[ Last edited by namejm on 2007-1-13 at 05:38 PM ]
Recent Ratings for This Post ( 9 in total) Click for details
RaterScoreTime
lxmxn +10 2007-01-14 05:27
vkill +5 2007-01-14 05:28
redtek +15 2007-01-14 07:19
pengfei +15 2007-01-14 10:34
ccwan +15 2007-01-14 21:01
chainliq +6 2007-01-15 01:20
zhxy9804 +2 2007-02-24 22:22
htysm +8 2007-04-23 04:32
huahua0919 +1 2007-12-05 10:36
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2007-01-14 05:27 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 9 Posted 2007-01-14 05:30 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  The moderator is really amazing, PF.
Floor 10 Posted 2007-01-14 08:56 ·  中国 广东 珠海 电信
初级用户
Credits 22
Posts 9
Joined 2007-01-14 08:34
19-year member
UID 76474
Gender Male
From 珠海
Status Offline
Originally posted by lxmxn at 2007-1-13 04:30 PM:

  The moderator is really amazing, PF.

I feel the same!~~
Floor 11 Posted 2007-01-14 13:25 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Brother namejm's code is extremely wonderful~~~! Here the nine-square grid refers to a magic square. The nine-square grid given by the original poster is a 3x3 magic square. When transforming, it just swaps the arrays on both sides with the horizontal and vertical centers as the axis. The sums of its rows, columns, and diagonals are all the same numbers added together.

You can also expand it and increase the order to 5 or 7, etc.


A magic square is a square array in which the sum of each row, each column, and the diagonal are all equal. The following is a 3x3 magic square:

8 1 6
3 5 7
4 9 2


Algorithm for printing a magic square:

1. Place 1 in the middle column of the first row;

2. For each number from 2 to n*n, place each number according to the following rule: The row of each number is 1 less than the row of the previous number, and the column number is 1 more. For example, in the above 3x3 magic square, 5 is in the row above and column to the right of 4;

3. If the row of the previous number is 1, then the row of the next number is n (referring to the bottom row). For example: 1 is in the 1st row, then 2 should be placed in the bottom row, and the column number is also increased by 1;

4. When the column number of the previous number is n, the column number of the next number should be 1, and the row number is decreased by 1. For example: 2 is in the last column of the 3rd row, then 3 should be placed in the 2nd row and 1st column;

5. If the position determined by the above rule already has a number, or the previous number is in the 1st row and nth column, then place the next number below the previous number. For example: According to the above regulations, 4 should be placed in the 1st row and 2nd column, but this position is already occupied by 1, so 4 is placed below 3. Since 6 is in the 1st row and 3rd column (i.e., the last column), 7 is placed below 6. In this way, you can get a magic square of any order.



According to the above algorithm, it is completely feasible to implement printing a magic square with batch processing. Probably need to construct a two-dimensional array.

Send a small program I wrote before to print a magic square.

[ Last edited by pengfei on 2007-1-15 at 01:29 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
lxmxn +10 2007-01-14 15:12
Attachments
sigil.rar (4.22 KiB, Credits to download 1 pts, Downloads: 83)
业精于勤而荒于嬉,形成于思而毁于随。
Floor 12 Posted 2007-01-14 15:14 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  To pengfei :

  The small program you wrote is also wonderful, and you have explained the algorithm in it very clearly, wonderful.
Floor 13 Posted 2007-01-14 21:02 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
The level of namejm is really remarkable, I admire it a lot!
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 14 Posted 2007-01-15 01:20 ·  中国 广西 贵港 电信
高级用户
★★
学无尽止
Credits 635
Posts 244
Joined 2006-04-15 05:07
20-year member
UID 53857
Gender Male
From 广西贵港
Status Offline
Master, KOOL````````
学无尽止
Floor 15 Posted 2007-01-15 08:12 ·  中国 广东 广州 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
```
@ECHO OFF
:TOP
SET /A R=%RANDOM%%%8
SET /A R=%R%+%R%%%2
IF %R% LEQ 0 GOTO TOP
IF 0x00%R% EQU 0x00%A% (
SET Ct=1) ELSE (
SET Ct=0)
SET A=%R%
SET /A E=5
SET /A At=15-%A%
SET /A I=%At%-%E%
SET /A It=15-%I%
SET /A X=15-%A%*2-%E%
IF %X% LEQ 0 SET /A X=~%X%+1
SET /A Y=20-%A%-%X%-%I%
IF %Ct% EQU 0 (SET /A C=%X%,G=%Y%) ELSE SET /A C=%Y%,G=%X%
SET /A B=%At%-%C%,D=%At%-%G%,F=%It%-%C%,H=%It%-%G%

ECHO %A% %B% %C%
ECHO %D% %E% %F%
ECHO %G% %H% %I%
pause>nul
cls&&goto top
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
pengfei +15 2007-01-15 10:29
Forum Jump: