@echo off rem TODO: Sorting maybe, interactive mode setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION if "%1"=="" goto usage set INPUT=%1 set WORK=%1 set STATE=A set POSITION=1 :loop if "%WORK%" equ "" goto afterloop set CURRENTCHAR=%WORK:~0,1% if %STATE% equ A ( if %CURRENTCHAR% gtr 9 ( call :error Expected a digit between 1 and 9. goto :EOF ) else if %CURRENTCHAR% lss 1 ( call :error Expected a digit between 1 and 9. goto :EOF ) else ( set N=%CURRENTCHAR% set STATE=B ) ) else if %STATE% equ B ( if /i %CURRENTCHAR% equ d ( set STATE=C ) else if /i %CURRENTCHAR% equ w ( set STATE=C ) else if %CURRENTCHAR% gtr 9 ( call :error Expected a digit between 0 and 9 or either D or W. goto :EOF ) else if %CURRENTCHAR% lss 0 ( call :error Expected a digit between 0 and 9 or either D or W. goto :EOF ) else ( set N=%N%%CURRENTCHAR% ) ) else if %STATE% equ C ( if %CURRENTCHAR% gtr 9 ( call :error Expected a digit between 1 and 9. goto :EOF ) else if %CURRENTCHAR% lss 1 ( call :error Expected a digit between 1 and 9. goto :EOF ) else ( set K=%CURRENTCHAR% set STATE=D ) ) else if %STATE% equ D ( if %CURRENTCHAR% equ + ( set STATE=E ) else if %CURRENTCHAR% gtr 9 ( call :error Expected a digit between 0 and 9. goto :EOF ) else if %CURRENTCHAR% lss 0 ( call :error Expected a digit between 0 and 9. goto :EOF ) else ( set K=%K%%CURRENTCHAR% ) ) else if %STATE% equ E ( if %CURRENTCHAR% gtr 9 ( call :error Expected a digit between 1 and 9. goto :EOF ) else if %CURRENTCHAR% lss 1 ( call :error Expected a digit between 1 and 9. goto :EOF ) else ( set X=%CURRENTCHAR% set STATE=F ) ) else if %STATE% equ F ( if %CURRENTCHAR% gtr 9 ( call :error Expected a digit between 0 and 9. goto :EOF ) else if %CURRENTCHAR% lss 0 ( call :error Expected a digit between 0 and 9. goto :EOF ) else ( set X=%X%%CURRENTCHAR% ) ) set WORK=%WORK:~1% set /a POSITION=POSITION+1 goto loop :afterloop if %STATE% equ B echo No number of sides given. && goto :EOF if %STATE% equ C echo No number of sides given. && goto :EOF if %STATE% equ E echo No number to add given. && goto :EOF REM Roll the dice for /l %%i in (1,1,%N%) do call :roll set OUTPUT=%ROLLS% if defined X call :sum echo %OUTPUT% goto :EOF :usage echo %~nx0 - Dice Roller echo. echo Usage: echo %~nx0 [nDk^|nWk][+x] echo. echo where n is the number of dice and k the number of sides. echo If +x is given then all rolls are counted together and x is added. endlocal && goto :EOF :roll set /a ROLL=%RANDOM% %% %K% set /a ROLL=ROLL+1 set ROLLS=%ROLL% %ROLLS% goto :EOF :sum set SUM=%X% for %%i in (%ROLLS%) do set /a SUM=SUM+%%i set OUTPUT=%SUM% goto :EOF :error echo Error on character %POSITION% '%CURRENTCHAR%': %* goto :EOF Parameters: [1-9][0-9]*[WDwd][1-9][0-9]*(\+[1-9][0-9]*)? States: A -> B -> C .>(D).-> E .>(F)-. '----' '-----'