Work the Shell - 007's Favorite Game: "Baccarat"?
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.
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?
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.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Designing Electronics with Linux
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
- Kernel Problem
4 hours 29 min ago - BASH script to log IPs on public web server
8 hours 56 min ago - DynDNS
12 hours 31 min ago - Reply to comment | Linux Journal
13 hours 4 min ago - All the articles you talked
15 hours 27 min ago - All the articles you talked
15 hours 30 min ago - All the articles you talked
15 hours 32 min ago - myip
19 hours 57 min ago - Keeping track of IP address
21 hours 48 min ago - Roll your own dynamic dns
1 day 3 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
A Baccara Shell Game
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