B24S_3_CIUGUREAN

 avatar
Ciugiu
batchfile
a month ago
2.3 kB
1
Indexable
Never
@echo off
setlocal enabledelayedexpansion

:: Initialize the game counter
set "gameCount=0"

:playAgain
:: Increment the game counter
set /a "gameCount+=1"

:: Ask the user to choose the difficulty level
echo Choose the difficulty level:
echo EASY (E), MEDIUM (M), HARD (H)
set /p "level=Enter your choice (E/M/H): "

:: Set the difficulty level and max tries based on the user's choice
set "maxTries=10"
set "k=1"
if /i "%level%"=="M" set "maxTries=8" & set "k=3"
if /i "%level%"=="H" set "maxTries=6" & set "k=10"

:: Generate a random number between 10 and 99
set /a "num=10 + %random% %% 90"

:: Initialize the number of tries
set "tries=0"

:guess
:: Increment the number of tries
set /a "tries+=1"

:: Ask the user to guess the number
set /p "guess=Guess the number: "

:: Check if the guess is valid
if !guess! lss 10 (
    echo INPUT ERROR
    goto guess
)
if !guess! gtr 99 (
    echo INPUT ERROR
    goto guess
)

:: Check if the guess is correct
if !guess! equ !num! (
    set /a "score=(maxTries - tries + 1) * k"
    echo YOU WIN score: !score!
    echo Score: !score!, Date: %date%, Time: %time% >> "%USERNAME%.txt"
    goto end
)

:: Check if the guess is too small or too big
if !guess! lss !num! (
    echo TOO SMALL
) else (
    echo TOO BIG
)

:: Check if the maximum number of tries has been reached
if !tries! geq !maxTries! (
    echo YOU LOOSE It was: !num!
    goto end
)

:: Let the user guess again
goto guess

:end
:: Display the best score if more than one game has been played
if !gameCount! gtr 1 (
    set "bestScore=0"
    for /f "tokens=2 delims=: " %%a in ('type "%USERNAME%.txt"') do (
        set "score=%%a"
        for /f "tokens=1 delims=," %%b in ("!score!") do (
            set /a "score=%%b"
            if !score! gtr !bestScore! set "bestScore=!score!"
        )
    )
    if !gameCount! equ 0 (
        echo == NO SCORES TO COMPARE TO YET ==
    ) else (
        echo == BEST SCORE !bestScore! ==
    )
)

:: Ask the user if they want to play again
set /p "playAgain=Do you want to play again (Y/N)? "
if /i "%playAgain%"=="Y" (
    goto playAgain
) else (
    if /i "%playAgain%"=="N" (
        echo Goodbye!
    ) else (
        echo Invalid choice
        goto end
    )
)

pause
Leave a Comment