Math Challenge game in Batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Math Challenge game in Batch

#1 Post by Aacini » 04 Oct 2017 12:00

At this SO question there was a Math Challenge game program that requested a time out feature. I added such a feature and improved the program, so here it is:

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

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Math Challenge game in Batch

#2 Post by einstein1969 » 05 Oct 2017 06:28

multi thread in dos batch ...
I like it!

+1 Antonio ;)

Post Reply