0 Replies - 1128 Views - Last Post: 24 August 2007 - 10:15 AM

#1 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Bashing Blackjack

Posted 24 August 2007 - 10:15 AM

Description: just type in sh /blackjack to start or ./blackjack at the CLI in Linux.A game of 21 that was written 1 1/2 years ago by me, as a bash script. I lost the code until recently when i re-activated the page that was hosting it. Any feedback on the performance, new features, bad design, etc is appreciated.
#! /bin/bash

###########################################################################
#                                                                         #
#     Black Jack Version 1.01 Beta                                        #
#     Written by Shawn Gilligan                                           #
#     Thanks to Dave Taylor for Card Randomizing Logic                    #
#     Feel free to modify or use this code under the terms of the         #
#     GNU Public License Version 2 or higher                              #
#                                                                         #
#                                                                         #
###########################################################################

# Revisions 
# Version 1.01 - Fixed a potential bug with player card coming up as the last card used 
#                in the deck instead of using a new card
#                Also allowed the user to continue playing as long as there is a tie
#                in the amount of times that the user and dealer has won games.
#                Released 3.9.2006
# Version 1.0 -  Initial release - 3.7.2006

# Initializes an empty deck
function initializeDeck
{
     card=1
     while [ $card -lt 53 ]
     do
          deck[$card]=$card
          card=$(( $card + 1 ))
     done
}

# Picks a random card from the deck
function pickCard
{
     local errcount randomcard
     
     threshold=20
     errcount=0

     while [ $errcount -lt $threshold ]
     do
          randomcard=$(( ( $RANDOM % 52 ) + 1 ))
          
          if [ ${deck[$randomcard]} -ne 0 ]; then
               picked=${deck[$randomcard]}
               deck[$picked]=0
               return $picked
          else
               errcount=$(( $errcount + 1 ))
          fi
     done

     randomcard=52
     
     while [ ${deck[$randomcard]} -eq 0 ]
     do
          randomcard=$(( $randomcard - 1 ))
     done

     picked=$randomcard
     deck[$picked]=0
     return $picked
}

# Shuffles the deck
function shuffleDeck
{
     count=1

     while [ $count -le 52 ]
     do
          pickCard
          if [ $picked -ne 0 ]; then
               newdeck[$count]=$picked
          else
               newdeck[$count]="Bad Card"
          fi
          count=$(( $count + 1 ))
     done

     echo "Shuffled the deck ..."
     echo "Verifying Deck ... "
     verifyDeck
     if [ $baddeck -eq 1 ] ; then
         echo "Bad Deck - reshuffling"
         shuffleDeck
     else
         echo "Deck Verified"
     fi
}

# Verifies the shuffled deck
function verifyDeck
{
    count=1
    baddeck=0
    
    #initializes an empty deck
    while [ $count -le 52 ]
    do
      tmpdeck[$count]=0
      count=$(( $count + 1 ))
    done

    count=1
    #loops through the current deck placing the value of card into tmpdeck.  If exists exits and returns baddeck
    while [ $count -le 52 ]
    do
      count2=1
      while [ $count2 -le 52 ]
      do
     if [ ${tmpdeck[$count2]} -eq ${newdeck[$count]} ] ; then
         baddeck=1
         return $baddeck
     fi
     count2=$(( $count2 + 1 ))
      done
      tmpdeck[$count]=${newdeck[$count]}
      count=$(( $count + 1 ))
    done
}

# Shows details of the card picked and gives a value to the card picked (10 for face cards, 11 for aces)
function showCard
{
     card=$1
     isranked=0

     if [ $card -lt 1 -o $card -gt 52 ]; then
          echo "Bad card value: $card"
          exit 1
     fi

     suite="$(( ( ( $card - 1 ) / 13 ) + 1 ))"
     rank="$(( $card % 13 ))"

     if [ $rank -gt 12 ] ; then
          echo "Bad Rank $rank for card $card"
     fi

     case $suite in
          1) suite="Hearts" ;;
          2) suite="Clubs" ;;
          3) suite="Spades" ;;     
          4) suite="Diamonds" ;;
          *) echo "Bad suite value $suite"; exit 1
     esac

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

     cardname="$rank of $suite"
     if [ $isranked -eq 0 ] ; then
          cardvalue=$rank
     fi
}

