User Tools

Site Tools


boggle

Bash Boggle

I'm proud of this little script, it works well, is modeled after the real game (I read the dice myself), and, at the time I wrote this, I only had about an hour invested in it. I think if I were to re-write it today, I couldn't do much to improve on it.


Boggle.sh
#!/bin/bash
# Print out a Boggle board to stdout.
# The 16 dice and their 6 sides each, this creates Dice[n]=
Dice=\
(QBAJOM EFIEYH ODENWS HPSEIN GKYLUE ILUWRG CITAOA BYLTIA \
 DZNVEA ODUNKT RHASOM PAECMD GVZTEI OFIRXB REALSC PELUTS)
#
AllDice="  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15"
 
# Cut the string that represents the order of the dice in 
# the tray 99 time at some random point, then swap the parts
# around. Repeat as needed.
for ((a=1; a <= 100 ; a++))
do
   LeftCut=$(($(($(($RANDOM%15))*3))+3))
   RightCut=$((LeftCut+3))
   LeftEnd=${AllDice:0:LeftCut} 
   RightEnd=${AllDice:RightCut}
   Middle=${AllDice:LeftCut:3}
   AllDice="$Middle$LeftEnd$RightEnd"
done
# Roll the Dice to get what side is up.
Count=0
for Each in $AllDice
    do
    Side=$(($RANDOM%6))
    Die=${Dice[Each]:Side:1}
    [ $Die == Q ] && echo -n $Die"u " || echo -n $Die"  "
    [ $((Count%4)) -eq 3 ] && echo
    (( Count++ ))
    done
 
boggle.txt · Last modified: by steve