Work the Shell - End Game
This is the last column in our Blackjack series, and in this column, I show the final snippets of code needed to weave all of the disparate pieces of the game script into a playable game. For obvious reasons, I can't present the entire script here in the magazine (it's almost 300 lines long), so instead I highly encourage you to pop over to the Linux Journal FTP site and grab a copy of the script as you read along (ftp.linuxjournal.com/pub/lj/listings/issue148/9051.tgz).
As with many betting games, Blackjack has evolved to have many esoteric rules with splitting pairs, insurance and various other things that take something relatively simple and make it more complex. We'll ignore all of that, however, and also ignore the betting component of the game too (this is a [geek] family magazine, after all) and just focus on the game play.
Therefore, the first thing we need to know is that the player can see both cards as dealt and one of the two cards that the dealer deals for itself. That's the first piece of code we need to add, and because we aren't allowing insurance or betting, it needs to be included immediately after the tests for blackjack in the code:
echo -n "** Dealer's hand: "
showCard ${dealer[1]} ; echo -n "$cardname, "
echo "(card face down)"
echo ""
From a strategic perspective, if you have even a rudimentary grasp of probability, you'll know that cards with a value of ten are far more likely than any other card value in the deck. If the dealer has an eight or nine showing, for example, odds are very good that it has 18 or 19 as a hand value. That'll, therefore, change your own playing strategy too, perhaps making it more likely that you'd take another card if you have an interim hand value of 17; whereas, if the dealer had a five showing, for example, you might be more likely to stick with a hand value of 16.
In previous columns, you've already seen the basic script to display and calculate a hand's value, so this is nothing new:
echo -n "** Dealer's hand: "
showCard ${dealer[1]} ; echo -n "$cardname, "
showCard ${dealer[2]} ; echo "$cardname"
handValue ${dealer[1]} ${dealer[2]}
But, now we're going to add a loop below this that will keep taking cards until the hand value is 17 or higher (that's standard Las Vegas Blackjack rules: 16 and lower the dealer “hits” or takes another card, and 17 or higher the dealer “stands” or sticks with its hand).
This is a bit tricky, so take your time reading it:
while [ $handvalue -lt 17 ]
do
dealer[$nextdealercard]=${newdeck[$nextcard]}
showCard ${dealer[$nextdealercard]}
nextcard=$(( $nextcard + 1 ))
nextdealercard=$(( $nextdealercard + 1 ))
echo """ ; echo "** Dealer takes: $cardname"
handValue ${dealer[1]} ${dealer[2]} ${dealer[3]} \
${dealer[4]} ${dealer[5]}
done
With some good routines and variables already in place, it turns out to be surprisingly succinct to have the dealer play its hand out. Hurray for that bit of good design!
Now that we have the game-play logic, it's simply a matter of having the set of conditionals to figure out who won or whether the game ended with a tie:
if [ $handvalue -gt 21 ] then echo "**** Dealer busted! Player wins with \ $playerhandvalue!" playerwin=$(( $playerwin + 1 )) elif [ $handvalue -eq $playerhandvalue ] then echo "**** Dealer and player tie with \ $handvalue points." elif [ $handvalue -lt $playerhandvalue ] then echo "**** Player wins with $playerhandvalue" playerwin=$(( $playerwin + 1 )) else echo "**** Dealer wins with $handvalue" dealerwin=$(( $dealerwin + 1 )) fi
Whaddya think? Enough code listings?
Let's see how the game plays now:
$ sh blackjack.sh **** Welcome to Blackjack.sh! **** Dealer's hand: Queen of Hearts, (card face down) You've been dealt: 2 of Spades, 7 of Hearts H)it or S)tand? (recommended: hit) hit You've been dealt: 10 of Diamonds H)it or S)tand? (recommended: stand) stand You stand with a hand value of 19 Dealer's hand: Queen of Hearts, 4 of Hearts Dealer takes: Queen of Clubs **** Dealer busted! Player wins with 19!
You can see that we had 2S and 7H (9 points), took a card, 10D, giving us 19 points. We stayed with that, and the dealer revealed that it had QH 4H, 14 points, and took another card which proved to be QC, which took the dealer over 21 points. We won!
There are a few more nuances to the program, including keeping track of how many times the dealer or player wins, and at the point where you're asked “hit or stand”, you now can type in quit (or q) to quit. Then, it'll show you:
H)it or S)tand? (recommended: stand) q Player quits. Standings: dealer wins: 5 and player wins: 3
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 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Reply to comment | Linux Journal
1 hour 11 min ago - Reply to comment | Linux Journal
1 hour 27 min ago - Favorite (and easily brute-forced) pw's
3 hours 19 min ago - Have you tried Boxen? It's a
9 hours 11 min ago - seo services in india
13 hours 42 min ago - For KDE install kio-mtp
13 hours 43 min ago - Evernote is much more...
15 hours 43 min ago - Reply to comment | Linux Journal
1 day 28 min ago - Dynamic DNS
1 day 1 hour ago - Reply to comment | Linux Journal
1 day 2 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!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
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
Some improvements
Here is one solution for the Ace problem. Note that it increases the probability to exceed the hard coded value of 5 cards in a hand.
The other stuff are just to make the program run smoother.
/freefox
> aces=0 # number of Ace in the hand
107,108c108,110
< 0|11|12 ) rankvalue=10 ;;
< 1 ) rankvalue=11 ;;
---
> 0|11|12 ) rankvalue=10 ;;
> 1 ) rankvalue=11
> aces=$(( $aces + 1 )) ;;
111a114,118
>
> if [ $handvalue -gt 21 ] && [ $aces -gt 0 ] ; then
> handvalue=$(( $handvalue - 10 ))
> aces=$(( $aces - 1 ))
> fi
123a131,133
> echo ""
> echo "------ $dealerwin <-- dealer -- player --> $playerwin ------"
>
156c166,167
< exit 0
---
> dealerwin=$(( $dealerwin + 1 ))
> continue
162c173,174
< exit 0
---
> dealerwin=$(( $dealerwin + 1 ))
> continue
166c178,179
< exit 0
---
> playerwin=$(( $playerwin + 1 ))
> continue
197,198c210,215
< if [ "$answer" = "stand" -o "$answer" = "s" ] ; then
< break
---
> ## To go for the obious choice, when no answer,
> ## the recommended value are analysed ones more.
> if [ "$answer" = "" ] && [ $handvalue -gt 15 ] ; then
> break
> elif [ "$answer" = "stand" -o "$answer" = "s" ] ; then
> break
222c239
< break
---
> continue
271,273d287
< echo ""
< echo "---------------------------------------------------"
<
What about a licens?
It's a gaim. It's code. It's been published.
You encourage downloads. You ask for updates.
Why not put a licens on it? GPL?
/freefox