Work the Shell

007's Favorite Game: "Baccarat"?

Dave Taylor

Issue #160, August 2007

Create a shell script to get a taste of being Bond, James Bond.

I was recently watching Casino Royale and thinking about the James Bond series, particularly how Sean Connery was so much more sophisticated as Bond than Daniel Craig. Connery was more debonair, and one of the ways he'd demonstrate it was by playing a mysterious high-stakes game called Baccarat while surrounded by gorgeous women in casinos in Monte Carlo.

Well, I can't create a casino as a shell script, and I certainly can't create either a secret agent or a gorgeous female sidekick, but I can create a Baccarat game as a shell script. Heck, it's probably the first time anyone's even attempted it!

If you've been a faithful reader of this column since the beginning, you'll know that almost two years ago we started out by writing a Blackjack game as a shell script. It was a long-winded affair (I didn't just say that, did I?), but as part of the project, we created a simple way to emulate a deck of cards, “shuffle” the cards (that is, put them in quasi-random order) and even convert a numeric 1–52 value into a suit and rank.

We'll use that as the starting point for creating our Baccarat game, so we can focus on the complicated rules. Let's start there.

The Basics of Baccarat

Baccarat has been around since the mid-1400s, and the variation I'll be coding, Punto Banco, is completely rule-based with no skill involved. Two cards are dealt to both the player and banker and, depending on those cards, a third might be dealt to one or both. Face cards are worth zero, and numeric cards are worth face value. You add the value of a hand and its final value is that value modulo 10. The higher point value wins, and if they're identical, it's a tie.

For example, if the player was dealt a 7H and a 3C, that'd be worth zero (7 + 3 = 10 % 10 = 0). A 6S and 2D is better though; it's worth 8. And, finally, a 9 + 3 + J = 2. Got it? The best possible hand is worth 9 points.

If either the banker or player have 8 or 9 points, no further cards are dealt to either, and the game ends with the dealer or player winning or in a tie. If the player has an initial total of 0–5 points, the player can draw one additional card.

The banker's play at this point is sufficiently complicated that I'll defer explaining it until next issue. For now, let's just look at how to code these rudiments of Baccarat.

Ready, Mr Bond?

Capturing the Basics of Baccarat

The first piece of the puzzle is pretty straightforward—a shell function that returns a Baccarat value for a given sequence of cards (integer 1–52):

function handValue
{
   handvalue=0  # initialize
   for cardvalue
   do
     if [ $cardvalue -ge 0 ] ; then
       rankvalue=$(( $cardvalue % 13 ))
       case $rankvalue in
         0|11|12 ) rankvalue=0  ;;
         1       ) rankvalue=11 ;;
       esac

       handvalue=$(( $handvalue + $rankvalue ))
     fi
   done

   handvalue=$(( $handvalue % 10 ))
}

This function makes it easy to calculate the value of a hand—whether it will have two or three cards. Here's a typical invocation:

handValue ${player[1]} ${player[2]}

The result is returned as the global variable handvalue, which is calculated by summing up the individual rankvalue of each card.

Dealing the cards is accomplished by initializing things:

initializeDeck
shuffleDeck

Then, here's actually dealing out the cards from newdeck into the player and dealer arrays:

player[1]=${newdeck[1]}
player[2]=${newdeck[3]}
nextplayercard=3        

dealer[1]=${newdeck[2]}
dealer[2]=${newdeck[4]}
nextdealercard=3        

Realistically, if it's a shuffled deck, it would be an identical result to have the first two cards go to the player and the next two dealt to the dealer, but since we're trying to emulate the actual sequence of events at a Baccarat game, I'm dealing cards #1 and #3 to the player and #2 and #4 to the dealer.

The next step is to calculate the value of both the player and dealer hands, which can be done with the handValue function:

handValue ${player[1]} ${player[2]}
playerhandvalue=$handvalue
handValue ${dealer[1]} ${dealer[2]}
dealerhandvalue=$handvalue

Now, let's test to see if we're done with the hand because either player or banker has a hand value of 8 or 9:

if [ $playerhandvalue -ge 8 -o $dealerhandvalue -ge 8 ] ; then
  echo -n "Play is complete. "
  showResult
  exit 0
fi

The showResult function simply calculates (and displays) who won:

function showResult
{

   if [ $dealerhandvalue -gt $playerhandvalue ] ; then
     echo "Dealer wins"
     result=1
   elif [ $dealerhandvalue -lt $playerhandvalue ] ; then
     echo "Player wins"
     result=2
   else
     echo "Tie"
     result=3
   fi
}

I'll stop here, but next column, I'll pick up the task by examining how to test whether the player should get a third card. Then, we'll really dig into the rules for the banker and start running some games!

Note: I have leaned heavily on Wikipedia's information on Baccarat for the rules and history of the game (en.wikipedia.org/wiki/Baccarat). I'm focused on what's called Punto Banco, the so-called North-American-rules Baccarat. If you, like Bond, prefer Baccarat Chemin de Fer or Baccarat Banque, you can tweak things as necessary.

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.