How to add a timer script into this math challenge program? [duplicate]

不羁岁月 提交于 2020-01-03 03:03:42

问题


So this is my script/program;

@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  

So, how do we add the timer script to time this math challenge which will lasts for 60 seconds until the player is redirected to the :wrong section. I've tried numerous ways like timeout /t 60 /NOBREAK which if scripted before the user input ( set /p ) won't let the players input the answers of the problem presented in to solve it but if scripted after the input the player wouldn't be timed either. using the && to run both the input and the command at once doesn't seem to work properly as it either returns a blank screen nor a same screen without the timers shown. Implementing other stopwatch scripts also faces the same problem.


回答1:


I had some spare time, so I completed the solution to this problem.

@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


来源:https://stackoverflow.com/questions/46543186/how-to-add-a-timer-script-into-this-math-challenge-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!