如何在这个数学挑战计划中添加计时器脚本?

时间:2017-10-03 11:09:56

标签: batch-file variables math cmd timer

所以这是我的脚本/程序;

@echo off  
cls  
color 0a  
:startup  
REM Determinant to Sign (+-*/)  
set /a deto1=%random% %%4 +1   
set /a deto2=%random% %%4 +1  
set /a deto3=%random% %%4 +1  
set /a deto4=%random% %%4 +1  
REM Set Sign from det  
if %deto1%==1 set o1=+  
if %deto1%==2 set o1=-  
if %deto1%==3 set o1=*  
if %deto1%==4 set o1=/  
if %deto2%==1 set o2=+  
if %deto2%==2 set o2=-  
if %deto2%==3 set o2=*  
if %deto2%==4 set o2=/  
if %deto3%==1 set o3=+  
if %deto3%==2 set o3=-  
if %deto3%==3 set o3=*  
if %deto3%==4 set o3=/  
if %deto4%==1 set o4=+  
if %deto4%==2 set o4=-  
if %deto4%==3 set o4=*  
if %deto4%==4 set o4=/  
REM Randomize digits from 1-9  
set /a d1=%random% %%9 +1  
set /a d2=%random% %%9 +1   
set /a d3=%random% %%9 +1  
set /a d4=%random% %%9 +1  
REM Operation for final answer stored inside memory  
set /a answer=%d1%%o1%%d2%%o2%%d3%%o3%%d4%  
title MathWhiz!  
:game  
cls  
echo.  
echo Find the correct operation in order. All non-intregers are rounded down 
to whole numbers. ( 60 seconds )  
echo.  
echo %d1%...%d2%...%d3%...%d4%=%answer%  
echo.  
set /p userans=Answer(Only sign in order from Left to Right):  
REM Comparasion of user answer to correct answer in memory  
if %userans%==%o1%%o2%%o3% goto :correct  
if not %userans%==%o1%%o2%%o3% goto :wrong  
:wrong  
cls  
title That's wrong! :(  
echo.  
echo That's wrong unfortunately because the answer is   
%d1%%o1%%d2%%o2%%d3%%o3%%d4%=%answer%. Try another one!  
echo.  
pause  
goto :startup  
:correct  
cls  
title That's correct! :)  
echo.  
echo That's correct as from %d1%%o1%%d2%%o2%%d3%%o3%%d4%=%answer%! Try 
another problem.  
echo.  
pause  
goto :startup  

那么,我们如何添加计时器脚本来计算这个数学挑战的时间,该数学挑战将持续60秒,直到玩家被重定向到:错误的部分。我尝试了很多方法,如 timeout / t 60 / NOBREAK ,如果在用户输入( set / p )之前编写脚本,则不会让玩家输入答案解决它的问题,但如果在输入后编写脚本,玩家也不会被计时。使用&& 同时运行输入和命令似乎无法正常工作,因为它在没有显示计时器的情况下返回空白屏幕或同一屏幕。实现其他秒表脚本也面临同样的问题。

1 个答案:

答案 0 :(得分:3)

我有空闲时间,所以我完成了解决这个问题的方法。

@echo off
setlocal EnableDelayedExpansion

rem If this file is re-executed as pipe's right side, go to it
if "%~1" equ "TimeoutMonitor" goto %1

cls
color 0a
set "oper=+-*/"

:startup
REM Determinant to Sign (+-*/)
for /L %%i in (1,1,3) do (
   set /A det=!random! %% 4
   for /F %%d in ("!det!") do set "o%%i=!oper:~%%d,1!"
)
REM Randomize digits from 1-9
for /L %%i in (1,1,4) do (
   set /A d%%i=!random! %% 9 +1
)
REM Operation for final answer stored inside memory
set /A answer=d1%o1%d2%o2%d3%o3%d4
title MathWhiz^^^!

cls
echo/
echo Find the correct operations in order in less than 60 seconds.
echo All non-integers are rounded down to whole numbers.
echo/
echo %d1%...%d2%...%d3%...%d4% = %answer%
echo/

REM Timed input
set "userans="
del InputLine.txt 2> NUL
(
   set /P "userans=Answer (only three +-*/ operators in order from Left to Right): " > CON
   >InputLine.txt call echo %%userans%%
) 2> NUL | "%~F0" TimeoutMonitor 60
set /P "userans=" < InputLine.txt
del InputLine.txt
if "%userans%" equ "timeout" goto :timeout

REM Comparison of user answer to correct answer in memory
set "result=wrong"
set /A "result=d1%userans:~0,1%d2%userans:~1,1%d3%userans:~2,1%d4" 2> NUL
if "%result%" equ "%answer%" goto :correct

:wrong
title That's wrong^^^! :(
echo/
echo That's wrong^^^! The answer is %d1%%o1%%d2%%o2%%d3%%o3%%d4%=%answer%. Try another one^^^!
goto next

:timeout
title Timed out^^^! :^|
echo/
echo Time out^^^! The correct answer is %d1%%o1%%d2%%o2%%d3%%o3%%d4%=%answer% Try another problem.
goto next

:correct
title That's correct^^^! :)
echo/
echo That's correct as from %d1%%userans:~0,1%%d2%%userans:~1,1%%d3%%userans:~2,1%%d4%=%answer% Try another problem.

:next
echo/
choice /M "Another problem"
if not errorlevel 2 goto :startup
goto :EOF


:TimeoutMonitor

rem Get the PID of pipe's left side
tasklist /FI "IMAGENAME eq cmd.exe" /FO TABLE /NH > tasklist.txt
for /F "tokens=2" %%a in (tasklist.txt) do (
   set "leftSidePipePID=!lastButOnePID!"
   set "lastButOnePID=%%a"
)
del tasklist.txt

rem Wait for the input line, or until the number of seconds passed
for /L %%i in (%2,-1,1) do (
   if exist InputLine.txt exit /B
   title MathWhiz^^^! (%%i seconds left^)
   ping -n 2 localhost > NUL
)

rem Timed out: kill the SET /P process and create a standard input line
taskkill /PID %leftSidePipePID% /F > NUL
echo/
echo timeout> InputLine.txt

exit /B