How to begin developing a computer version of the popular card game.
Between the previous 163 columns I've written here in Linux Journal and the dozens of games I programmed and explored during the creation of my Wicked Cool Shell Scripts book, I've written a lot of Bash shell games. The challenge is to find one that's simple enough where a shell script will work, but isn't so simple that it ends up being only a half-dozen lines.
Magic 8-Ball is a perfect example. It turns out that the entire "predict the future" gizmo was really just a 20-sided die floating in dark purple fluid. So an array of 20 possible values and a random number selector and boom—you've got a magic 8-ball script:
#!/bin/sh
# magic 8 ball. Yup. Pick a random number, output message
# messages harvested from the Wikipedia entry
answers=("It is certain." "It is decidedly so."
"Without a doubt." "Yes - definitely."
"You may rely on it." "As I see it, yes." "Most likely."
"Outlook good." "Yes." "Signs point to yes."
"Reply hazy, try again." "Ask again later."
"Better not tell you now." "Cannot predict now."
"Concentrate and ask again." "Don't count on it."
"My reply is no." "My sources say no."
"Outlook not so good." "Very doubtful.")
echo "Oh! Magic 8 Ball, Please Tell Me True..." ; echo ""
/bin/echo -n "What is your question? "
read question
answer=$(( $RANDOM % 20 ))
echo ""
echo "I have looked into the future and I say: "
echo " ${answers[$answer]}" ; echo ""
exit 0
Let's do a quick run to see if I'm the most popular LJ writer:
$ sh magic8.sh
Oh! Magic 8 Ball, Please Tell Me True...
What is your question? Am I the most popular LJ writer?
I have looked into the future and I say:
My reply is no.
Ouch, that's harsh. I write the darn divination program, and it just drops a brick on my foot. Yeesh.
More seriously, Magic 8 Ball is too simple to make an interesting shell script. By contrast, Call of Duty is way too complex, even if I did a version with text output instead of gorgeously rendered 3D graphics.
Card Game Function Library
That's why card games prove to be good as programming challenges or exercises: the core mechanism of a 52-card random deck is pretty straightforward, so it's all about the actual cardplay.
Not only that, but as I've written before about card games as shell scripts, I already have a handy set of functions to create, shuffle and display cards out of a deck. If you want to rummage in the archives, I've tackled Acey-Deucey, Baccarat and some bits and pieces of Cribbage.
In order to jump right into the new game that I'm going to describe how to build, Go Fish!, let's steal the following functions from my earlier scripts:
from Linux Journal - The Original Magazine of the Linux Community https://ift.tt/2K6APJ2
via IFTTT
No comments:
Post a Comment