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-08-05 23:04
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original] Guess the number game based on returning the correct number of digits and their counts View 2,482 Replies 16
Original Poster Posted 2007-07-30 07:48 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
### Simple Introduction of this game
There are four decimal digits, and you can guess 8 times.
Each time, it returns aAbB.
A means the digit is correct and in the correct position.
B means the digit is correct but in the wrong position.”

For example: Suppose the number to guess is 1234.
If the player guesses 0134, it returns 2A1B (3, 4 are A, 1 is B).
----------------------------------------------------------------------

### About this code
Because it's only initially implemented,
it's inevitable to be somewhat crude.
Now I feel I took a wrong turn on JudgeNum.
I'll improve it when I have time.
Reverse guessing really tests the program algorithm.
Everyone, if you have any good ideas,
feel free to put them forward for discussion.


:: Guess the number game : qzwqzw : 2007-07-29
:: From: http://www.cn-dos.net/forum/
@echo off & setlocal EnableDelayedExpansion
:Start - Game starts
set nCount=0
mode con cols=40 lines=25 & cls
echo.
echo The number guessing game starts now, I set the number and you guess.
echo.
for /l %%i in (0,1,9) do set n%%i=%%i
set i=0

:GenLoop - Use random exchange algorithm to generate four non-repeating random numbers to form the target number
setlocal
set r=%random:~-1,1%
call set tmp=%%n%i%%%
call set n%i%=%%n%r%%%
call set n%r%=%tmp%
set /a i+=1
if %i% neq 4 goto :GenLoop
endlocal & set nDest=%n0%%n1%%n2%%n3%

:Input - Wait for user to input the guessed number
set nGuess=
set /p nGuess= Please enter the guessed number (press A to give up):
if /i "%nGuess%"=="a" set "End=Abort" & goto :End
set /a nGuess=1%nGuess% 2>nul || (echo ERR1: Invalid number, please re-enter & goto :Input)
if %nGuess% lss 10123 echo ERR2: Invalid number, please re-enter & goto :Input
if %nGuess% gtr 19876 echo ERR3: Invalid number, please re-enter & goto :Input
set nGuess=%nGuess:~1,4%

:JudgeNum - Judge the accuracy of the guessed number
setlocal EnableDelayedExpansion
set /a na=0,nb=0
for /l %%i in (0,1,3) do (
set bitDest=!nDest:~%%i,1!
set bitGuess=!nGuess:~%%i,1!
if !bitDest!==!bitGuess! (
set /a na+=1
) else (
set numt=!numt! !bitDest!
set gnumt=!gnumt! !bitGuess!
)
)
for %%i in (%numt%) do (
echo.%gnumt%|find "%%i">nul
if not errorlevel 1 (
set /a nb+=1
)
)
endlocal & set na=%na%&set nb=%nb%

set /a nCount+=1
echo The %nCount%th judgment result: %na%A%nb%B
set strGuess=%strGuess% %nGuess%-%na%A%nb%B
if "%nCount%"=="9" set "End=Failure" & goto :End
if not "%na%"=="4" goto :Input
set End=Success

:End
echo.
set /p choose= Guessed %End%, target is %nDest%, continue? (Y/N)
echo %End%:-: %strGuess% >> guess.log
set strGuess=
if "%choose%"=="y" goto :Start


[ Last edited by qzwqzw on 2007-9-25 at 11:19 AM ]
Floor 2 Posted 2007-07-30 10:20 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
The song that is too lofty has few who can sing along. One can only watch the dust and be unable to catch up.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2007-07-31 02:39 ·  中国 广东 东莞 电信
初级用户
Credits 50
Posts 20
Joined 2007-06-17 00:18
19-year member
UID 91565
Gender Male
From 湖南常宁
Status Offline
Floor 4 Posted 2007-07-31 08:08 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
It seems that no one has provided opinions.
Post a demo of reverse guessing by myself.

The Judge has been modified.
The premise is that the digits of the target number to be compared are all unique. This can be limited in the non - demo.

The algorithm of Guess is relatively weak.
It probably needs 5 to 8 times in terms of both speed and the number of guesses.
In the 2nd to 4th times, there may be a situation where guessing is very slow.
Because it is judging one by one at that time.

