|
dosmania
初级用户
 
积分 172
发帖 54
注册 2007-1-2
状态 离线
|
『楼 主』:
猜数字游戏[game's over]
使用 LLM 解释/回答一下
写个猜数字的游戏,不知有没有人玩过,家里电话有这游戏,玩了好多次了
我自己也在写,7788,没完善好,想看下大家有没更好的思路。
在论坛里感觉没挑战的高手都可以出来讨论下了,谢谢啊``
规则:
1. 游戏开始会随机获取四位不重复的0-9的数字(第一位可以为0)
2. 玩家首先进行猜测,输入四位不重复的0-9的数字
3. 将游戏一开始获取的数字跟玩家输入的数字进行对比:
假如有一位的数字和位置完全匹配的话,设置标志 A1
假如有一位的数字匹配,但是位置不匹配的话,设置标志B1
假如数字和位置均不匹配的话,设置标志B0
数字和位置完全匹配只设置标志A,跟B无关
例如: 答案是 4690 若我猜 0679 则 提示为 A1B2
若提示 A0B0 则表示所猜的数字在答案中完全没出现过
若提示 A0B4 则表示所猜的数字完全匹配,但是位置全错了
A4B0 就是正确答案了。
4. 只有10机会
大概思路:
* 必须验证随机答案
验证随机获取的数字是否四位,每一位是否有重复
* 必须验证玩家的输入规范
验证玩家输入的是否是纯数字,是否有重复,是否是4位,否则给出相应提示然后跳回重新输入
* 记录历史记录
第一次用户输入了什么,得到的提示是什么,必须在第二次猜测的时候显示出来供参考
* 记录使用次数
猜了多少次,剩下多少次,跟历史记录一起显示
* 必须有重玩的机会
玩家使用完10次之后还没猜出来必须要有重玩机会,或者一次就猜出来了,也要有重玩机会
-------------------------------------------------------------------------------------------
最终答案见 7楼、9楼、11楼、16楼
结帖!
Last edited by dosmania on 2007-9-26 at 09:32 PM ]
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 ]
|
|
2007-9-21 15:25 |
|
|
dosmania
初级用户
 
积分 172
发帖 54
注册 2007-1-2
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
我狂汗啊``怎么没人研究啊``
I'm in a cold sweat``Why is no one researching it``
|
|
2007-9-24 15:04 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
这个挺有意思的;
只是这段时间比较忙没时间;
十一放假;一定写出来贴上来;
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;
|
|
2007-9-24 16:55 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
很早就有人写了个猜数字的游戏,在那个上面稍微修改下就可以的
There was someone who wrote a number guessing game a long time ago, and it can be modified a bit on that basis.
|
|
2007-9-24 16:57 |
|
|
HAT
版主
       
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
我以前写了个C语言版的
有时间把它转成批处理
I wrote a C language version before. I'll convert it to a batch file when I have time.
|
|
2007-9-24 21:43 |
|
|
dosmania
初级用户
 
积分 172
发帖 54
注册 2007-1-2
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
re bjsh:
期待版主的思路吖````
re vkill:
是有人写过,但截然不同,差很多呢,那游戏够假
我说的这个可是真实存在我家固定电话里的游戏。
我提到的一些必要的验证也是和你说的那个区别开来。
re HAT:
把代码贴上来
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
|
|
2007-9-25 03:37 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
|
2007-9-25 11:20 |
|
|
dosmania
初级用户
 
积分 172
发帖 54
注册 2007-1-2
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
re qzwqzw:
拜读了一下你的贴子``反猜解太慢了,等了N 久``
其实现实存在的那个猜数字游戏是猜10次,而不是8次。
我测试了你的release 1 版,出现的问题也贴到你贴子里了`自己去看``
感觉你的代码N 多```
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...
|
|
2007-9-25 14:15 |
|
|
s11ss
银牌会员
    
