Work the Shell - <emphasis>Yahtzee</emphasis> as a Shell Script? When Will It End?

by Dave Taylor

We seem to spend a lot of time talking about games and how to program them as shell scripts, don't we? From Blackjack to Baccarat, we're in danger of having to rename this column “game programming in the shell”. But, that'd be crazy; who in the heck would write multiple games as shell scripts?

So, this month, I thought it would be fun to look at a dice game and see how the basic set of playing card functions we've written previously compare to the necessary functions to play a dice game.

Yahtzee was first introduced by Hasbro in 1956 as Yacht (having been invented by a wealthy couple on their fancy boat) and has been one of its best-selling titles since, spawning many variants, including hand-held electronic games and more. At its heart though, it's basically five-card draw poker played with dice. The wrinkle is that there are a set number of possible hands you can roll, and you attempt to achieve them all to maximize your score.

For example, roll a 3 4 4 4 5, and you might well pick up the 3 and the 5, hoping for either “your fours” (which you can get only once and want to choose when you have the maximum number of fours showing), or if you get five of a kind, a “Yahtzee”, which is a big-points bonus but obviously difficult to achieve.

Like five-card draw, you can pick up zero to five dice and reroll them, but unlike five-card draw, you can do this twice on your turn, not once. So, perhaps the 3 4 4 4 5 rerolls as a 1 4 4 4 4. The second roll would then be to reroll the one and hope for another four. Either way, it's a good roll (unless you've already marked your fours).

Modeling It All

Dice are quite easy to create in a script—so easy it reveals how straightforward a script like liar's dice would be to write:

function rollDie()
{

    dieroll=$(( ( $RANDOM % 6 ) + 1 ))
}

If it's this easy to roll a die, though (dice, by the way, is plural of die), it'd be darn easy to write a quick Dungeons and Dragons dice roller too, as shown:

function rollDie()
{
    sides=${1:-6}

    echo "testing with a $sides-sided die...."

    dieroll=$(( ( $RANDOM % $sides ) + 1 ))
}

All you need to do is call rollDie with the number of sides you want on the dice it needs to roll. Using a 20-sided die? Try rollDie 20 to see what rolls.

This also can quickly and easily be converted into a command-line function, so you could be a real D&D nerd by having a laptop adjacent and typing in roll 20 every time you're actually supposed to roll the die.

But back to Yahtzee, yes? The easy part of modeling the game is the dice rolls. We need to have five dice, and that easily can be done with an array:

rollDie ; dice[0]=$dieroll
rollDie ; dice[1]=$dieroll
rollDie ; dice[2]=$dieroll
rollDie ; dice[3]=$dieroll
rollDie ; dice[4]=$dieroll

There, that's your first roll of the five dice. Displaying the results also is easy:

echo "You rolled " ${dice[0]} ${dice[1]} ${dice[2]} ${dice[3]} ${dice[4]}

Note carefully where I do and don't need to use the curly braces to get the array to work properly in the shell. Try this to see how it differs:

echo "You rolled " $dice[0] $dice[1]

Quite different results, as you can see. (And, as usual with shell programming, there's no useful warnings or error messages to clue you in to what might be wrong.)

Rerolling Specific Dice

Rolling the dice to get an initial hand is pretty straightforward, so let's take the next step and write the code to let you reroll any or all of the five dice twice to get your final hand.

There are a number of ways to ask for this sort of input, but to make it a bit chatty, let's simply present each die in ordinal value and let the player enter the appropriate number to indicate that it should be rerolled. Um, let me show you what I mean:

echo -n "Reroll which dice? "

read answer
for reroll in $answer
do
  echo "Requested: $reroll"
done

Here, you might specify that you want die 1 and 3 rerolled by typing in 1 3. Tweaking this just a bit, the for loop then can test for the validity of each entry:

for reroll in $answer
do
  if [ $reroll -lt 1 -o $reroll -gt 5 ] ; then
    echo "Invalid entry: $reroll. Please enter 1-5"
  else
    echo "Requested: $reroll"
  fi
done

Now, of course, it's time for some actual logic here, not merely a rudimentary test. I've simplified things just a wee bit by using array indices 1–5 rather than 0–4, sacrificing the slot of entry 0 so that it's easier to work with the values. This means if you ask to reroll die 4, for example, it's just a reassignment of dice[4].

Here's the new, improved for loop:

for reroll in $answer
do
  if [ $reroll -lt 1 -o $reroll -gt 5 ] ; then
    echo "Invalid entry: $reroll. Please enter 1-5"
  else
    rollDie
    dice[$reroll]=$dieroll
  fi
done

You can see that it's quite simple, and if we're not afraid of the code stretching out a bit, we simply can copy and paste some of it to show our before and after rolls:

echo -n "Your new  roll: [${dice[1]}], "
echo -n "[${dice[2]}], [${dice[3]}], "
echo    "[${dice[4]}] and [${dice[5]}]"

Let's run it once to see what's happening, and then next month, we'll start working on the actual game itself, rather than just the dice rolls:

$ ./yahtzee.sh
You rolled [2], [6], [5], [2] and [1]
Reroll which dice? 2 3 5
Your new  roll: [2], [2], [4], [2] and [5]

Yes, I snuck in the notation of having the dice values shown within square brackets just for visual appearance. It makes the echo statements a bit more confusing, as you can see just a bit earlier, but the output is more attractive.

Listing 1. yahtzee.sh


#!/bin/sh

function rollDie()
{
    sides=${1:-6}

    dieroll=$(( ( $RANDOM % $sides ) + 1 ))
}

rollDie ; dice[1]=$dieroll
rollDie ; dice[2]=$dieroll
rollDie ; dice[3]=$dieroll
rollDie ; dice[4]=$dieroll
rollDie ; dice[5]=$dieroll

echo -n "You rolled [${dice[1]}], [${dice[2]}], [${dice[3]}], "
echo    "[${dice[4]}] and [${dice[5]}]"

echo -n "Reroll which dice? "

read answer
for reroll in $answer
do
  if [ $reroll -lt 1 -o $reroll -gt 5 ] ; then
    echo "Invalid entry: $reroll. Please enter 1-5"
  else
    rollDie
    dice[$reroll]=$dieroll
  fi
done

echo -n "Your new  roll: [${dice[1]}], [${dice[2]}], [${dice[3]}], "
echo    "[${dice[4]}] and [${dice[5]}]"

exit 0

The yahtzee.sh script is also available on the LJ FTP site at ftp.linuxjournal.com/pub/lj/listings/issue162/9819.tgz

Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up tech support at AskDaveTaylor.com.

Load Disqus comments

Firstwave Cloud