If each time we carry out the judgment one by one, then the number of guesses will be effectively reduced, but the guessing time will be greatly increased.
So the existing algorithm is the result after comprehensive consideration.


:: Number guessing game (demonstration version) : qzwqzw : 2007 - 07 - 30
:: From: http://www.cn - dos.net/forum/
@echo off & setlocal EnableDelayedExpansion

mode con cols = 40 lines = 25
:Start - Game start
setlocal & cls
echo.
echo The number guessing game starts now, you choose the number and I will guess
echo.
call :Input iDest
if "%iDest%"=="" goto :eof
:: echo Guess target value: %iDest%

:GuessLoop - Game main loop
set /a iCount += 1
call :Guess iGuess %iGuess% %iNumA% %iNumB% %iCount%
if "%iGuess%"=="" set "End=Failure" & goto :End
call :Judge iNumA iNumB %iGuess% %iDest%
echo.
echo. The %iCount%th guess: %iGuess%, the judgment value is: %iNumA%A %iNumB%B
set "strGuess=%strGuess% %iGuess%:%iNumA%A%iNumB%B"
if "%iCount%"=="9" set "End=Failure" & goto :End
if "%iNumA%"=="4" set "End=Success" & goto :End
goto GuessLoop

:Input - Accept and save the target number entered by the user
if "%1"=="" goto :eof
set _input =
set /p _input= Please enter the number to guess (press A to give up):
if /i "%_input%"=="a" goto :eof
set /a _input = 1%_input% 2>nul || (echo ERR1: Invalid number, please enter again & goto :Input)
if %_input% lss 10123 echo ERR2: Invalid number, please enter again & goto :Input
if %_input% gtr 19876 echo ERR3: Invalid number, please enter again & goto :Input
set "%1=%_input:~1,4%"
goto :eof

:Guess - I will guess the target number
if not "%4"=="" goto :GuessNext
:GuessFirst - The first guess directly generates a number table containing all unique four - digit numbers
if exist guess.tbl (
copy guess.tbl _guess.tbl > nul
set _count = 5040
goto :GuessSelect
)
if exist _guess.tbl del _guess.tbl
for /l %%i in (0,1,9) do (
for /l %%j in (0,1,9) do (
if not %%i == %%j for /l %%k in (0,1,9) do (
if not %%i == %%k if not %%j == %%k for /l %%l in (0,1,9) do (
if not %%i == %%l if not %%j == %%l if not %%k == %%l (
echo %%i%%j%%k%%l>>_guess.tbl
set /a _count += 1
)
)
)
)
)
copy _guess.tbl guess.tbl > nul
goto :GuessSelect

:GuessNext - Use findstr to simply and quickly screen the number table according to the similarity value
if not exist _guess.tbl goto :eof
set %1=
set _gss=%2
set /a _n=%3+%4
if "%_n%"=="0" set pattern="^*$"
if "%_n%"=="1" set pattern="^**$"
if "%_n%"=="2" set pattern="^***$"
if "%_n%"=="3" set pattern="^**$"
if "%_n%"=="4" set pattern="^*$"
findstr /r %pattern% _guess.tbl > _guess.tmp
for /f %%f in ('find /v /c "" ^<_guess.tmp') do set _count=%%f
if "%_count%"=="0" echo ERR4: Retrieval failed, please check the similarity value & goto :eof
copy _guess.tmp _guess.tbl > nul
for /f %%f in ('find /v /c "" ^<_guess.tbl') do set _count=%%f

set _isJudge=true
if 1%5 lss 13 set _isJudge=false
if 1%_n% equ 10 set _isJudge=false
if 1%3 equ 14 set _isJudge=true
if 1%_count% gtr 1500 set _isJudge=false
if 1%_count% lss 150 set _isJudge=true
if "%_isJudge%"=="false" goto :GuessSelect

:GuessJudge - Judge the number table one by one according to the similarity value and screen out the completely conforming number table
if exist _guess.tbl del _guess.tbl
set _nf=%2
for /f %%f in (_guess.tmp) do (
set _ng=%%f
set /a _numa=0, _numb=0
for /l %%i in (0,1,3) do (
set _nfb=!_nf:~%%i,1!
set _ngb=!_ng:~%%i,1!
if "!_nfb!"=="!_ngb!" (
set /a _numa+=1
) else (
echo.!_ng!|find "!_nfb!">nul && set /a _numb+=1
)
)
if 1%3==1!_numa! if 1%4==1!_numb! (
echo %%f>>_guess.tbl
)
)