积分 2098
发帖 566
注册 2007-9-11
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
@echo off
setlocal enabledelayedexpansion
::::::::::::::::::::::::::: 猜数字游戏 :::::::::::::::::::::::::::
:::::::::::::::::::::::::::{s11ss 2007-9-25}:::::::::::::::::::::::::::
:Begin
set/a counter=1
:GetStr
echo 请输入一个四位数:
set/p str=
if "%str%"=="" goto :GetStr
call set one=%%str:~3,1%%
if "%one%" equ "" (echo 位数一定为四!& goto :GetStr)
call set one=%%str:~4,1%%
if not "%one%" equ "" (echo 位数一定为四!& 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 怎么会有非数字出现呢? & 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 各位数字要各不相同!& 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%
::下一句可以显示出答案:
::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 第%%a次猜测:!str%%a!,结果:!r%%a!
set/a remain=10-!counter!
echo 共猜了!counter!次,还剩!remain!次。
set/a counter+=1
if not !aa! equ 4 (
goto :GetStr
) else (
echo 答对了,答案就是!str%counter%!,中秋节快乐!
goto :Replay
)
)
echo 已经猜了10次!
:Replay
set/p q=要重玩么?(y/n)
if not "%q%" equ "y" (
if not "%q%" equ "Y" (
echo 按任意键退出...
pause>nul
goto :eof
)
)
goto :Begin
Last edited by s11ss on 2007-9-25 at 02:44 PM ]
```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 ]
|
|
2007-9-25 14:37 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
dosmania:
我承认那个猜解算法确实有些蹩脚
毕竟只是一个闲暇之余的R1而已
可惜当时没人对此感兴趣
没有形成讨论的气氛
后来我也失去了这方面的兴趣
你若有新的算法那就贴出来
也许会重新激发我的兴趣也不一定
------------------------------------------------------
至于猜8次还是猜10次问题
我认为那不是问题
最初我就设定了10次
后来因为显示版面和日志的原因改成了8次
真要说起来
我接触的此类游戏中
游戏机和电视机的是不限次数的
录像机是限制了8次,并且不使用数字9和0
电脑的就很多样了
一个优秀的猜解算法
穷举算法应该就是次数最少的算法
但不是效率最高的
平均猜解次数应该是小于6次吧
设定8次也算中级难度吧
------------------------------------------------------
“感觉你的代码N 多``` ”
不明白,可以解释一下吗
Last edited by qzwqzw on 2007-9-25 at 03:05 PM ]
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 ]
|
|
2007-9-25 14:50 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
批处理我是写不出来了,发一个VBS的吧
dim a,b,cs,s
msgbox "游戏方法:" _
& vbcrlf & " 电脑随机产生一个四位十进制数字(开头的可以为0),猜10次。" _
& vbcrlf & " 每次返回AaBb" _
& vbcrlf & " A表示数字正确并且位置正确" _
& vbcrlf & " B表示数字正确但位置不正确" _
& vbcrlf & " 如:假设要猜的数字是1234" _
& vbcrlf & " 如果游戏者猜0134即返回A2B1(3、4为A,1为B)",4096+32,"猜数游戏"
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 & "请输入猜测数:(还有" & 10-cs & "次机会)","猜数游戏")
if n=false then wscript.quit
if len(n)<>4 then
msgbox "位数不正确,请重新输入!",16+4096,"错误"
elseif pdsz(n)=false then
msgbox "输入的不是数字,请重新输入!",16+4096,"错误"
elseif not pd(n) then
msgbox "不能有重复的数字,请重新输入!",16+4096,"错误"
else
cs=cs+1:exit do
end if
loop
call jg(n)
if cs=10 and n<>s then msgbox "电脑随机数为:" & s & vbcrlf & "很遗憾,没有猜对,继续努力!",4096,"结果"
if n=s then msgbox "电脑随机数为:" & s & vbcrlf & "第" & cs & "次猜测:" & n & vbcrlf & "结果:" & "A" & a & "B" & b,4096+48,"恭喜你,猜对了!":exit do
sm=sm & "第" & cs & "次猜测:" & n & vbcrlf & "结果:" & "A" & a & "B" & b & vbcrlf & vbcrlf
loop
s=""
loop until msgbox("是否再来一遍?",vbyesno,"游戏结束")=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 ]
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 ]
|
|
2007-9-25 15:56 |
|
|
dosmania
初级用户
 
积分 172
发帖 54
注册 2007-1-2
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
呵呵``jmz573515 厉害,VBS互动性N 好
if len(n)<>4 靠``根本不能比嘛```VBS这个就搞定了位数``
不知jmz573515 对s1 s2 s3 s4 的重复可能性进行考虑没有```?
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```?
|
|
2007-9-26 14:31 |
|
|
dosmania
初级用户
 
积分 172
发帖 54
注册 2007-1-2
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
re s11ss:
你的代码也完全符合要求```不知你对随机数重复的验证是哪些``
re s11ss:
Your code also fully meets the requirements```I don't know which ones you verified for random number repetition``
|
|
2007-9-26 14:53 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
dosmania:
if len(n)<>4 靠``根本不能比嘛```VBS这个就搞定了位数``
不知你说的是不能比是什么意思,我认为可以限定在4位啊??
不知jmz573515 对s1 s2 s3 s4 的重复可能性进行考虑没有```?
重复的可能性不知道你指的是什么?(你是说可能性有多大?还是说有可能重复?)
这里我只考虑了有可能重复所以做了判断,如果重复的话重新生成随机数,并没有考虑他的可能性有多大,也许效率不高...)
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...)
|
|
2007-9-26 15:31 |
|
|
s11ss
银牌会员
    
积分 2098
发帖 566
注册 2007-9-11
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
Originally posted by dosmania at 2007-9-26 02:53 PM:
re s11ss:
你的代码也完全符合要求```不知你对随机数重复的验证是哪些``
随机数重复?
不明白你的具体意思.
反正我的代码是让随机数的4位数字不重复,至于随机数与随机数之间是否重复我并未控制,不过题目里好象也没有要求啊
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?
|
|
2007-9-26 16:52 |
|
|