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
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Linux Systems Administrator
- Validate an E-Mail Address with PHP, the Right Way
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Introduction to MapReduce with Hadoop on Linux
- RSS Feeds
- user namespaces
1 hour 50 min ago - yea
2 hours 16 min ago - One advantage with VMs
4 hours 44 min ago - about info
5 hours 17 min ago - info
5 hours 18 min ago - info
5 hours 19 min ago - info
5 hours 21 min ago - info
5 hours 22 min ago - abut info
5 hours 24 min ago - info
5 hours 25 min ago
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