:GuessSelect - Randomly select one from the screened number table as the new guess number
copy _guess.tbl _guess.sel > nul
for /f %%f in ('find /v /c "" ^<_guess.sel') do set _count=%%f
set /a rn=%random% %% _count + 1
for /f "tokens=2 delims=] " %%f in (
'find /v /n "" _guess.sel^|find ""'
) do (
set _guess=%%f
)
copy _guess.tbl guess_%5.txt > nul
endlocal & set %1=%_guess%
goto :eof

:Judge - Judge the accuracy of the guess number
setlocal EnableDelayedExpansion
set /a _numa=0,_numb=0
if not "%4"=="" set iGuess=%3&set iDest=%4
for /l %%i in (0,1,3) do (
set _dest=!iDest:~%%i,1!
set _guess=!iGuess:~%%i,1!
if !_dest!==!_guess! (
set /a _numa+=1
) else (
echo.%iGuess%|find "!_dest!">nul && set /a _numb+=1
)
)
endlocal & set %1=%_numa%&set %2=%_numb%
goto :eof

:End - Post - processing after a single game of the game
echo.
set /p choose= Guessed %End%, the target is %iDest%, continue? (Y/N)
echo %End%:-: %strGuess% >> guess.log
set strGuess=
endlocal & if /i "%choose%"=="y" goto :Start
del _guess*.*


[ Last edited by qzwqzw on 2007 - 8 - 1 at 10:19 AM ]
Floor 5 Posted 2007-07-31 18:04 ·  马来西亚
新手上路
Credits 18
Posts 9
Joined 2007-07-25 18:11
19-year member
UID 94020
Gender Male
Status Offline
Good game......
Floor 6 Posted 2007-07-31 18:52 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
A guessing number game written a long time ago. The gameplay is similar.
It was a bit messy when I wrote it back then, and I'm too lazy to fix it.


@echo off&setlocal enabledelayedexpansion&color fc
:num
set/a a=%random%%%10
for %%a in (%num%) do if %a% equ %%a goto num
set num=%num% %a%
if "%num:~7%"=="" goto num
set z=%num: =%
set t=11

:start
echo\&echo/&echo=
set n=0&set a=0&set b=0&set aa=0&set aa1=
set/a t-=1
if %t%==0 goto fail
echo.你有%t%次机会.
set /p aa=请输入4位不同的数字:
if %aa%==%z% goto true
if %aa% leq 9999 if %aa% geq 0123 goto in
echo 输入错误,请输入4位不同的数字.&goto start

:in
set aa1=%aa1% %aa:~0,1%
set aa=%aa:~1%
if not defined aa goto next
for %%a in (%aa1%) do if %aa:~0,1% equ %%a echo.有重复数字,请重输入.&&goto start
goto in

:next
for %%a in (%aa1%) do (
call set str=%%z:~!n!,1%%
set/a n+=1
if %%a==!str! set/a a+=1
for %%b in (%num%) do (
if %%a==%%b set/a b+=1 ))
set/a b-=a
echo/数字位置都正确的有%a%个!
echo/数字对了位置不对的有%b%个!
goto :start

:fail
echo 你失败了
echo 答案是:%z%
echo.
pause>nul&goto :eof

:true
echo/&echo/&echo\
echo 你真厉害,猜对了!!!
echo.
pause>nul

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 7 Posted 2007-08-01 10:20 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Now release an interactive guessing version


:: Number Guessing Game (Interactive Guessing Version) : qzwqzw : 2007-07-31
:: From: http://www.cn-dos.net/forum/
@echo off & setlocal EnableDelayedExpansion

mode con cols=40 lines=20
:Start - Game Start
setlocal & cls
set iCount=0
echo.
echo The number guessing game starts now, you set the number and I'll guess
echo.

