Search code examples
batch-filedelay

Creating a geometrically decaying pause loop


So I am working on creating a batch file game analogious to a "Memory" game. (ie where the player is presented a list of objects for a short time, then asked to repeat the pattern)

My problem comes in how to decrease the time the pattern is exposed to the player as the round # increases.

Here is my current code:

@echo off
set /a y=50
set /a x=1000

:foo
set /a y=%y% + %y%
set /a x= %x% - %y%
echo %y%
echo %x%
ping -n 10 -w %x% 127.0.0.1 > nul
goto foo

When run, the above code does present x and y values that change as expected, however the wait time is always the same. Why is this and how can I fix it?

Thank you for your time.


Solution

  • Firstly why dont you use sleep? It would work fine (type sleep /? for more info) However, here is another way of doing this with for /l loops

    @echo off
    setlocal enabledelayedexpansion
    set score=0
    title Memory Test : Current Score = !score!
    
    for /l %%a in (0,1,20) do (
    Rem In the above sequence, increase 20 to the amount of times you want the test to be performed
    set number[%%a] = !random!!random!
    echo Number: !number[%%a]!
    set /a wait=21-%%a
    set /a wait=!wait!*1000/4
    sleep -m !wait!
    cls
    set /p "input=What was the last number youy saw? "
    if !number[%%a]! equ "!input!" (
    set /a score=!score!+1
    Echo Correct !
    title Memory Test : Current Score = !score!
    )else(
    Echo Incorrect! Coreect Answer = !number[%%a]!
    )
    )
    echo Calculating score...
    pause
    cls
    echo.
    if %score% leq 14 set msg="Nice Try! But you can do better!"
    if %score% geq 15 set msg="Good Job! Your on your way to the top!"
    if %score% equ 20 set msg="Your So Close! Almost a perfect socre!"
    if %score% equ 21 set msg="You got a perfect score! Woderful!"
    Echo %score%/21 : %msg%
    echo.
    pause    
    

    And that should work fine. Note you can change how long the test goes for, but for the first game they'll have a bit more then 5 seconds to study the question, and in the last round a quarter of a second!

    Mona