2017-10-03 10 views
1

これは私のスクリプト/プログラムです。間違ったセクション: この数学チャレンジプログラムにタイマースクリプトを追加するには?

@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秒間意志を持続し、この数学の挑戦をタイマースクリプトを追加します。私はタイムアウト/ t 60/NOBREAKのような多くの方法を試しましたが、ユーザーの入力(セット/ p)の前にスクリプト化されている場合、プレイヤーは問題を解決するために提示されませんが、入力はプレーヤーにもタイムアウトされません。 & &を使用すると、入力とコマンドの両方を一度に実行すると、表示されていない空白の画面も同じ画面も返されるため、正しく動作しないようです。他のストップウォッチスクリプトの実装も同じ問題に直面しています。

+2

https://stackoverflow.com/questions/28143549/how-to-exit-a-set-p-in-a-batch-file/28196468#28196468 – Aacini

+0

。私はそれを勉強してスクリプトに実装する必要があります。 – kornkaobat

答えて

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 
+0

ありがとうございます。素人の言葉であなたのスクリプトがどのように動作するかを調べることから、高度なバッチスクリプトを学ぶ余地は常にあるようです。良い一日を過ごしてください! – kornkaobat

+0

素敵なもの........ – npocmaka

関連する問題