:GuessLoop - Game Main Loop
set /a iCount+=1
set /a iLimit=9-iCount
call :Guess iGuess %iGuess% %iNumA% %iNumB% %iCount%
if "%iGuess%"=="" set "End=Failure" & goto :End
echo. The %iCount%th guess (there are %iLimit% times left) is: %iGuess%
set "iNumA=x" & set "iNumB=y"
call :UserJudge iNumA iNumB
set "strGuess=%strGuess% %iGuess%:%iNumA%A%iNumB%B"
if "%iNumB%"=="y" set "End=Aborted" & goto :End
if "%iNumA%"=="4" set "End=Success" & goto :End
if "%iCount%"=="9" set "End=Failure" & goto :End
goto :GuessLoop

:Guess - Computer Guessing the Target Number
if not "%4"=="" goto :GuessFilter
:GuessFirst - First Guessing Directly Generate a Number Table Containing All Non-repeating Four-digit Numbers
setlocal
if exist guess.tbl (
copy guess.tbl _guess.tbl > nul
set _count=5040
goto :GuessSelect
)
if exist _guess.tbl del _guess.tbl
for /l %%i in (0,1,9) do (
for /l %%j in (0,1,9) do (
if not %%i==%%j for /l %%k in (0,1,9) do (
if not %%i==%%k if not %%j==%%k for /l %%l in (0,1,9) do (
if not %%i==%%l if not %%j==%%l if not %%k==%%l (
echo %%i%%j%%k%%l>>_guess.tbl
set /a _count+=1
)
)
)
)
)
copy _guess.tbl guess.tbl > nul
goto :GuessSelect

:GuessFilter - Use findstr to Simply and Quickly Filter the Number Table According to Similar Values
set %1=
setlocal
set _gss=%2
set /a _n=%3+%4
if "%_n%"=="0" set pattern="^*$"
if "%_n%"=="1" set pattern="^**$"
if "%_n%"=="2" set pattern="^***$"
if "%_n%"=="3" set pattern="^**$"
if "%_n%"=="4" set pattern="^*$"
findstr /r %pattern% _guess.tbl > _guess.tmp
copy _guess.tmp _guess.tbl > nul
for /f %%f in ('find /v /c "" ^<_guess.tbl') do set _count=%%f
if "%_count%"=="0" echo ERR4: Retrieval failed, please check the specified similarity value & goto :eof

set _isJudge=true
if 1%5 lss 13 set _isJudge=false
if 1%_n% equ 10 set _isJudge=false
if 1%3 equ 14 set _isJudge=true
if 1%_count% gtr 1500 set _isJudge=false
if 1%_count% lss 150 set _isJudge=true
if "%_isJudge%"=="false" goto :GuessSelect

:GuessJudge - Judge the Number Table One by One According to Similar Values and Filter Out the Completely Matching Number Table
if exist _guess.tbl del _guess.tbl
set _nf=%2
for /f %%f in (_guess.tmp) do (
set _ng=%%f
set /a _numa=0, _numb=0
for /l %%i in (0,1,3) do (
set _nfb=!_nf:~%%i,1!
set _ngb=!_ng:~%%i,1!
if "!_nfb!"=="!_ngb!" (
set /a _numa+=1
) else (
echo.!_ng!|find "!_nfb!">nul && set /a _numb+=1
)
)
if 1%3==1!_numa! if 1%4==1!_numb! (
echo %%f>>_guess.tbl
)
)
if not exist _guess.tbl echo ERR5: Retrieval failed, please check the specified similarity value & goto :eof

:GuessSelect - Randomly Select from the Filtered Number Sequence
copy _guess.tbl _guess.sel > nul
for /f %%f in ('find /v /c "" ^<_guess.sel') do set _count=%%f
set /a rn=%random% %% _count + 1
for /f "tokens=2 delims=] " %%f in (
'find /v /n "" _guess.sel^|find ""'
) do (
set _guess=%%f
)
copy _guess.tbl guess_%5.txt > nul
endlocal & set %1=%_guess%
goto :eof

:UserJudge - Accept and Save the Similarity Value Entered by the User
if "%1"=="" goto :eof
set _judge=
set /p _judge= Please specify the similarity value with two digits (press A to give up):
if /i "%_judge%"=="a" goto :eof
set /a _judge=1%_judge% 2>nul || (echo ERR6: Invalid number, please enter again & goto :UserJudge)
if %_judge% lss 100 echo ERR7: Invalid number, please enter again & goto :UserJudge
if %_judge% gtr 140 echo ERR8: Invalid number, please enter again & goto :UserJudge
set "%1=%_judge:~1,1%"
set "%2=%_judge:~2,1%"
goto :eof

