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 22:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Guess the number game [game's over] View 2,529 Replies 16
Original Poster Posted 2007-09-21 15:25 ·  中国 广东 广州 天河区 电信
初级用户
★★
Credits 172
Posts 54
Joined 2007-01-02 10:38
19-year member
UID 75260
Gender Male
Status Offline
Write a number guessing game. I don't know if anyone has played it. There is this game on the home phone, and I've played it many times. I'm also writing one myself, 7788, not perfected yet. I want to see if everyone has better ideas.

In the forum, those who feel there are no challenging experts can come out and discuss. Thanks.

Rules:

1. The game will randomly obtain a four-digit number without repeated digits from 0-9 at the beginning (the first digit can be 0)

2. The player first makes a guess, entering a four-digit number without repeated digits from 0-9

3. Compare the number obtained at the beginning of the game with the number entered by the player:

If one digit has the exact same number and position, set flag A1

If one digit has a matching number but wrong position, set flag B1

If the number and position are both mismatched, set flag B0

The exact match of number and position only sets flag A, regardless of B

For example: The answer is 4690. If I guess 0679, then the prompt is A1B2

If the prompt is A0B0, it means the guessed number does not appear at all in the answer

If the prompt is A0B4, it means the guessed number matches exactly but all positions are wrong

A4B0 is the correct answer.

4. Only 10 chances

General idea:

* Must verify the random answer

Verify if the randomly obtained number is four digits, and if each digit has repetition

* Must verify the player's input specification

Verify if the player's input is pure digits, if there is repetition, if it is 4 digits, otherwise give corresponding prompt and jump back to re-enter

* Record historical records

What the user entered for the first time, what the prompt was, must be displayed for reference when guessing the second time

* Record the number of uses

How many times have been guessed, how many times are left, displayed together with the historical records

* Must have a chance to play again

After the player uses up 10 times and hasn't guessed it, there must be a chance to play again, or if guessed correctly at once, there must be a chance to play again

-------------------------------------------------------------------------------------------
The final answer is in posts 7, 9, 11, 16
Close the post!

[ Last edited by dosmania on 2007-9-26 at 09:32 PM ]
Floor 2 Posted 2007-09-24 15:04 ·  中国 广东 广州 天河区 电信
初级用户
★★
Credits 172
Posts 54
Joined 2007-01-02 10:38
19-year member
UID 75260
Gender Male
Status Offline
I'm in a cold sweat``Why is no one researching it``
Floor 3 Posted 2007-09-24 16:55 ·  中国 浙江 杭州 教育网
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
This is quite interesting;
Just been quite busy these days and haven't had time;

During the National Day holiday, I will definitely write it out and post it;
Floor 4 Posted 2007-09-24 16:57 ·  中国 甘肃 天水 秦州区 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
There was someone who wrote a number guessing game a long time ago, and it can be modified a bit on that basis.
Floor 5 Posted 2007-09-24 21:43 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
I wrote a C language version before. I'll convert it to a batch file when I have time.
Floor 6 Posted 2007-09-25 03:37 ·  中国 广东 广州 天河区 电信
初级用户
★★
Credits 172
Posts 54
Joined 2007-01-02 10:38
19-year member
UID 75260
Gender Male
Status Offline
re bjsh:
Looking forward to the moderator's ideas yo````

re vkill:

There are people who have written it, but it's completely different, a lot different. That game is quite fake
The one I'm talking about is a game that really exists in my home fixed-line phone.
Some necessary verifications I mentioned are also distinguished from the one you mentioned.

re HAT:

Post the code here
Floor 7 Posted 2007-09-25 11:20 ·  中国 北京 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
The landlord is talking about this game, right?
http://www.cn-dos.net/forum/viewthread.php?tid=32554
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
dosmania +2 2007-09-26 21:33
Floor 8 Posted 2007-09-25 14:15 ·  中国 广东 广州 天河区 电信
初级用户
★★
Credits 172
Posts 54
Joined 2007-01-02 10:38
19-year member
UID 75260
Gender Male
Status Offline
re qzwqzw:

Read your post. "The anti-guessing is too slow, waited for a long time."

In fact, the actual existing number guessing game has 10 guesses, not 8.

I tested your release 1 version, and the problems I encountered are also posted in your post. Go check it yourself.

It feels like your code is quite a lot...
Floor 9 Posted 2007-09-25 14:37 ·  中国 北京 电信
银牌会员
★★★
Credits 2,098
Posts 566
Joined 2007-09-11 07:27
18-year member
UID 97070
Gender Male
Status Offline
```batch
@echo off
setlocal enabledelayedexpansion
::::::::::::::::::::::::::: Guess the Number Game :::::::::::::::::::::::::::
:::::::::::::::::::::::::::{s11ss 2007-9-25}:::::::::::::::::::::::::::
:Begin
set/a counter=1
:GetStr
echo Please enter a four-digit number :
set/p str=
if "%str%"=="" goto :GetStr
call set one=%%str:~3,1%%
if "%one%" equ "" (echo The number must be four digits!& goto :GetStr)
call set one=%%str:~4,1%%
if not "%one%" equ "" (echo The number must be four digits!& goto :GetStr)
set/a len=0
:IsAllNum
call set word=%%str:~%len%,1%%
if not "%word%"=="" (
set isnum=False
for /l %%a in (0,1,9) do (
if "%word%"=="%%a" (
set/a len+=1
set isnum=True
goto :IsAllNum
)
)
)
if "%isnum%"=="False" (echo How can there be non-numeric characters? & goto :GetStr)
set state=chkinput
call :CheckSameNum n %str%
if not !counter! equ 1 goto :CheckAnswer
goto :CreateRandom
:CheckSameNum
set s=%2
for /l %%a in (0,1,3) do call set %1%%a=%%s:~%%a,1%%
set/a i=0
:ci
set/a j=!i!+1
:cj
if !%1%j%! equ !%1%i%! (
if !state! equ chkinput (
echo All digits must be different!& goto :GetStr
) else (goto :CreateRandom)
)
set/a j+=1
if not !j! gtr 3 goto :cj
set/a i+=1
if not !i! gtr 2 goto :ci
goto :eof
:CreateRandom
set/a answer=%random%
call set one=%%answer:~3,1%%
if "%one%" equ "" (goto :CreateRandom)
call set one=%%answer:~4,1%%
if not "%one%" equ "" (goto :CreateRandom)
set state=chkrandom
call :CheckSameNum y %answer%
::The next line can display the answer:
::echo %answer%
:CheckAnswer
set/a aa=0,bb=0
if not !counter! equ 11 (
for /l %%a in (0,1,3) do (
for /l %%i in (0,1,3) do (
if !n%%i! equ !y%%a! (
if %%i equ %%a (
set/a aa+=1
) else (set/a bb+=1)
)
)
)
set r%counter%=A!aa!B!bb!
set str%counter%=!str!
for /l %%a in (1,1,!counter!) do echo Guess %%a:!str%%a!,Result:!r%%a!
set/a remain=10-!counter!
echo Guessed !counter! times, remaining !remain! times.
set/a counter+=1
if not !aa! equ 4 (
goto :GetStr
) else (
echo Got it, the answer is !str%counter%!,Happy Mid-Autumn Festival!
goto :Replay
)
)
echo Have guessed 10 times!
:Replay
set/p q=Do you want to play again?(y/n)
if not "%q%" equ "y" (
if not "%q%" equ "Y" (
echo Press any key to exit...
pause>nul
goto :eof
)
)
goto :Begin
```

[ Last edited by s11ss on 2007-9-25 at 02:44 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
dosmania +2 2007-09-26 21:34
Floor 10 Posted 2007-09-25 14:50 ·  中国 北京 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
dosmania:
I admit that the guessing algorithm was indeed a bit shoddy
After all, it was just an R1 in my spare time

Unfortunately, no one was interested in it at that time
No discussion atmosphere was formed
Later, I also lost interest in this aspect

If you have a new algorithm, just post it out
Maybe it will rekindle my interest
------------------------------------------------------
As for the issue of guessing 8 times or 10 times
I don't think it's a problem
I originally set it to 10 times
Later, it was changed to 8 times because of the display layout and log reasons

To be honest
Among the games of this kind I have come into contact with
Those for game consoles and TVs have no limit on the number of times
For video recorders, it's limited to 8 times, and it doesn't use numbers 9 and 0
There are many varieties for computers

An excellent guessing algorithm
The exhaustive algorithm should be the one with the least number of times
But it's not the most efficient
The average number of guessing times should be less than 6 times
Setting 8 times can also be considered medium difficulty
------------------------------------------------------

"Feels like you have a lot of code``` "
I don't understand, can you explain it?

[ Last edited by qzwqzw on 2007-9-25 at 03:05 PM ]
Floor 11 Posted 2007-09-25 15:56 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
I can't write batch processing, send a VBS one


dim a,b,cs,s
msgbox "Game method: " _
& vbcrlf & " The computer randomly generates a four-digit decimal number (the first one can be 0), guess 10 times." _
& vbcrlf & " Each time it returns AaBb" _
& vbcrlf & " A means the number is correct and the position is correct" _
& vbcrlf & " B means the number is correct but the position is incorrect" _
& vbcrlf & " For example: Suppose the number to guess is 1234" _
& vbcrlf & " If the player guesses 0134, it returns A2B1 (3, 4 are A, 1 is B)",4096+32,"Guess the number game"
do
a=0:b=0:cs=0:sm=""
while not pd(s)
randomize
s1=Int(10 * Rnd)
s2=Int(10 * Rnd)
s3=Int(10 * Rnd)
s4=Int(10 * Rnd)
s=s1 & s2 & s3 & s4
wend
'msgbox s
do until cs=10
do
n=inputbox(sm & "Please enter the guess number: (there are " & 10-cs & " chances left)","Guess the number game")
if n=false then wscript.quit
if len(n)<>4 then
msgbox "Incorrect number of digits, please enter again!",16+4096,"Error"
elseif pdsz(n)=false then
msgbox "The input is not a number, please enter again!",16+4096,"Error"
elseif not pd(n) then
msgbox "No duplicate numbers, please enter again!",16+4096,"Error"
else
cs=cs+1:exit do
end if
loop
call jg(n)
if cs=10 and n<>s then msgbox "The computer's random number is: " & s & vbcrlf & "Unfortunately, it wasn't guessed, keep working hard!",4096,"Result"
if n=s then msgbox "The computer's random number is: " & s & vbcrlf & "Guess for the " & cs & "th time: " & n & vbcrlf & "Result: " & "A" & a & "B" & b,4096+48,"Congratulations, you guessed it right!":exit do
sm=sm & "Guess for the " & cs & "th time: " & n & vbcrlf & "Result: " & "A" & a & "B" & b & vbcrlf & vbcrlf
loop
s=""
loop until msgbox("Do you want to do it again?",vbyesno,"Game over")=vbno
function pd(a)
my1=mid(a,1,1)
my2=mid(a,2,1)
my3=mid(a,3,1)
my4=mid(a,4,1)
pd=my1<>my2 and my1<>my3 and my1<>my4 and my2<>my3 and my2<>my4 and my3<>my4
end function

sub jg(r)
a=0:b=0
for i=1 to 4
m=mid(r,i,1)
h=mid(s,i,1)
if m=h then
a=a+1
else
if instr(s,m)<>0 then b=b+1
end if
next
end sub

function pdsz(a)
l=len(a)
for i=1 to l
if isNumeric(mid(a,i,1)) then
pdsz=true
else
pdsz=false:exit function
end if
next
end function


[ Last edited by jmz573515 on 2007-9-25 at 04:09 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
dosmania +2 2007-09-26 21:34
Floor 12 Posted 2007-09-26 14:31 ·  中国 广东 广州 白云区 电信
初级用户
★★
Credits 172
Posts 54
Joined 2007-01-02 10:38
19-year member
UID 75260
Gender Male
Status Offline
Hehe``jmz573515 is amazing, VBS interactivity is N good
if len(n)<>4 Oh``simply can't compare嘛```VBS this one solves the digit problem``
I wonder if jmz573515 has considered the repetition possibility of s1 s2 s3 s4```?
Floor 13 Posted 2007-09-26 14:53 ·  中国 广东 广州 白云区 电信
初级用户
★★
Credits 172
Posts 54
Joined 2007-01-02 10:38
19-year member
UID 75260
Gender Male
Status Offline
re s11ss:
Your code also fully meets the requirements```I don't know which ones you verified for random number repetition``
Floor 14 Posted 2007-09-26 15:31 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
dosmania:
if len(n)<>4 靠``根本不能比嘛```VBS这个就搞定了位数``
I don't know what you mean by "can't compare", I think it can be limited to 4 digits??

I don't know if jmz573515 has considered the possibility of repetition of s1 s2 s3 s4...?

What do you mean by the possibility of repetition? (Do you mean how likely it is? Or whether it is possible to repeat?)
Here I only considered the possibility of repetition, so I made a judgment. If there is repetition, I generate a random number again. I didn't consider how likely it is, maybe it's not efficient...)
Floor 15 Posted 2007-09-26 16:52 ·  中国 北京 电信
银牌会员
★★★
Credits 2,098
Posts 566
Joined 2007-09-11 07:27
18-year member
UID 97070
Gender Male
Status Offline
Originally posted by dosmania at 2007-9-26 02:53 PM:
re s11ss:
Your code also fully meets the requirements```I don't know which ones are the verifications for random number repetition for you``


Random number repetition?
I don't understand your specific meaning.

Anyway, my code is to make the 4-digit numbers of the random number not repeat. As for whether the random numbers repeat among themselves, I didn't control it, but there doesn't seem to be a requirement in the question, right?
Forum Jump: