@echo off set /p disks=Enter number of disks: :: Initialize the towers set tower1= for /l %%i in (1,1,%disks%) do set tower1=%%i;%tower1% set tower2= set tower3= :game cls :: Display the current state of the towers echo Tower 1: %tower1% echo Tower 2: %tower2% echo Tower 3: %tower3% echo. :: Prompt the user to input the move set /p move=Enter move (source,destination): :: Parse the user input for /f "tokens=1,2 delims=," %%a in ("%move%") do ( set source=%%a set destination=%%b ) :: Validate the user input if not "%source%"=="1" if not "%source%"=="2" if not "%source%"=="3" goto invalid if not "%destination%"=="1" if not "%destination%"=="2" if not "%destination%"=="3" goto invalid if "%source%"=="%destination%" goto invalid :: Get the top disk from the source tower for /f "tokens=1* delims=;" %%a in ("!tower%source%!") do ( set disk=%%a set tower%source%=%%b ) :: Check if the move is valid if defined disk ( for /f "tokens=1* delims=;" %%a in ("!tower%destination%!") do ( if not defined %%a ( set tower%destination%=%disk%;%%b set /a moves+=1 ) else ( if %disk% lss %%a ( set tower%destination%=%disk%;%%b set /a moves+=1 ) else ( set tower%source%=%disk%;!tower%source%! ) ) ) ) else ( set tower%source%=!tower%source%! ) :: Check if the game is won if "!tower3!"=="%tower1%" ( cls echo Tower 1: %tower1% echo Tower 2: %tower2% echo Tower 3: %tower3% echo. echo Congratulations! You won in %moves% moves. pause>nul exit /b ) :: Ask for the next move goto game :invalid echo Invalid move. Please try again. pause>nul goto game
Write, Run & Share Bash code online using OneCompiler's Online Bash Shell for free. It's one of the robust, feature-rich Bash shell available over online and getting started with the OneCompiler's Bash Shell is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Bash
. OneCompiler also has reference scripts, where you can look for the sample scripts and start coding.
Bash (Bourne Again Shell) is a shell program written by Brian Fox and is an upgraded version of Bourne Shell program 'sh'.
name="Foo"
echo $name
echo "$name"
echo "${name}"
if [ conditional-expression ]
then
statements
fi
if [ conditional-expression ]
then
statements
else
statements
fi
if [ conditional-expression ]
then
statements
elif [ conditional-expression ]
then
statements
else
statements
fi