:End - Post-processing After Single Game End
echo.
set /p choose= Guessed to%strGuess:~-9,4%%End%,Restart? (Y/N)
echo %End% at: %strGuess% >> guess.log
set strGuess=
endlocal & if /i "%choose%"=="y" goto :Start
del _guess*.*
Floor 8 Posted 2007-08-01 18:46 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
tao0610
It is best to allow users to enter repeated numbers. Although the target number is always non - repeated, using test numbers with repeated digits may facilitate determining the target number.
Floor 9 Posted 2007-08-01 23:56 ·  中国 北京 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
Just remove the part about judging repeated input. Actually, within 10 times, judging the target number is not that difficult.

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 10 Posted 2007-08-02 18:24 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Now release a fully functional Release 1 version


:: Guess the number game : Full function version Release 1 : qzwqzw : 2007-08-02
:: Source: http://www.cn-dos.net/forum/viewthread.php?tid=32554
@echo off & setlocal EnableDelayedExpansion
set iCount=0
set iLimit=9
set sGuess=-:----:----

:Start
mode con cols=40 lines=25 & cls
echo.
echo. Introduction to the number guessing game
echo.
echo. The number selector randomly selects a four-digit number
echo. Each digit in the four-digit number is unique and not repeated
echo.
echo. The computer guesses this four-digit number
echo. The first time you can randomly guess a four-digit number
echo.
echo. The number selector returns a similarity value according to the computer's guess number
echo. The similarity value consists of 2 numbers, similar to: xAyB
echo. xA means that there are x numbers in the guessed number that exist and are in the correct position
echo. yB means that there are y numbers that exist but are in the wrong position
echo.
echo. For example: Suppose the number selected by the number selector is 1234
echo. If the computer guesses 1364, the number selector will return 2A1B
echo. The 2A here refers to the numbers 1 and 4, and 1B refers to the number 3
echo.
echo. The computer continues to guess according to the similarity value returned by the number selector
echo. The number selector returns the similarity value according to the computer's guessed number
echo. This cycle continues until the computer guesses the real number
echo. Or the computer gives up
echo.
pause

:ModeSelect
cls
echo.
echo. The following game modes are provided in this game:
echo.
echo. U. I choose the number and you guess
echo. I. You choose the number and I guess
echo.
echo. A. I choose the number and I guess
echo. M. You choose the number and you guess
echo.
echo. Q. Exit the game
echo.
echo.
set cMode=
set /p cMode= Please choose (U/I/A/M/Q):
if /i "%cMode%"=="U" goto :UGuess
if /i "%cMode%"=="I" goto :IGuess
if /i "%cMode%"=="A" goto :AGuess
if /i "%cMode%"=="M" goto :MGuess
if /i "%cMode%"=="Q" goto :eof
goto :ModeSelect

:UGuess - User mode starts
setlocal
set sMode=User
call :Generate iDest
:UGLoop
cls
echo.
echo. The game is now starting, I choose the number and you guess
echo.
echo. Please enter the 4-digit number you guess directly at the cursor
echo. I will display the similarity value of the guessed number below
echo. Press Q at any time to abort this round of guessing game
echo.
echo. Number of times Guessed number Similarity value
for %%s in (%sGuess%) do (
for /f "tokens=1,2,3 delims=:" %%i in ("%%s") do (
echo. %%i %%j %%k
)
)
echo. - ---- ----
echo.
if "%iNumA%"=="4" set End=Success& goto :End
if "%iCount%"=="9" set End=Failure& goto :End
set iGuess=
set /a iCount+=1
set /a iLimit-=1
call :UInput iGuess %iCount% %iLimit%
if "%iGuess%"=="" set End=Abort& set /a iCount-=1 & goto :End
call :IJudge iNumA iNumB %iGuess% %iDest%
set sGuess=%sGuess% %iCount%:%iGuess%:%iNumA%A%iNumB%B
goto :UGLoop

