這是第二篇,今天我要教大家猜數(shù)字
代碼是:
@echo off&title 猜數(shù)字游戲 QQ:137563876
mode con: cols=55 lines=40
for %%i in (n m r a0 a1 a2 a3 a4 a5 a6 a7 a8 a9) do (set %%i=0)
setlocal enabledelayedexpansion
echo ┌─────────────────────┐
echo │? ? ? ? ? ? ? 規(guī)則說(shuō)明? ? ? ? ? ? ? ? ? │
echo └─────────────────────┘
echo │首先,本程序在打開(kāi)時(shí)會(huì)自動(dòng)使用0~9這10個(gè)數(shù) │
echo? ──────────────────────
echo │生成一個(gè)隨機(jī)的4位數(shù),且每個(gè)數(shù)字只使用 1 次│
echo? ──────────────────────
echo │,這個(gè)數(shù)是不會(huì)顯示出來(lái)的。在你輸入一個(gè)4 位│
echo? ──────────────────────
echo │數(shù)之后,系統(tǒng)會(huì)自動(dòng)將它與系統(tǒng)初始生成的隨機(jī)│
echo? ──────────────────────
echo │4 位數(shù)相比較。在個(gè)、十、百、千位上,每猜中│
echo? ──────────────────────
echo │一位,正確數(shù)加1;若猜中數(shù)字,但數(shù)字所在位 │
echo? ──────────────────────
echo │不對(duì)(即數(shù)字順序不對(duì)),則位置錯(cuò)誤數(shù)加1。 │
echo? ──────────────────────
echo │你猜測(cè)的歷史記錄將會(huì)顯示在列表中 ,你可以 │
echo? ──────────────────────
echo │根據(jù)你的猜測(cè)記錄來(lái)推測(cè)正確的數(shù)字。? ? ? ? │
echo? ──────────────────────
echo │注意:你不能輸入含重復(fù)數(shù)字的4位數(shù)。? ? ? │
echo? ──────────────────────
echo │? ? 準(zhǔn)備好了嗎?按任意鍵開(kāi)始游戲?。? ? │
echo └─────────────────────┘
pause>nul
cls
set "f=┌───────────────────────┐"
set "h=│猜測(cè)記錄│? ? 數(shù)字? ? │? 正確? │ 順序錯(cuò)誤 │"
set "i= ────────────────────────"
echo %f%&echo %h%&echo %i%
::隨機(jī)生成數(shù)字不重復(fù)的4位數(shù)
set u=1
set num0=10
:again
set /a num%u%=%random%%%10
set /a v=u-1
for /l %%i in (1 1 %v%) do (if !num%%i!==!num%u%! goto :again)
if %u% lss 4 set /a u+=1&goto :again
set ans=%num1%%num2%%num3%%num4%
::計(jì)算隨機(jī)數(shù)字中0~9每個(gè)數(shù)字的個(gè)數(shù)
for /l %%i in (0 1 9) do (
for %%j in (%num1% %num2% %num3% %num4%) do (
if %%j==%%i set /a a%%i=a%%i+1
)
)
::輸入猜測(cè)數(shù)并獲取每個(gè)位上的數(shù)
:input
set t=1
set ent=
set /p "ent=│輸入你猜測(cè)的有效4位數(shù)(超過(guò)則截取前4位):"
set "t4=%ent:~3,1%"
if not defined t4 goto :input
set "t1=%ent:~0,1%"
set "t2=%ent:~1,1%"
set "t3=%ent:~2,1%"
::檢測(cè)輸入是否正確(是否有重復(fù)數(shù)字、輸入的是否全是數(shù)字)
for /l %%i in (1 1 3) do (
set /a ii=%%i+1
? for /l %%j in (!ii! 1 4) do (
? ? if "!t%%i!"=="!t%%j!" goto :input
)
)
:te
set "t0=!t%t%!"
for /l %%i in (0 1 9) do (if "%t0%"=="%%i" set wr=true)
if defined wr (set /a t+=1) else (goto :input)
set wr=
if %t% lss 5 goto :te
set /a num0-=1
::計(jì)算猜對(duì)個(gè)數(shù)
cls
for /l %%i in (1 1 4) do (
? for /l %%j in (%%i 1 %%i) do (
? ? if "!t%%i!"=="!num%%j!" set /a n+=1
)
)
::計(jì)算輸入數(shù)字中0~9每個(gè)數(shù)字的個(gè)數(shù)
for /l %%i in (0 1 9) do (
for %%j in (%t1% %t2% %t3% %t4%) do (
if %%j==%%i (set /a b%%i=b%%i+1)
)
)
::計(jì)算位置錯(cuò)誤個(gè)數(shù)
for /l %%i in (0 1 9) do (
if !a%%i! gtr 0 (if !b%%i! gtr 0 (set /a m=m+!a%%i!))
)
set /a g=%m%-%n%
::輸出猜測(cè)記錄
set /a r+=1
set /a s=r
if %r% lss 10 set "s=0%r%"
set r%r%=│ 第%s%組 │? ? %t1% %t2% %t3% %t4%? │? %n%? ? │? ? %g%? ? │
echo %f%
echo %h%
echo %i%
for /l %%i in (1 1 %r%) do (echo !r%%i!&echo %i%)
for %%i in (m n g b0 b1 b2 b3 b4 b5 b6 b7 b8 b9) do (set %%i=0)
if "%ans%"=="%ent%" set zz=? ? ? 猜對(duì)啦,你好厲害哦?。。?!? &goto :right
if "%num0%"=="0" set zz=還猜不對(duì)???笨哪!正確答案是:%ans% &goto :right
goto :input
:right
echo ┌───────────────────────┐
echo │? ? %zz%? ? ? │
echo └───────────────────────┘
pause>nul
趕快去試一下,怎么樣你猜對(duì)了嗎?