#!/bin/bash # cards.sh # Deals four random hands from a deck of cards. UNPICKED=0 PICKED=1 DUPE_CARD=99 LOWER_LIMIT=0 UPPER_LIMIT=51 CARDS_IN_SUIT=13 CARDS=52 declare -a Deck declare -a Suits declare -a Cards # It would have been easier to implement and more intuitive #+ with a single, 3-dimensional array. # Perhaps a future version of Bash will support multidimensional arrays. initialize_Deck () { i=$LOWER_LIMIT until [ "$i" -gt $UPPER_LIMIT ] do Deck[i]=$UNPICKED # Set each card of "Deck" as unpicked. let "i += 1" done echo } initialize_Suits () { Suits[0]=C #Clubs Suits[1]=D #Diamonds Suits[2]=H #Hearts Suits[3]=S #Spades } initialize_Cards () { Cards=(2 3 4 5 6 7 8 9 10 J Q K A) # Alternate method of initializing an array. } pick_a_card () { card_number=$RANDOM let "card_number %= $CARDS" # Restrict range to 0 - 51, i.e., 52 cards. if [ "${Deck[card_number]}" -eq $UNPICKED ] then Deck[card_number]=$PICKED return $card_number else return $DUPE_CARD fi } parse_card () { number=$1 let "suit_number = number / CARDS_IN_SUIT" suit=${Suits[suit_number]} echo -n "$suit-" let "card_no = number % CARDS_IN_SUIT" Card=${Cards[card_no]} printf %-4s $Card # Print cards in neat columns. } seed_random () # Seed random number generator. { # What happens if you don't do this? seed=`eval date +%s` let "seed %= 32766" RANDOM=$seed } # Consider other methods of seeding the random number generator. deal_cards () { echo cards_picked=0 while [ "$cards_picked" -le $UPPER_LIMIT ] do pick_a_card t=$? if [ "$t" -ne $DUPE_CARD ] then parse_card $t u=$cards_picked+1 # Change back to 1-based indexing, temporarily. Why? let "u %= $CARDS_IN_SUIT" if [ "$u" -eq 0 ] # Nested if/then condition test. then echo echo fi # Each hand set apart with a blank line. let "cards_picked += 1" fi done echo return 0 } # Structured programming: # Entire program logic modularized in functions. #=============== seed_random initialize_Deck initialize_Suits initialize_Cards deal_cards #=============== exit # Exercise 1: # Add comments to thoroughly document this script. # Exercise 2: # Add a routine (function) to print out each hand sorted in suits. # You may add other bells and whistles if you like. # Exercise 3: # Simplify and streamline the logic of the script.