:IGuess - Computer mode starts
setlocal & cls
set sMode=Computer
echo.
echo.
set iDest=XXXX
:IGLoop
if "%iNumB%"=="y" set End=Abort& goto :End
if "%iNumA%"=="4" set End=Success& goto :End
if "%iCount%"=="9" set End=Failure& goto :End
cls
echo.
echo. The game is now starting, you choose the number and I guess
echo.
echo. First, I will display the number I guessed below
echo. Please enter a 2-digit number at the cursor to determine its similarity value
echo. Press Q at any time to abort this round of guessing game
echo.
echo. Number of times Guessed number Similarity value
for %%s in (%sGuess%) do (
for /f "tokens=1,2,3 delims=:" %%i in ("%%s") do (
echo. %%i %%j %%k
)
)
echo. - ---- ----
echo.
call :IGuess iGuess %iGuess% %iNumA% %iNumB% %iCount%
if "%iGuess%"=="" set End=Failure& goto :End
set /a iCount+=1
set /a iLimit=9-iCount
set "iNumA=x" & set "iNumB=y"
call :UJudge iNumA iNumB "The %iCount%th guess (there are %iLimit% times left): %iGuess%"
set sGuess=%sGuess% %iCount%:%iGuess%:%iNumA%A%iNumB%B
goto :IGLoop

:AGuess - Automatic mode starts
setlocal & cls
set sMode=Automatic
echo.
echo. The game is now starting, I choose the number and I guess
echo.
call :Generate iDest
echo. The target number I guessed is now set to %iDest%
echo. I will display the guessed number and similarity value below respectively
echo.
echo. Number of times Guessed number Similarity value
echo. - ---- ----
:AGLoop
if "%iNumA%"=="4" set End=Success& goto :End
if "%iCount%"=="9" set End=Failure& goto :End
set /a iCount+=1
call :IGuess iGuess %iGuess% %iNumA% %iNumB% %iCount%
if "%iGuess%"=="" set End=Failure& goto :End
call :IJudge iNumA iNumB %iGuess% %iDest%
echo. %iCount% %iGuess% %iNumA%A%iNumB%B
set sGuess=%sGuess% %iCount%:%iGuess%:%iNumA%A%iNumB%B
goto :AGLoop

:MGuess - Manual mode starts
setlocal & cls
set sMode=Manual
cls
set iDest=
echo.
set /p iDest= Please set the target number to guess:
:MGLoop
cls
echo.
echo. The game is now starting, you choose the number and you guess
echo.
echo. The target number you set is %iDest%
echo. Please enter the 4-digit number you guess directly at the cursor
echo. I will display the guessed number and similarity value below
echo. Press Q at any time to abort this round of guessing game
echo.
echo. Number of times Guessed number Similarity value
for %%s in (%sGuess%) do (
for /f "tokens=1,2,3 delims=:" %%i in ("%%s") do (
echo. %%i %%j %%k
)
)
echo. - ---- ----
echo.
if "%iNumA%"=="4" set End=Success& goto :End
if "%iCount%"=="9" set End=Failure& goto :End
set iGuess=
set /a iCount+=1
set /a iLimit-=1
call :UInput iGuess %iCount% %iLimit%
if "%iGuess%"=="" set End=Abort& goto :End
call :IJudge iNumA iNumB %iGuess% %iDest%
set sGuess=%sGuess% %iCount%:%iGuess%:%iNumA%A%iNumB%B
goto MGLoop

:Generate - Use random exchange algorithm to generate a target number composed of four non-repeating random numbers
if "%1"=="" goto :eof
setlocal
for /l %%i in (0,1,9) do set n%%i=%%i
set i=0
:GenLoop
set r=%random:~-1,1%
call set tmp=%%n%i%%%
call set n%i%=%%n%r%%%
call set n%r%=%tmp%
set /a i+=1
if %i% neq 4 goto GenLoop
endlocal & set "%1=%n0%%n1%%n2%%n3%" & goto :eof

:UInput - Accept and save the guessed number entered by the user
if "%1"=="" goto :eof
set _input=
set /p _input= The %2th guess (there are %3 times left):
if /i "%_input%"=="Q" goto :eof
set /a _input=1%_input% 2>nul
if errorlevel 9167 echo ERR1: Invalid number, please re-enter & goto :UInput
if %_input% lss 10000 echo ERR2: Invalid number, please re-enter & goto :UInput
if %_input% gtr 19999 echo ERR3: Invalid number, please re-enter & goto :UInput
set "%1=%_input:~1,4%"
goto :eof

:IJudge - Judge the accuracy of the guessed number
setlocal EnableDelayedExpansion
set /a _numa=0, _numb=0
if not "%4"=="" set iGuess=%3&set iDest=%4
for /l %%i in (0,1,3) do (
if "!iDest:~%%i,1!"=="!iGuess:~%%i,1!" (
set /a _numa+=1
) else (
echo.%iGuess%|find "!iDest:~%%i,1!">nul && set /a _numb+=1
)
)
endlocal & set %1=%_numa%&set %2=%_numb%
goto :eof

:IGuess - I guess the target number
if not "%4"=="" goto :IGFilter
:IGFirst - The first guess directly generates a number table containing all four-digit numbers that are not repeated
setlocal
if exist guess.tbl (
copy guess.tbl _guess.tbl > nul
set _count=5040
goto :IGSelect
)
if exist _guess.tbl del _guess.tbl
for /l %%i in (0,1,9) do (
for /l %%j in (0,1,9) do (
if not %%i==%%j for /l %%k in (0,1,9) do (
if not %%i==%%k if not %%j==%%k for /l %%l in (0,1,9) do (
if not %%i==%%l if not %%j==%%l if not %%k==%%l (
echo %%i%%j%%k%%l>>_guess.tbl
set /a _count+=1
)
)
)
)
)
copy _guess.tbl guess.tbl > nul
goto :IGSelect

:IGFilter - Use findstr to simply and quickly filter the number table according to the similarity value
set %1=
setlocal
set _gss=%2
set /a _ab=%3+%4
if "%_ab%"=="0" set pattern="^*$"
if "%_ab%"=="1" set pattern="^**$"
if "%_ab%"=="2" set pattern="^***$"
if "%_ab%"=="3" set pattern="^**$"
if "%_ab%"=="4" set pattern="^*$"
findstr /r %pattern% _guess.tbl > _guess.tmp
for /f %%f in ('find /v /c "" ^<_guess.tmp') do set _count=%%f
if "%_count%"=="0" echo ERR4: Guessing failed, the entered similarity values are contradictory & goto :eof
if "%_ab%"=="2" (
findstr /r "^**$" _guess.tbl > _guess.tm2
for /f %%f in ('find /v /c "" ^<_guess.tm2') do set _count=%%f
if "!_count!"=="0" (
set _count=%_count%
del _guess.tm2
)
)
copy _guess.tmp _guess.tbl > nul

set _isJudge=true
:: if 1%3 equ 10 set _isJudge=false
if 1%5 lss 13 set "_isJudge=false" %- Do not make a one-by-one judgment for the first two screenings -%
if 1%_ab% equ 10 set "_isJudge=false" %- Do not make a one-by-one judgment when A+B equals 0 -%
if 1%3 equ 14 set "_isJudge=true" %- Make a one-by-one judgment when A equals 4 -%
if 1%_count% gtr 1500 set "_isJudge=false" %- Do not make a one-by-one judgment when the table is greater than 1500 -%
if 1%_count% lss 150 set "_isJudge=true" %- Make a one-by-one judgment when the table is less than 150 -%
if "%_isJudge%"=="false" goto :IGSelect

:IGJudge - Make a one-by-one judgment on the number table according to the similarity value, and filter out the completely conforming number table
del _guess.tbl
set _nf=%2
set _count=
set /p _tmp= Guessing data, please wait a moment…<nul
for /f %%f in (_guess.tmp) do (
set _ng=%%f
set /a _numa=0, _numb=0
for /l %%i in (0,1,3) do (
if "!_nf:~%%i,1!"=="!_ng:~%%i,1!" (
set /a _numa+=1
) else (
echo.!_ng!|find "!_nf:~%%i,1!">nul && set /a _numb+=1
)
)
if 1%3==1!_numa! if 1%4==1!_numb! (
echo %%f>>_guess.tbl
set /a _count+=1
)
)
set /p _tmp=<nul
if not exist _guess.tbl echo ERR5: Guessing failed, the entered similarity values are contradictory & goto :eof

:IGSelect - Randomly select one from the filtered number table as a new guessed number
copy _guess.tbl guess_%5.txt > nul
copy _guess.tbl _guess.sel > nul
if exist _guess.tm2 (
copy _guess.tm2 _guess.sel > nul
del _guess.tm2
)
set /a rn=%random% %% _count + 1
for /f "tokens=2 delims=] " %%f in (
'find /v /n "" _guess.sel^|find ""'
) do (
set _guess=%%f
)
endlocal & set %1=%_guess%
goto :eof

:UJudge - Accept and save the judgment value entered by the user
if "%1"=="" goto :eof
set _judge=
set /p _judge= %~3, please judge:
if /i "%_judge%"=="q" goto :eof
set /a _judge=1%_judge% 2>nul
if errorlevel 9167 echo ERR6: Invalid number, please re-enter & goto :UJudge
if %_judge% lss 100 echo ERR7: Invalid number, please re-enter & goto :UJudge
if %_judge% gtr 140 echo ERR8: Invalid number, please re-enter & goto :UJudge
set /a _ab=10+%_judge:~1,1%+%_judge:~2,1%
if %_ab% gtr 14 echo ERR9: Invalid number, please re-enter & goto :UJudge
set "%1=%_judge:~1,1%"
set "%2=%_judge:~2,1%"
goto :eof

:End - Post-processing after the end of a single game
set Report=%sMode%guess%iDest%,The %iCount%th guess to%sGuess:~-9,4% when%End%
echo.
echo. %Report%
set choose=
set /p choose= Do you want to start over (Y/N)
echo %Report%。The steps are: %sGuess:~12% >>guess.log
endlocal & if /i "%choose%"=="y" goto :%cMode%Guess
del _guess*.*
goto :ModeSelect
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
yovie +2 2007-09-26 14:32
Floor 11 Posted 2007-08-02 18:25 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
tao0610
If duplicate numbers are allowed, your :next also needs to be modified; otherwise, the B value may be repeatedly accumulated
Floor 12 Posted 2007-09-25 14:09 ·  中国 广东 广州 天河区 电信
初级用户
★★
Credits 172
Posts 54
Joined 2007-01-02 10:38
19-year member
UID 75260
Gender Male
Status Offline
to qzwqzw:

Hehe``July 30th, probably also when I was researching this game, didn't expect that you were also researching this back then~
Tested your release 1 version
All four modes have issues, specifically see my screenshots:









[ Last edited by dosmania on 2007-9-25 at 02:11 PM ]
Floor 13 Posted 2007-09-25 14:45 ·  中国 北京 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Why take screenshots of cmd output? Just copy and paste the text directly :)

The error you encountered can't be reproduced when I debug it. It might be due to differences in the running environment. I heard you are quite knowledgeable about this, so please help track and debug the code to identify the problem point, which will help us solve the problem together. Thanks!

[ Last edited by qzwqzw on 2007-9-26 at 10:52 PM ]
Floor 14 Posted 2007-09-26 14:13 ·  中国 广东 广州 白云区 电信
初级用户
★★
Credits 172
Posts 54
Joined 2007-01-02 10:38
19-year member
UID 75260
Gender Male
Status Offline
You have the chance to explain, but unfortunately I don't have the chance to apologize.
I have already lost the chance to become a member of the internal group of this forum.
----------------------------------------------------------
If things are made clear, misunderstandings can be avoided to a large extent. It was a misunderstanding...
You should know that it took me about 10 minutes to test your code...
What I got was "not understanding" from others. Everyone would get angry...
------------------------------------------------------------
If you are interested in the code of that game, you can browse my post again...
------------------------------------------------------------
Finally, I apologize...

[ Last edited by dosmania on 2007-9-26 at 08:56 PM ]
Floor 15 Posted 2007-09-26 21:28 ·  中国 广东 广州 白云区 电信
初级用户
★★
Credits 172
Posts 54
Joined 2007-01-02 10:38
19-year member
UID 75260
Gender Male
Status Offline
You have the chance to explain, but unfortunately I don't have the chance to apologize.
I have lost the chance to become a member of the internal group of this forum because of this.
----------------------------------------------------------
If things are made clear, misunderstandings can be avoided to a large extent, it's a misunderstanding...
You should know that it took me about 10 minutes to test your code...
What I got was "not understanding" from others, anyone would get angry...
------------------------------------------------------------
If you are interested in the code of that game, take another look at my post...
------------------------------------------------------------
Finally, I apologize...
Forum Jump: