Work the Shell - 007's Favorite Game: "Baccarat"?

August 1st, 2007 by Dave Taylor in

Create a shell script to get a taste of being Bond, James Bond.
Your rating: None

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.

__________________________


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Jeff Waren's picture

A Baccara Shell Game

On December 3rd, 2007 Jeff Waren (not verified) says:

I think it's pretty amazing to be able to make a baccarat game using the shell. To be honest, i would not have been able to make a shell function that returns a Baccarat value. What is great about all this is that it makes us work with a "limited" coding language and try to make stuff that we would be able to make in 20-30 minutes in C++ ;)
The rpg gamer,
Jeff Waren

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

July 2009, #183

News Flash: Linux Kernel 3.0 to include an on-the-go Expresso machine interface! Ok, maybe not, but Linux is definitely going mobile, from phones to e-readers. Find out more inside about Android, the Kindle 2, the Western Digital MyBook II, The Bug, and Indamixx (a portable recording studio). And if you've gone mobile and you been wanting more Emacs in your life then check out Conkeror.


To compliment the mobile we've got the stationary: parsing command line options with getopt, checking your Ruby code with metric_fu, and building a secure Squid proxy. How is this stationary you ask? What can we say? It's not. We just wanted to see if anybody actually read this part of the page :) .


All this and more, and all you have to do is get your hot sweaty hands on the latest copy of Linux Journal.





Read this issue