# Retrieves the next card, if avail, otherwise shuffles and retrieves the first card
function retrieveNextCard
{
     nextCard=$(( $nextCard + 1 ))
     if [ $nextCard -gt 52 ] ; then
          initializeDeck
          shuffleDeck
          echo "Deck Shuffled, do you want to continue after this hand?"
          read choicecont
          if [ $choicecont = "y" -o $choicecont = "Y" ] ; then
               shuffled=0
          else
               shuffled=1
          fi
          nextCard=1
     fi
     
     return $nextCard
}

#
#     Main Program Logic
#

initializeDeck
shuffleDeck

#deck shuffle debugging purpose
#count=1
#while [ $count -le 52 ]
#do
#  showCard ${newdeck[$count]}
#  echo "card $count = $cardname"
#  count=$(( $count + 1 ))
#done
#exit 0
#goodCards="Y"  #used in debugging the shuffle logic

#while [ $count -le 52 ] #same as above
#do
#     showCard ${newdeck[$count]}
#     echo "Card $count = $cardname"
#     count=$(( $count + 1 ))
#done
#if [ $goodCards = "N" -o $goodCards = "n" ] ; then
#     echo "Bad Card Shuffle - aborting"
#     exit 91
#fi

# Initializes beginning values for global vars
nextCard=0
shuffled=0
busted=0
blackjack=0
hitable=0
valuePlr=0
valueDlr=0
dlrWinCt=0
plrWinCt=0
gameTot=0
angame=1
choicecont="Y"

# Starts the dealing
while [ $shuffled -eq 0 -a $angame -eq 1 ]
do
choicehit="n"
blackjack=0
hitable=1
busted=0
valuePlr=0
valueDlr=0
gameTot=$(( $gameTot + 1 ))
retrieveNextCard
showCard ${newdeck[$nextCard]}
card1="$cardname"
valuePlr=$(( $valuePlr + $cardvalue ))
retrieveNextCard
showCard ${newdeck[$nextCard]}
card2="$cardname"
valueDlr=$(( $valueDlr + $cardvalue ))
retrieveNextCard
showCard ${newdeck[$nextCard]}
card3="$cardname"
valuePlr=$(( $valuePlr + $cardvalue ))
retrieveNextCard
showCard ${newdeck[$nextCard]}
card4="$cardname"
valueDlr=$(( $valueDlr + $cardvalue ))

# Calculates the main player information
echo "** Player's Hand **: $card1, $card3"
echo -n "   ** Player's Total **: $valuePlr "
if [ $valuePlr -eq 21 ] ; then
     echo " Blackjack!!! "
     blackjack=1
else
     if [ $valuePlr -gt 21 ] ; then
          valuePlr=11
     fi
     echo
fi
# if value more than 19, hit is not an option
if [ $valuePlr -gt 19 ] ; then
     hitable=0
fi
# if value is less than or equal to 12 then hit is not an option and is forced
while [ $valuePlr -le 12 ]
do
     echo " Player must Hit - Drawing another card "
     retrieveNextCard
     showCard ${newdeck[$nextCard]}
     valuePlr=$(( $valuePlr + $cardvalue ))
     if [ $cardvalue -eq 11 ] ; then
          if [ $valuePlr -gt 21 ] ; then
               valuePlr=$(( $valuePlr - 10 ))
          fi
     fi
     echo " Player drew a $cardname new value is: $valuePlr " 
     if [ $valuePlr -gt 21 ] ; then
          echo " *** Player Busted ! *** "
          busted=1
          hitable=0
     fi
     if [ $valuePlr -eq 21 ] ; then
          echo " Blackjack!!! "
          hitable=0
     fi
     if [ $valuePlr -gt 16 ] ; then
          hitable=0
     fi
done
# if dealer shows blackjack, hand is over and dealer wins.
if [ $valueDlr -eq 21 ] ; then
     echo "Unable to hit - dealer shows blackjack "
     hitable=0
fi
# if hitable, give option to do so
while [ $hitable -eq 1 ] 
do
     if [ $choicehit = "n" ] ; then
          echo -n " Dealer is showing a $card2 "
     fi
     echo " Do you want to hit? "
     read choicehit
     if [ $choicehit = "y" -o $choicehit = "Y" ] ; then
          retrieveNextCard
          showCard ${newdeck[$nextCard]}
          valuePlr=$(( $valuePlr + $cardvalue ))
          echo " Player drew a $cardname newvalue is: $valuePlr "
          if [ $valuePlr -gt 21 ] ; then
               if [ $cardvalue -eq 11 ] ; then
                    valuePlr=$(( $valuePlr - 10 ))
                    echo " Player drew an ace, considering it 1 instead of 11 "
                    echo " New value is $valuePlr "
               else
                    echo " *** Player Busted - On his/her own *** "
                    busted=1
               fi
          fi
          if [ $valuePlr -eq 21 ] ; then
               echo " *** Great Job - BlackJack!!! *** "
          fi
     else
          hitable=0
     fi
     if [ $valuePlr -gt 19 ] ; then
          hitable=0
     fi
done

# Shows and calculates dealer information

echo "** Dealer's Hand **: $card2, $card4"
echo -n "   **Dealer's Total **: $valueDlr "
if [ $valueDlr -eq 21 ] ; then
     echo " Blackjack!!! "
else
     if [ $valueDlr -gt 21 ] ; then
          valueDlr=11
          echo " Dealer drew 2 aces, counting one as 1 instead of 11 "
     fi
     echo
fi

# If player has busted or black jack already, no option to hit and hand stands as is
if [ $busted -eq 0 ] ; then
     if [ $blackjack -eq 0 ] ; then
          # otherwise if dealer has less than 17 forced hit
          while [ $valueDlr -lt 17 ]
          do
               echo " Dealer under 17 - must hit - Drawing another card "
               retrieveNextCard
               showCard ${newdeck[$nextCard]}
               valueDlr=$(( $valueDlr + $cardvalue ))
               echo " Dealer drew a $cardname new value is: $valueDlr"
               if [ $valueDlr -gt 21 ] ; then
                    if [ $cardvalue -eq 11 ] ; then
                         valueDlr=$(( $valueDlr - 10 ))
                         echo " Dealer drew an ace, counting as 1 instead of 11 "
                         echo " New value is $valueDlr "
                    else
                         echo " Dealer Busted! "
                    fi
               fi
               if [ $valueDlr -eq 21 ] ; then
                    echo " Blackjack!!! "
               fi
          done
     fi
fi

# Calculate the winner, if any
if [ $valuePlr -eq $valueDlr ] ; then
     echo " *** Neither Wins - Bust *** "
else
     if [ $valuePlr -gt $valueDlr ] ; then
          if [ $valuePlr -le 21 ] ; then
               echo " *** Player Wins *** "
               plrWinCt=$(( $plrWinCt + 1 ))
          else
               echo " *** Dealer Wins *** " 
               dlrWinCt=$(( $dlrWinCt + 1 ))
          fi
     else
          if [ $busted -eq 1 ] ; then
               echo " *** Dealer Wins - Player busted ! *** "
               dlrWinCt=$(( $dlrWinCt + 1 ))
          else
               if [ $valueDlr -lt 22 ] ; then
                    echo " *** Dealer Wins *** "
                    dlrWinCt=$(( $dlrWinCt + 1 ))
               else
                    echo " *** Player Wins *** "
                    plrWinCt=$(( $plrWinCt + 1 ))
               fi
          fi
     fi
fi
if [ $shuffled -eq 1 ] ; then
    if [ $plrWinCt -eq $dlrWinCt ] ; then
     echo "The game win counts for both player and dealer are the same, do you wish to continue?"
     read choicegame
     if [ $choicecont = "y" -o $choicecont = "Y" ] ; then
         angame=1
     else
         angame=0
     fi
    fi
fi
done

# Give status of games and exit
echo "Status: Dealer won $dlrWinCt time(s) and Player won $plrWinCt time(s) and a total of $gameTot game(s) played "

exit 0


Is This A Good Question/Topic? 0
  • +

Page 1 of 1