Solved

Simple blackjack card count - Shell (Help me please!)

Posted on 2013-12-03
4
345 Views
Last Modified: 2013-12-04
Hi Guys,

New here and wondering if you could give me some help with this simple black jack card count im trying to troubleshoot.
So far it just loops without reading the card value's properly however the X to exit function is working.

Any help will be greatly appreciated


 
#!/bin/sh
#by Anthony Challis
#on the 27/11/2013

echo "Blackjack PEW PEW"
echo "If you would like to exit hit X on each card"
echo "Pick a card"                                                            # echo pick a card
read CARD                                                                     # get card
echo "Pick another card"                                                # echo pick another card      
read CARD2                                                            #g et sec card
while ([ $CARD != X ]) || ([ $CARD2 != X ])                                     # if CARD and CARD2 do not = to X follow next line
do
case $CARD in                                                            # array card
"A") VALUE=11                                                            #ace =11
      ;;
"2") VALUE=2                                                            # 2=2 and THE REST
      ;;
"3") VALUE=3            
      ;;
"4") VALUE=4
      ;;      
"5") VALUE=5
      ;;
"6") VALUE=6
      ;;
"7") VALUE=7
      ;;
"8") VALUE=8
      ;;
"9") VALUE=9
      ;;
"10") VALUE=10
      ;;
"J") VALUE=10                                                            # jack = 10
      ;;
"Q") VALUE=10                                                            #queen = 10
      ;;
"K") VALUE=10                                                            #king = 10
      ;;
*) VALUE=0
                                                                  #others = 0      
echo "INVAILD OPTION"
      ;;
esac                                                                  #calculate

case $CARD2 in                                                             #array card2
"A") VALUE2=11                                                            #ace =11
      ;;
"2") VALUE2=2                                                            # 2=2 and etc
      ;;
"3") VALUE2=3            
      ;;
"4") VALUE2=4
      ;;      
"5") VALUE2=5
      ;;
"6") VALUE2=6
      ;;
"7") VALUE2=7
      ;;
"8") VALUE2=8
      ;;
"9") VALUE2=9
      ;;
"10") VALUE2=10
      ;;
"J") VALUE2=10                                                            # jack = 10
      ;;      
"Q") VALUE2=10                                                            #queen = 10
      ;;
"K") VALUE2=10                                                            #king = 10
      ;;
*) VALUE2=0                                                            #others = 0
            
echo "INVAILD OPTION"
      ;;
esac                                                                  #calculate

if ([ $VALUE -eq 11 ]) && ([ $VALUE2 -eq 11 ])                                    #if VALUE and VALUE2 = 11 then
then
VALUE=1                                                                  #make VALUE = 1
fi                                                                  #end if
echo "You pick on Card 1 is $VALUE"
echo "You pick on Card 2 is $VALUE2"
GAME=$(($VALUE + $VALUE2))                                                #sum the value and value 2
clear                                                                  #break
echo "You Scored $GAME"
clear                                                                  #break
echo "Blackjack"
echo "If you would like to exit, press X on each Card"
echo "Pick a card"                                                      # echo pick a card
read CARD                                                            # get card
echo "Pick another card"                                                  #echo pick another card      
read CARD2                                                            #get sec card

done                                                                  #end of the loop

Thanks,
0
Comment
Question by:stellarsystems
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39694576
It seems to work for me. (although it annoyingly clears the screen while I'm trying to read the score)
What problem are you having?
0
 
LVL 19

Accepted Solution

by:
xterm earned 500 total points
ID: 39694590
After the line echo "You Scored $GAME" you have a clear statement - comment that out, and it will show you your score.
0
 
LVL 19

Assisted Solution

by:xterm
xterm earned 500 total points
ID: 39694624
Here is a slightly different spin on it using an array of values instead, maybe it can give you some ideas and direction:

#!/bin/sh

if [ x$1 != xnotitle ]; then echo "Blackjack PEW PEW"; fi
echo "CTRL-C to exit"
echo "Pick a card"              # echo pick a card
read CARD1                      # get first card
echo "Pick another card"        # echo pick another card
read CARD2                      # get second card

values=(
        A=11 1=1 2=2 3=3 4=4 5=5 6=6 7=7 8=8 9=9 10=10 J=11 Q=11 K=11
       )

score=0
while [ "x${values[count]}" != "x" ]
do
        key=`echo ${values[$count]} | cut -d= -f1`      # key is the first value in the array pair
        value=`echo ${values[$count]} | cut -d= -f2`    # value is the second value in the array pair
        CARD1=`echo $CARD1 | tr [a-z] [A-Z]`            # Make inputs case-insensitive by lowering all
        CARD2=`echo $CARD2 | tr [a-z] [A-Z]`            # Make inputs case-insensitive by lowering all
        if [ x$CARD1 = x$key ]; then
                echo "First card '$CARD1' is worth $value"
                score=$(( $score + $value ))            # add the value to the total score
        fi
        if [ x$CARD2 = x$key ]; then
                echo "Second card '$CARD2' is worth $value"
                score=$(( $score + $value ))            # add the value to the total score
        fi
        count=$(( $count + 1 ))
done

echo "You scored $score"
echo ""
$0 notitle

Open in new window

0
 
LVL 1

Author Closing Comment

by:stellarsystems
ID: 39694821
Thanks, i removed the clear statement and it is working fine also the array value used in second example is awesome
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now