Work the Shell - Writing a Shell Game
We've spent the last three columns talking about the basic nuts and bolts of shell script programming, so I think it's time to start digging into a real shell script, and build something interesting and useful. Well, interesting, at least!
What I would like to do—and up front I admit that this might be a crazy hard problem for a shell script—is to try to write a rudimentary Blackjack game. It's simple enough that it should be manageable, but it's hard enough that we'll really have to flex our scripting muscle to get everything working. Needless to say, it won't have a fancy graphical interface!
We'll go into the specific rules of Blackjack as needed, but for now all you need to know about Blackjack is that each player gets two cards from a deck of standard playing cards, and that players can then request additional cards, trying to get their point total as close to 21 as possible, without going over that value. All face cards are worth 10 points each, and an Ace is worth 1 or 11, depending on how the player wants it to count.
The first challenge is to create a virtual deck of cards, but this is easier than you might think, because it can be represented simply by an array of 52 elements, with the first 13 representing one suit, the second 13 a second suit and so forth. So, card #37 might be a Jack of Hearts, for example.
It turns out that shell scripts can use arrays, so let's start by creating a 52-element array and populating it with the values 1–52:
card=1 while [ $card -lt 53 ] do deck[$card]=$card card=$(( $card + 1 )) done
If you're used to Perl, you might be thinking that a for loop would be a more logical choice for this sort of task, but for loops in shell scripts lack the ability to step through a range of values. Arrays in the Bourne Shell are easy to work with: simply specify a reference index and the array will be grown to that size dynamically.
Now we have a representation of a deck of cards, but it's perfectly sorted, so the next step is to write some code that will shuffle the deck. This proves to be a bit more tricky, as you might expect!
The basic idea is that we'll randomly pick a number between 1 and 52, and then see if its card is available or not. So the initial deck we created that's sorted is used as the source for the shuffled deck, which will actually end up in a new array. Here's the basic piece of code for the random card selection:
while [ $errcount -lt $threshold ]
do
randomcard=$(( ( $RANDOM % 52 ) + 1 ))
errcount=$(( $errcount + 1 ))
if [ ${deck[$randomcard]} -ne 0 ] ; then
picked=${deck[$randomcard]}
deck[$picked]=0 # picked, remove it
return $picked
fi
done
There's a lot to see here, but let's talk about the basic logic first: although we're going to pick a card randomly between 1 and 52, and then see if it has already been picked, we also need to make sure we don't end up trapped in an infinite loop because of a mediocre random number function. That's managed by keeping track of the number of guesses you have to make with the variable errcount. The threshold can be adjusted to allow more or fewer guesses for each card. I have it set to 10 as a default value.
You can see that working with arrays makes variable references quite a bit more tricky. Setting the value isn't too bad, as shown earlier, but referencing the array requires the addition of curly braces, so the reference to ${deck[$randomcard]} is to the randomcard slot in the array deck.
Otherwise, don't let all the notation distract you as this is a fairly straightforward loop. Try threshold times to pick a card randomly out of the array deck that hasn't already been chosen (for example, had its value set to zero rather than the initialized value).
The other interesting piece of this code block is the RANDOM variable. Every time you reference $RANDOM, you get a different number between zero and MAXINT (a very large integer value), automatically, without having to initialize anything or do any special work. Try it yourself by typing echo $RANDOM at the Bourne Again Shell command prompt.
This isn't the full code segment, because we also need to have a fall-through, a block of code that is used when the random guesses don't produce a desired card and we instead need to step through the array deck linearly to find one that's available. Typically, it'd be used only at the very end of the shuffle when there are only a few cards left. This code looks like:
randomcard=1
while [ ${deck[$randomcard]} -eq 0 ]
do
randomcard=$(( $randomcard + 1 ))
done
picked=$randomcard
deck[$picked]=0 # picked, remove it
return $picked
This should be even easier to read now that you're becoming familiar with arrays.
I'm going to stop here for this month, and we'll pick up the card shuffling task again next month, including an explanation of how to make it a shell function and utilize it in the main game script itself. Stay tuned!
Dave Taylor is a 25-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.
Dave Taylor is the author of the popular Work the Shell column in Linux Journal.
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- 100% disappointed with the decision to go all digital.
- Readers' Choice Awards 2011
- Parallel Programming with NVIDIA CUDA
- Validate an E-Mail Address with PHP, the Right Way
- You Need A Budget
- Why Python?
- The Linux powered LAN Gaming House
- Linux-Based X Terminals with XDMCP
- Short Notices: News In Linux Audio
- buena información
2 hours 46 min ago - One important "bucket" that I didn't note (désolé si qqun deja d
3 hours 47 min ago - Gnome3 is such a POS. No one
13 hours 14 min ago - Gnome 3 is the biggest POS
13 hours 25 min ago - I didn't knew this thing by
19 hours 29 min ago - Author's reply
22 hours 53 min ago - Link to modlys
1 day 42 sec ago - I use YNAB because of the
1 day 12 min ago - Search
1 day 5 hours ago - Question
1 day 5 hours ago






Comments
while or for ?
I can see how you'd like to use a while loop in your code, after all it's about shell scripting. However I think that in order to really unleash the full power of the shell one needs to know about the tools that are available. You missed an opportunity to introduce a useful tool in this column--seq.
You're while loop could have been written as a for loop using seq:
for card in `seq 52`; do
deck[$card]=$card
done
Why not use for?
Why not use a for loop to increment the numbers?
for ((card=1;card<=52;card++));do
deck[$card]=$card
done