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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Developer Poll
- Dart: a New Web Programming Experience
- May 2013 Issue of Linux Journal: Raspberry Pi
- What's the tweeting protocol?
- Reply to comment | Linux Journal
51 min 8 sec ago - Reply to comment | Linux Journal
1 hour 37 min ago - Web Hosting IQ
3 hours 11 min ago - Thanks for taking the time to
4 hours 48 min ago - Linux is good
6 hours 46 min ago - Reply to comment | Linux Journal
7 hours 3 min ago - Web Hosting IQ
7 hours 33 min ago - Web Hosting IQ
7 hours 33 min ago - Web Hosting IQ
7 hours 34 min ago - Reply to comment | Linux Journal
10 hours 35 min ago
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




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