Work the Shell - Calling All Functions, and Some Math Too!

 in
Shuffle off to the next step in creating a shell script Blackjack game.

and:

case $rank in
 0 ) rank="King"    ;;
 1 ) rank="Ace"     ;;
 11) rank="Jack"    ;;
 12) rank="Queen"  ;;
esac

Put these together in a function called showCard (which returns “$cardname” as the calculated rank and suite), and we can now clean up a bit:

initializeDeck shuffleDeck

echo -n "** Player's hand: "
  showCard ${newdeck[1]} ; echo -n "$cardname, "
  showCard ${newdeck[3]} ; echo "$cardname"

echo -n "** Dealer's hand: "
  showCard ${newdeck[2]} ; echo -n "$cardname, "
  showCard ${newdeck[4]} ; echo "$cardname"

Now we can start to see the game come together! Consider:

$ ./blackjack.sh
** Player's hand: 8 of Clubs, 3 of Diamonds
** Dealer's hand: King of Spades, 3 of Spades
$ ./blackjack.sh
** Player's hand: 2 of Spades, 4 of Spades
** Dealer's hand: 10 of Spades, 4 of Hearts

Let's stop here this month as that's a lot of code to dig through already. I invite you to pop over to the LJ FTP site to grab all the source code so far, so you can experiment with this script yourself too.

Next month, we'll start looking at the game logic itself, but for now, Vegas beckons for a big tradeshow and, well, I can write it off as research for Linux Journal, can't I?

Dave Taylor is a 26-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 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.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

SUIT/SUITE

Anonymous's picture

AARGH - four SUITs of cards... a SUITE of rooms... where's your editor!

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

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.

Learn More

Sponsored by ActiveState