User Tools

Site Tools


user:mtaft4:portfolio:scripts:dice_roller

D&D Style Dice Roller

#!/bin/bash
#Dice rolling script using d20 notation
 
if [ "$1" == "eyes" ]
	then
		echo "Har har, very funny."
		exit
fi
 
if [ "$1" != "-n" ]
	then
		dienum=`echo -n $1 | cut -d'd' -f1` #extract the number of die from the d20 notation argument
		facenum=`echo -n $1 | cut -d'd' -f2 | cut -d'+' -f1 | cut -d'-' -f1` #extract the number of faces on the die from d20 notation argument
		isadd="`echo -n $1 | cut -s -d'+' -f2`"
		issub="`echo -n $1 | cut -s -d'-' -f2`"
		target=$(( ($dienum - 1) ))
		i="0"
		echo -n "[ "
		while [ $i -lt $dienum ]
		do
			dieroll=$(( ($RANDOM % $facenum ) + 1 ))
			dietot=$(( ($dietot + $dieroll) ))
			echo -n $dieroll
			if [ $i -ne $target ]
				then
					echo -n " + "
				else
					echo -n " ]"
				if [ "$isadd" != "" ]
					then
						echo -n " + "$isadd
						dietot=$(( ($dietot + $isadd) ))
						echo -n " = "
					elif [ "$issub" != "" ]
						then
							echo -n " - "$issub
							dietot=$(( ($dietot - $issub) ))
							echo -n " = "
					else
						echo -n " = "
				fi
			fi
			i=$(( ($i + 1) ))
		done
	echo $dietot
	else
		dienum=`echo -n $2 | cut -d'd' -f1` #extract the number of die from the d20 notation argument
		facenum=`echo -n $2 | cut -d'd' -f2 | cut -d'+' -f1 | cut -d'-' -f1` #extract the number of faces on the die from d20 notation argument
		isadd="`echo -n $2 | cut -s -d'+' -f2`"
		issub="`echo -n $2 | cut -s -d'-' -f2`"
		target=$(( ($dienum - 1) )) 
		i="0"
		echo -n "[ "
		while [ $i -lt $dienum ]
		do
			dieroll=$(( ($RANDOM % $facenum ) + 1 ))
			dietot=$(( ($dietot + $dieroll) ))
			echo -n $dieroll
			if [ $i -ne $target ]
				then
					echo -n ", "
				else
					echo " ]"
			fi
			i=$(( ($i + 1) ))
		done
fi
user/mtaft4/portfolio/scripts/dice_roller.txt · Last modified: 2011/03/22 11:21 by mtaft4