Solved

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

Posted on 2013-12-03
4
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.

749 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