Work the Shell - End Game
July 1st, 2006 by Dave Taylor in
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.ssc.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
There's also one outstanding bug in the code, and I invite you to dig around and figure it out. If the dealer is dealt an Ace in play, it's automatically counted as 11 points, not one or 11, as it should be. Your challenge: figure out where that problem arises and how to fix it. If you think you know, e-mail me your proposed solution (perhaps a diff of your version versus the version on the Linux Journal FTP site), and we'll see how you did.
Next month, we'll crack open a completely different shell script task and see what we can do to help your day-to-day Linux administrative tasks, because we've probably wasted plenty enough ink on a casino game for this year! See ya then.
Subscribe now!
Breaking News
| The C in CNET Stands for CBS | 9 hours 43 min ago |
| Charter Trades Privacy for Pocketbook | 1 day 13 hours ago |
| SSL Glitch Unlocks Debian, Ubuntu, & Others | 2 days 13 hours ago |
| MySpace Cashes in Spam to the Tune of $234 Million | 2 days 15 hours ago |
Featured Video
Linux Journal Gadget Guy, Shawn Powers, takes us through installing Ubuntu on a machine running Windows with the Wubi installer.
Delicious
Digg
Reddit
Newsvine
Technorati






Some improvements
On August 2nd, 2006 freefox says:
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?
On July 31st, 2006 freefox says:
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