Im am trying to make a simple coin game in batch. The problem I have is once the delay is completed (ping) "goto was unexpected at this time" is all I get before the window closes. Any help would be appreciated!
#echo off
color f0
title Nim In Batch
::: Main Menu
:main
set /a coins=12
set /a time=0
cls
echo Nim In Batch!
echo.
choice /n /c pr /m "Play Or See Rules?"
if %errorlevel%==1 (
goto play
)
if %errorlevel%==2 (
goto rules
)
::: Rules
:rules
cls
echo Objective: Be the one to claim the last coin
echo.
echo Rules:
echo -You Play First
echo -You Must Remove 1,2 Or 3 Coins Each Turn
echo -Turns Switch Once Coins Are Removed
echo -Coins Must Be Removed In Order
echo.
echo Ready?
pause>nul
goto main
::: Game
:play
cls
echo (%coins%) Coins
echo.
choice /n /c 123 /m "How Many Coins Do You Wish To Remove?"
if %errorlevel%==1 (
set /a Coins=%coins%-1
)
if %errorlevel%==2 (
set /a Coins=%coins%-2
)
if %errorlevel%==3 (
set /a Coins=%coins%-3
)
cls
echo (%coins%) Coins
echo.
echo Computer Is Thinking
ping 1.1.1.1 -n 1 -w 3250 >NUL
if %errorlevel%==1 (
set /a Coins=%coins%-3
)
if %errorlevel%==2 (
set /a Coins=%coins%-2
)
if %errorlevel%==3 (
set /a Coins=%coins%-1
)
if %coin%==0 (
goto end
) else (
goto play
)
:end
cls
echo Computer Wins!
echo He Collected The Final Coin!
echo.
pause
goto main
You forget an s in %coins% here :
if %coins%==0 (
goto end
) else (
goto play
)
Related
I'm trying to code a tictactoe game in batch.
But I ran into several problems I can't solve.
After the third move of player 1, the game has to check if player 1 has won.
I tried to do that by making 8 variables of all possible 8 winlines. And then the game checks if any of the winlines equals to XXX or OOO.
The Problem is that the field variables (_f1, _f2, etc.) don't change to X or O. I set them at the beginning of the script by their numbers, but I dont understand why they dont change once a player put a X or O in that field/variable.
The code is very ugly and unnecessarily long. I'm aware of the for command and i can do basic loops, but I cant wrap my head around the syntax if the command gets too complicated. How can I put all the repititions in for loops?
#echo off
setlocal enabledelayedexpansion
title .:TIC TAC TOE:.
mode con: cols=40 lines=20
color 02
cls
goto :main
:check
if !_win1!==XXX (
echo END OF GAME
pause
)
if !_win2!==XXX (
echo END OF GAME
pause
)
if !_win3!==XXX (
echo END OF GAME
pause
)
if !_win4!==XXX (
echo END OF GAME
pause
)
if !_win5!==XXX (
echo END OF GAME
pause
)
if !_win6!==XXX (
echo END OF GAME
pause
)
if !_win7!==XXX (
echo END OF GAME
pause
)
if !_win8!==XXX (
echo END OF GAME
pause
)
goto :eof
:player2turn
choice /c 123456789 /n /m "!_player2!, it's your turn!"
if !errorlevel!==1 (
if !_f1!==1 (
set _f1=!_player2symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==2 (
if !_f2!==2 (
set _f2=!_player2symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==3 (
if !_f3!==3 (
set _f3=!_player2symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==4 (
if !_f4!==4 (
set _f4=!_player2symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==5 (
if !_f5!==5 (
set _f5=!_player2symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==6 (
if !_f6!==6 (
set _f6=!_player2symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==7 (
if !_f7!==7 (
set _f7=!_player2symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==8 (
if !_f8!==8 (
set _f8=!_player2symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==9 (
if !_f9!==9 (
set _f9=!_player2symbol!
goto :eof
) else (
goto :player1turn
)
)
goto :eof
:player1turn
choice /c 123456789 /n /m "!_player1!, it's your turn!"
if !errorlevel!==1 (
if !_f1!==1 (
set _f1=!_player1symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==2 (
if !_f2!==2 (
set _f2=!_player1symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==3 (
if !_f3!==3 (
set _f3=!_player1symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==4 (
if !_f4!==4 (
set _f4=!_player1symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==5 (
if !_f5!==5 (
set _f5=!_player1symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==6 (
if !_f6!==6 (
set _f6=!_player1symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==7 (
if !_f7!==7 (
set _f7=!_player1symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==8 (
if !_f8!==8 (
set _f8=!_player1symbol!
goto :eof
) else (
goto :player1turn
)
)
if !errorlevel!==9 (
if !_f9!==9 (
set _f9=!_player1symbol!
echo !_win1!
echo !_player1symbol!
pause
goto :eof
) else (
goto :player1turn
)
)
goto :eof
::displaying the map
:map
echo.
echo.
echo.
echo !_f7! ^| !_f8! ^| !_f9!
echo ---------
echo !_f4! ^| !_f5! ^| !_f6!
echo ---------
echo !_f1! ^| !_f2! ^| !_f3!
echo.
echo.
echo.
goto :eof
::setting variables
:setvar
set _f1=1
set _f2=2
set _f3=3
set _f4=4
set _f5=5
set _f6=6
set _f7=7
set _f8=8
set _f9=9
set _win1=!_f7!!_f8!!_f9!
set /a _win2=!_f4!+!_f5!+!_f6!
set /a _win3=!_f1!+!_f2!+!_f3!
set /a _win4=!_f7!+!_f4!+!_f1!
set /a _win5=!_f8!+!_f5!+!_f2!
set /a _win6=!_f9!+!_f6!+!_f3!
set /a _win7=!_f7!+!_f5!+!_f3!
set /a _win8=!_f9!+!_f5!+!_f1!
goto :eof
::player settings
:setup
echo.
set /p _player1=Enter name for Player 1:
echo.
choice /c xo /n /m "!_player1! do you want to play as X or O?"
if !errorlevel!==1 (
set _player1symbol=X
set _player2symbol=O
) else (
set _player1symbol=O
set _player2symbol=X
)
echo.
echo.
set /p _player2=Enter name for Player 2:
echo.
cls
echo.
echo.
echo.
echo !_player1!(!_player1symbol!) vs. !_player2!(!_player2symbol!)
pause>nul
goto :eof
:main
setlocal
call :setup
cls
call :setvar
call :map
call :player1turn
cls
call :map
call :player2turn
cls
call :map
call :player1turn
cls
call :map
call :player2turn
cls
call :map
call :player1turn
call :check
endlocal
goto :eof
You should research and define your coding approach before write a single line of program. Also, you should learn the language features in order to make good use of its facilities. This applies to any programming language.
This is the way I would do it:
#echo off
setlocal EnableDelayedExpansion
rem Test for win: Horizontal rows
set "win[1]=%%pos[1]%%%%pos[2]%%%%pos[3]%%"
set "win[2]=%%pos[4]%%%%pos[5]%%%%pos[6]%%"
set "win[3]=%%pos[7]%%%%pos[8]%%%%pos[9]%%"
rem Test for win: Vertical cols
set "win[4]=%%pos[1]%%%%pos[4]%%%%pos[7]%%"
set "win[5]=%%pos[2]%%%%pos[5]%%%%pos[8]%%"
set "win[6]=%%pos[3]%%%%pos[6]%%%%pos[9]%%"
rem Test for win: Diagonals
set "win[7]=%%pos[1]%%%%pos[5]%%%%pos[9]%%"
set "win[8]=%%pos[7]%%%%pos[5]%%%%pos[3]%%"
for /L %%i in (1,1,9) do set "pos[%%i]=%%i"
set "taken=_"
cls
set /P "player[X]=Enter player 1 (X) name: "
set /P "player[O]=Enter player 2 (O) name: "
call :showBoard
set "players=XO"
for /L %%n in (1,1,9) do (
call :playerTurn !players:~0,1!
if !result! equ 1 echo You win^^^! & goto :EOF
set "players=!players:~1,1!!players:~0,1!"
)
echo Game was a draw...
goto :EOF
:playerTurn wichOne
choice /C 123456789 /N /M "!player[%1]!, it's your turn: "
set "choice=%errorlevel%"
if "!taken:%choice%=!" neq "%taken%" echo Bad position & goto playerTurn
set "taken=%taken%%choice%"
set "pos[%choice%]=%1"
call :showBoard
rem Test if win
set "result=0"
for /L %%i in (1,1,8) do if !result! equ 0 (
call set "line=!win[%%i]!
if "!line!" equ "%1%1%1" set "result=1"
)
exit /B
:showBoard
echo/
echo/
echo %pos[1]% ^| %pos[2]% ^| %pos[3]%
echo ---------
echo %pos[4]% ^| %pos[5]% ^| %pos[6]%
echo ---------
echo %pos[7]% ^| %pos[8]% ^| %pos[9]%
echo/
echo/
exit /B
EDIT 2021/12/08: New version added
The original version of this program was simple enough to be understand by the OP. The next version below was written with the purpose of be as efficient and short as possible:
#echo off
setlocal EnableDelayedExpansion
set "win=" & set "out="
for %%a in (1:2:3 4:5:6 7:8:9 1:4:7 2:5:8 3:6:9 1:5:9 3:5:7) do (
for /F "tokens=1-3 delims=:" %%x in ("%%a") do (
set "win=!win! ^!pos[%%x]^!^!pos[%%y]^!^!pos[%%z]^!"
if "!out:~-3,1!" neq "9" set "out=!out!$^!pos[%%x]^! ^^| ^!pos[%%y]^! ^^| ^!pos[%%z]^!"
)
)
set "out=!out:~1!"
:menu
cls
set /P "player[X]=Enter player 1 (X) name: "
set /P "player[O]=Enter player 2 (O) name: "
for /L %%i in (1,1,9) do set "pos[%%i]=%%i"
set "options=123456789" & set "filler=asdfghjkl"
for %%n in (X O X O X O X O X) do (
cls
echo/& echo/& echo %out:$=& echo --+---+--& echo %& echo/& echo/
choice /C !options! /N /M "!player[%%n]!, it's your turn: "
for /F "tokens=1,2" %%e in ("!errorlevel! !filler:~0,1!") do (
set "options=!options:%%e=%%f!" & set "filler=!filler:~1!" & set "pos[%%e]=%%n"
)
set "line=%win%"
if "!line:%%n%%n%%n=!" neq "!line!" echo You win^^^! & goto nextGame
)
echo Game was a draw...
:nextGame
echo/
pause
goto :menu
#Aacini posted a great answer, so I decided to post my own as well. As they say, there are many ways to skin a cat.
#echo off & Title Tic-Tac-Toe
setlocal enabledelayedexpansion & mode con: cols=40 lines=20 & color 02
for /F %%i in ('echo prompt $E ^| cmd') do set "n=%%iE"
:menu
cls & set gone=. & for %%p in (player[X] player[O]) do set /p "%%~p=enter name %%p: "
for /l %%i in (1,1,9) do set a%%i=%%i
for /L %%i in (1,1,4) do for %%a in (X O) do (
for %%x in (!a1!!a2!!a3! !a4!!a5!!a6! !a7!!a8!!a9! !a1!!a4!!a7! !a2!!a5!!a8! !a3!!a6!!a9! !a1!!a5!!a9! !a3!!a5!!a7!) do (
if "%%x" == "!_h!!_h!!_h!" echo !pl! wins & timeout /t 4 /NOBREAK >nul & goto :menu
)
set "pl=!player[%%a]!" & set _h=%%a
call :play
set "a!ch!=%%a"
if %%i geq 4 echo No winners & timeout /t 4 /NOBREAK >nul & goto :menu
)
:play
cls & echo(
echo !a1! ^| !a2! ^| !a3!%n% ----------%n% !a4! ^| !a5! ^| !a6!%n% ----------%n% !a7! ^| !a8! ^| !a9!%n%
choice /c 123456789 /m "!pl!'s turn"
set ch=!errorlevel!
if !ch! equ 0 exit /b
if "!gone:%ch%=!" neq "%gone%" echo That spot has been taken, try again & timeout /t 3 /NOBREAK >nul & goto :play
set "gone=%gone%!ch!"
goto :eof
And a version for older windows that does not support VT100:
#echo off & Title Tic-Tac-Toe
setlocal enabledelayedexpansion & mode con: cols=40 lines=20 & color 02
:menu
cls & set gone=. & for %%p in (player[X] player[O]) do set /p "%%~p=enter name %%p: "
for /l %%i in (1,1,9) do set a%%i=%%i
for /L %%i in (1,1,4) do for %%a in (X O) do (
for %%x in (!a1!!a2!!a3! !a4!!a5!!a6! !a7!!a8!!a9! !a1!!a4!!a7! !a2!!a5!!a8! !a3!!a6!!a9! !a1!!a5!!a9! !a3!!a5!!a7!) do (
if "%%x" == "!_h!!_h!!_h!" echo !pl! wins & timeout /t 4 /NOBREAK >nul & goto :menu
)
set "pl=!player[%%a]!" & set _h=%%a
call :play
set "a!ch!=%%a"
if %%i geq 4 echo No winners & timeout /t 4 /NOBREAK >nul & goto :menu
)
:play
cls & echo(
echo !a1! ^| !a2! ^| !a3!&echo ----------&echo !a4! ^| !a5! ^| !a6!&echo ----------&echo !a7! ^| !a8! ^| !a9!&echo(
choice /c 123456789 /m "!pl!'s turn"
set ch=!errorlevel!
if !ch! equ 0 exit /b
if "!gone:%ch%=!" neq "%gone%" echo That spot has been taken, try again & timeout /t 3 /NOBREAK >nul & goto :play
set "gone=%gone%!ch!"
goto :eof
I got the solution
All i had to do was to set the win variables later, when checking the winlines.
For the task of testing, string substitution can be used with variables to test a list of winning cell combinations concisely.
#Echo off & CHCP 65001 > nul
Set LF=^
%= Above empty line required =%
For /l %%i in (1 1 9)Do Set c%%i=_
Set Screen=Player: !Player!!LF!!LF! [!c7!][!c8!][!c9!]!LF! [!c4!][!c5!][!c6!]!LF! [!c1!][!c2!][!c3!]!LF!
Set state=!c1!!c2!!c3!,!c1!!c4!!c7!,!c1!!c5!!c9!,!c4!!c5!!c6!,!c7!!c8!!c9!,!c7!!c5!!c3!,!c3!!c6!!c9!,!c2!!c5!!c8!
Set "p1=X" & Set "p2=O"
Set /A "choices=123456789","Move=0","p%p1%%p1%%p1%=1","p%p2%%p2%%p2%=2"
Setlocal EnableDelayedExpansion & For /l %%m in (1 1 9)Do (
Set /A "player=((%%m-1) %% 2)+1"
cls & Echo(%Screen%
For /f "Delims=" %%e in ('Choice /n /c:!choices!')Do (
Set "choices=!choices:%%e=!"
For %%i in (!player!)Do Set "c%%e=!p%%i!"
)
cls & Echo(%Screen%
If %%m GTR 4 (
Set "test=%State%"
For %%i in (!p1!!p1!!p1! !p2!!p2!!p2!)Do if not "!test:%%i=!"=="!test!" (
Echo(player !p%%i! won
Goto:Eof
) ) )
Echo(draw & Endlocal
I am trying to make a fully immersive text adventure using a batch file.
Here is my problem: I want the answers to be a text input, so that the players type in a response which dictates where they will go.
For a lot of questions I need there to be multiple possible inputs. For example when you get to an enemy there are tons of different things you could do, however I can only figure out how to get it to recognise one input.
With other words, I want system to take user input and do actions accordingly.
Here is my code for this section so far:
:forest1
echo you awake in a forest, you do not know where you are or why you are there.
echo infront of you is a small goblin like creature
:recoil1
echo What do you do?
set /p answer=
if %answer%==run (
goto run1
) else (
if %answer%==attack (
goto attack1
) else (
if %answer%==befriend(
goto befriend1
) else (
if %answer%==scream(
goto scream1
) else (
if %answer%==dance (
goto dance1
) else (
echo Nothing happened
timeout /t 1
goto forest1
)
Your way should be modified like this to work:
#echo off
rem Your code before the code you provided above ^^
:forest1
echo You awake in a forest, you do not know where you are or why you are there.
echo In front of you is a small goblin like creature
goto :recoil1
:recoil1
set /p "answer=What do you do? "
if "%answer%" == "run" (
goto :run1
) else (
if "%answer%" == "attack" (
goto :attack1
) else (
if "%answer%" == "befriend" (
goto :befriend1
) else (
if "%answer%" == "scream" (
goto :scream1
) else (
if "%answer%" == "dance" (
goto :dance1
) else (
echo Nothing happened.
timeout /t 1
goto :forest1
)
)
)
)
)
You see: this is complicated a bit; you missed lots of parenthesis!
So, use choice command with some modifications:
#echo off
rem Your code before the code you provided above ^^
:forest1
echo You awake in a forest, you do not know where you are or why you are there.
echo In front of you is a small goblin like creature
goto :recoil1
:recoil1
echo What do you do? Here is a list of options:
echo r - run away
echo a - attack the goblin
echo b - be friend with the goblin
echo s - scream
echo d - dance
echo n - do nothing
choice /C:rabsdn /N
if errorlevel 6 (
echo Nothing happened.
timeout /t 1
goto :forest1
)
if errorlevel 5 goto :dance1
if errorlevel 4 goto :scream1
if errorlevel 3 goto :befriend1
if errorlevel 2 goto :attack1
if errorlevel 1 goto :run1
which is clearer, faster and more readable, isn't it?
Note: the if with the errorlevel should be in descending order because if errorlevel n means if the errorlevel is greater than or equal to n!
Modify the options to better suit for you.
Why not try use if in the for looping to do this job?
#echo off
:forest1
cls & echo/ & if defined answer set answer=<nul
echo/ you awake in a forest, you do not know where you are or why you are there.
echo/ infront of you is a small goblin like creature
:recoil1
set /p "answer= What do you do? "
for %%i in (run attack befriend scream dance) do if /i "%answer%" == "%%i" goto :%answer%1
echo/ Nothing happened
timeout /t 1 & goto forest1
:run1
echo/ Here I'm in Label run1 & exit /b
:attack1
echo/ Here I'm in Label attack1 & exit /b
:befriend1
echo/ Here I'm in Label befriend1 & exit /b
:scream1
echo/ Here I'm in Label scream1 & exit /b
:dance1
echo/ Here I'm in Label dance1 & exit /b
I am trying to add a time out to Set /p Var1= So far I have only found this piece of code from here.
#echo off
setlocal EnableDelayedExpansion
if "%1" NEQ "" goto %1
del enter.tmp 2>nul >nul
start /b cmd /c %0 :secondThread
:FirstThread
set n=0
echo Enter Text (5 seconds timeout):
:loop
set /a n+=1
ping -n 2 localhost > nul
if !n! LSS 5 (
if not exist entER.tmp goto :loop
< Enter.tmp (
set /p input=
)
echo !input!
) ELSE (
echo Timeout for input
)
exit /b
:secondThread
set /p var=
> enter.tmp echo !var!
exit /b
This piece of code works great except it stops the count down only when the input is entered. I would like the count down to stop when any key is pressed. I don't know if this is possible. Thanks
I am currently making a game that has a persuasion system in it. I had all the code for one of the systems set up, but then I set up 2 more, and it started give me an error that said '(number) was not expected at this time'. when I put in 2 for the second choice, and 3 for the 3rd choice.
The code is like this.
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
set name=Quantum
cls
color 0a
Echo King Redwood: So 2000?
pause >nul
echo.
call :colorText 09 "1. 2500"
echo.
call :colorText 0e "2. 3000"
echo.
call :colorText 0c "3. 4000"
echo.
echo 4. 2000
echo.
set /p "purs=Enter:"
if /i %purs% == 1 (
goto CheckB )
if /i %purs% == 2 (
goto CheckY )
if /i %purs% == 3 (
goto CheckR )
if /i %purs% == 4 (
goto Convo )
:CheckB
set bleu=%random:~-2,1%
if %bleu% GTR 10 (
goto CheckB )
if %bleu% LSS 0 (
goto CheckB )
set /a num = 3
set /a reward = 2500
goto Res
:CheckY
set Yel=%random:~-2,1%
if %Yel% GTR 10 (
goto CheckY )
if %Yel% LSS 0 (
goto CheckY )
set /a num = 5
set reward = 3000
goto Res
:CheckR
set red=%random:~-2,1%
if %red% GTR 10 (
goto CheckB )
if %red% LSS 0 (
goto CheckB )
set /a num = 7
set /a reward = 4000
goto Res
:Convo
set /a reward = 2000
Echo %name%: I think that is a reasonable price.
Echo King Redwood: Very well.
Echo King Redwood: We will now take you to make sure you are
echo ready.
pause >nul
:Res
if %bleu% GEQ %num% goto Succeed
if NOT %bleu% GEQ %num% goto Fail
:Succeed
Echo %name%: I think that the struggles for such a long trip will be more then that
Echo %name%: How about %reward%?
Echo King Redwod: OK %reward% will work.
pause >nul
goto end
:Fail
Echo %name%: I think that you can give me %reward%.
Echo %name%: You know, for the struggles that there will be along the way.
echo If 2000 isn't good enough for you, I'll just have someone else do it.
pause >nul
:end
exit
:colorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
First, make sure to close the FOR loop by putting a ) before :CheckB.
For the 'was not expected at this time' error, you're sometimes comparing an empty variable to something. For example, by following CheckY, you set Yel, then proceed to Res and check Bleu, which is empty because it hasn't been set. You're putting nothing next to the binary GEQ operator, and that's why it's complaining.
Tip: to debug, try inserting ECHO statements like this:
:Res
echo bleu=%bleu%,num=%num%
Another problem: when using SET, do not surround the = by spaces. SET /a will work with spaces around =, just because of the nature of /a, but plain SET will not. Well, it will append your variable name with a space and prepend your value with a space, which is not what you want.
Another tip: you can constrain what RANDOM returns through SET /a and the modulus operator, like this.
SET /a red=%random% %% 11
This will set red to a number between 0 and 10, so there is no need for the substrings and goto routines you're using after picking your random number.
Also, consider using EXIT /b to exit the batch file and not the whole CMD environment.
I'm trying to do "game" for my friend but when i try to load from save file it will do half of it's job
Code
set penize=0
set penizesekunda=0
for /f "EOL=: tokens=* Delims=0" %%a in (penize.txt) do (
set penize=%%a
)
for /f "EOL=: tokens=* Delims=1" %%b in (penize.txt) do (
set penizesekunda=%%b
)
echo Penize %penize% penize za sekundu %penizesekunda%
sorry it in my language (penize = money, penizesekunda = money per second)
And input is 200 200 but it should be 500 200.
If you want full code :
#echo off
title Hra (Imui)
color 6a
echo Ahoj %computername% cas %time% datum %date%
echo.
echo.
echo Vitej ve hre
echo.
echo.
echo.
echo Tvym cilem bude ovladnout cely VESMIR
echo.
timeout>nul /t 3 /nobreak
echo Nacitani!
set penize=0
set penizesekunda=0
for /f "EOL=: tokens=* Delims=0" %%a in (penize.txt) do (
set penize=%%a
)
for /f "EOL=: tokens=* Delims=1" %%b in (penize.txt) do (
set penizesekunda=%%b
)
if errorlevel 1 do (
echo Pozor nebyla nalezena zadna ulozena hra nebo doslo k chybe startovani! (hry se nachazeji v souboru penize.txt)
timeout>nul /t 3 /nobreak
cls
goto Start
)
:Start
cls
set /p "Password=Zadej heslo > "
if %Password%== Admin goto RMV
if NOT %Password%== Imui goto FAIL
if %Password%== Imui goto HesloSpravne
goto Start
:Fail
cls
echo Spatne heslo!
echo Zadej heslo prosim (zbyvaji 2 pokusy)
set /p "Password=Zadej heslo > "
if %Password%== Admin goto RMVCzech
if NOT %Password%== Imui goto FailTwo
if %Password%== Imui goto HesloSpravne
goto Fail
:FailDva
cls
echo Spatne heslo!
color ac
echo Pozor! Mas uz jenom jeden pokus!
echo Prosim zadej heslo!
set /p "Password=Zadej heslo > "
if %Password%== Admin goto RMV
if NOT %Password%== Imui goto RMV
if %Password%== Imui goto HesloSpravne
goto FailTwo
:RMV
echo Smazavam ti tvoji hru
#ECHO ON
del "Hra.exe" /P /Q /F
echo Smazavani dokonceno!
Pause>nul
exit
:HesloSpravne
cls
echo Heslo bylo spravne
goto HlavniMenu
:HlavniMenu
echo Vitej
echo %penizesekunda% %penize%
Pause>nul
Content of penize.txt :
500
200
Another (more efficient) option is to use SET /P to read each line:
<penize.txt (
set "penize="
set /p "penize="
set "penizesekunda="
set /p "penizesekunda="
)
The simple SET statements that clear each variable are there just in case the line happens to be empty or missing in the file, in which case SET /P would preserve any pre-existing variable value.
The code can be shortened by using a FOR loop, especially if you have many more lines to read:
<penize.txt ( for %%V in (penize penizesekunda) do (
set "%%V="
set /p "%%V="
))
Note that you can list the variables on separate lines, which can help readability:
<penize.txt (
for %%V in (
penize
penizesekunda
) do (
set "%%V="
set /p "%%V="
)
)
Here is some example code for you:
REM Get first line of a file
set /p penize=<penize.txt
REM Get second line of a file
for /f "skip=1" %%a in (penize.txt) do set penizesekunda=%%a