Skip to content

Instantly share code, notes, and snippets.

@jessebutryn
Last active September 10, 2017 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessebutryn/c5235814bb1c5a5a91a7646a0d1f8576 to your computer and use it in GitHub Desktop.
Save jessebutryn/c5235814bb1c5a5a91a7646a0d1f8576 to your computer and use it in GitHub Desktop.
Blackjack game
#! /usr/local/bin/bash -
#
TXT_BLD=$(tput bold)
TXT_YLW=$(tput setaf 3)
TXT_RED=$(tput setaf 1)
TXT_GRN=$(tput setaf 2)
TXT_WARN="${TXT_BLD}${TXT_YLW}"
TXT_ERR="${TXT_BLD}${TXT_RED}"
TXT_OKAY="${TXT_BLD}${TXT_GRN}"
TXT_RST=$(tput sgr0)
L_BREAK='------------------------------------------------------------'
PS3="${TXT_OKAY}[BlackJack 1.0]${TXT_RST}: "
BJ_CARDS=('❶ One (1)' '❷ Two (2)' '❸ Three (3)' '❹ Four (4)' '❺ Five (5)' '❻ Six (6)' '❼ Seven (7)' '❽ Eight (8)' '❾ Nine (9)' '❿ Ten (10)' '♘ Jack (10)' '♕ Queen (10)' '♔ King (10)' '❖ Ace (1/11)' '❖ Ace (1)' '❖ Ace (11)')
randnum_p () {
RANDOM_NUM=$[$RANDOM % 14]
if [[ "$RANDOM_NUM" = '13' ]]; then
echo "You have been dealt an Ace! Do you want this to be a one or an eleven?" >/dev/tty
select choice in One Eleven; do
case $choice in
One) RANDOM_NUM=14; break;;
Eleven) RANDOM_NUM=15; break;;
esac
done >/dev/tty
echo "$RANDOM_NUM"
else
echo "$RANDOM_NUM"
fi
}
randnum_d () {
RANDOM_NUM=$[$RANDOM % 14]
if [[ "$RANDOM_NUM" = '13' ]]; then
RANDOM_NUM=14
echo "$RANDOM_NUM"
else
echo "$RANDOM_NUM"
fi
}
player.deal () {
CURRENT_CARDS=$(echo ${#CARD_PLAYER[@]})
if [[ "$CURRENT_CARDS" -ge '1' ]]; then
ARRAY_INDEX=$(($CURRENT_CARDS+1))
else
ARRAY_INDEX=0
fi
if [[ "$ARRAY_INDEX" = '0' ]]; then
CARD_PLAYER[0]=${BJ_CARDS[$(randnum_p)]}
echo -e "Your first card is:\t${TXT_BLD}$(echo ${CARD_PLAYER[0]} | awk '{print $0}')${TXT_RST}"
CARD_PLAYER[1]=${BJ_CARDS[$(randnum_p)]}
echo -e "Your second card is:\t${TXT_BLD}$(echo ${CARD_PLAYER[1]} | awk '{print $0}')${TXT_RST}"
else
CARD_PLAYER[$ARRAY_INDEX]=${BJ_CARDS[$(randnum_p)]}
echo -e "Your next card is:\t${TXT_BLD}$(echo ${CARD_PLAYER[$ARRAY_INDEX]} | awk '{print $0}')${TXT_RST}"
fi
player.total
}
player.total () {
PLAYER_TOTAL=0
for i in "${CARD_PLAYER[@]}"; do
CARD_VALUE=$(echo $i | tr -d '()' | awk '{print $3}')
PLAYER_TOTAL=$(($PLAYER_TOTAL+$CARD_VALUE))
done
echo -e "Your currently have:\t${TXT_OKAY}$PLAYER_TOTAL${TXT_RST}"
}
dealer.deal () {
CURRENT_CARDS=$(echo ${#CARD_DEALER[@]})
if [[ "$CURRENT_CARDS" -ge '1' ]]; then
ARRAY_INDEX=$(($CURRENT_CARDS+1))
else
ARRAY_INDEX=0
fi
if [[ "$ARRAY_INDEX" = '0' ]]; then
CARD_DEALER[0]=${BJ_CARDS[$(randnum_d)]}
echo -e "Dealer receives face down card."
CARD_DEALER[1]=${BJ_CARDS[$(randnum_d)]}
echo -e "Dealers second card is:\t${TXT_BLD}$(echo ${CARD_DEALER[1]} | awk '{print $0}')${TXT_RST}"
else
CARD_DEALER[$ARRAY_INDEX]=${BJ_CARDS[$(randnum_d)]}
echo -e "Dealers next card is:\t${TXT_BLD}$(echo ${CARD_DEALER[$ARRAY_INDEX]} | awk '{print $0}')${TXT_RST}"
fi
}
dealer.total () {
DEALER_TOTAL=0
for i in "${CARD_DEALER[@]}"; do
CARD_VALUE=$(echo $i | tr -d '()' | awk '{print $3}')
DEALER_TOTAL=$(($DEALER_TOTAL+$CARD_VALUE))
done
echo -e "Dealer currently has:\t${TXT_WARN}$DEALER_TOTAL${TXT_RST}"
}
dealer.play () {
while [[ "$DEALER_TOTAL" -lt '17' ]]; do
dealer.deal
dealer.total
done
}
reset.game () {
unset DEALER_TOTAL; unset PLAYER_TOTAL; unset CARD_DEALER; unset CARD_PLAYER; unset CURRENT_CARDS; unset ARRAY_INDEX
BJ_PLAY='yes'
}
BJ_PLAY='yes'
while [[ "$BJ_PLAY" = "yes" ]]; do
echo "Hello $USER, would you like to play blackjack?"
select answer in Yes No; do
case $answer in
Yes) echo "Excellent, have fun!"; reset.game; break;;
No) echo "Beat it then, chump!"; BJ_PLAY='no'; exit 1;;
esac
done
echo "$L_BREAK"
player.deal
while [[ "$PLAYER_TOTAL" -lt '21' ]]; do
select answer in Hit Stay; do
case $answer in
Hit) player.deal; break;;
Stay) break 2;;
esac
done
done
if [[ "$PLAYER_TOTAL" -gt '21' ]]; then
echo "${TXT_ERR}You bust! ${TXT_RST}Dealer wins."
echo "$L_BREAK"
PLAYER_BUST='yes'
else
PLAYER_BUST='no'
fi
echo "$L_BREAK"
if [[ "$PLAYER_BUST" = 'no' ]]; then
dealer.play
fi
if [[ "$DEALER_TOTAL" -gt '21' && "$PLAYER_BUST" = 'no' ]]; then
echo "Dealer bust. ${TXT_OKAY}You win! ${TXT_RST}"
echo "$L_BREAK"
elif [[ "$DEALER_TOTAL" -ge "$PLAYER_TOTAL" && "$PLAYER_BUST" = 'no' ]]; then
echo -e "Dealer has:\t$DEALER_TOTAL\nYou have:\t$PLAYER_TOTAL\n${TXT_ERR}You lose! ${TXT_RST}"
echo "$L_BREAK"
elif [[ "$DEALER_TOTAL" -lt "$PLAYER_TOTAL" && "$PLAYER_BUST" = 'no' ]]; then
echo -e "Dealer has:\t$DEALER_TOTAL\nYou have:\t$PLAYER_TOTAL\n${TXT_OKAY}You win! ${TXT_RST}"
echo "$L_BREAK"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment