### 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.
[ Last edited by qzwqzw on 2007-9-25 at 11:19 AM ]
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 ]
