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.

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.

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):

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:

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.

Comment viewing options

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