Code: Select all
@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
set "seconds=30"
set "oper=+-*/"
:startup
REM Determinant to Operator (+-*/)
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
if %answer% equ 0 goto startup
color 0A
cls
echo/
echo Find the correct operations in order in less than %seconds% 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 %seconds%
set /P "userans=" < InputLine.txt
if "!userans!" neq "timeout" goto :check
:timeout
title Time out^^^! :^|
echo/
echo Timed out^^^! The right answer is %d1%%o1%%d2%%o2%%d3%%o3%%d4%=%answer%
goto next
:check
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%" == "%answer%" goto :correct
:wrong
title That's wrong^^^! :(
echo/
echo Your answer gives %result%, the right answer is %d1%%o1%%d2%%o2%%d3%%o3%%d4%=%answer%
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%
:next
echo/
choice /M "Another problem"
if %errorlevel% equ 1 goto :startup
color
del InputLine.txt
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
set "secs="
set "spcs="
for /L %%i in (1,1,%2) do set "secs=!secs!Û" & set "spcs=!spcs! "
for /L %%i in (%2,-1,1) do (
if exist InputLine.txt exit /B
set /A spc=%%i*3
for %%s in (!spc!) do title Seconds left: %%i !secs:~0,%%i!!spcs:~%%s!.
if %%i equ 3 color 0E
ping -n 2 localhost > NUL
)
rem Timed out: kill the SET /P process and create a standard input line
color 0C
taskkill /PID %leftSidePipePID% /F > NUL
echo/
echo timeout> InputLine.txt
exit /B
Antonio