如何在此数学挑战赛程序中添加计时器脚本? [英] How to add a timer script into this math challenge program?

查看:86
本文介绍了如何在此数学挑战赛程序中添加计时器脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的脚本/程序;

@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秒,直到玩家重定向到:wrong 部分.我尝试了许多方法,例如 timeout/t 60/NOBREAK ,如果在用户输入( set/p )之前编写脚本,则不会让玩家输入游戏的答案出现了解决该问题的方法,但是如果在输入后编写脚本,则播放器也不会计时.使用&& 同时运行输入和命令似乎无法正常工作,因为它要么返回空白屏幕,要么返回不显示计时器的同一屏幕.实现其他秒表脚本也面临相同的问题.

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.

推荐答案

我有一些业余时间,因此我完成了解决此问题的方法.

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

这篇关于如何在此数学挑战赛程序中添加计时器脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