Skip to content

Instantly share code, notes, and snippets.

@skydrome
Last active March 3, 2025 02:24
Show Gist options
  • Save skydrome/7b49404a3b8b3b5d81242243cf08d528 to your computer and use it in GitHub Desktop.
Save skydrome/7b49404a3b8b3b5d81242243cf08d528 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
white_bg=$(tput setab 15)
grey=$(tput setaf 8)
black=$(tput setaf 0)
red=$(tput setaf 1)
bold=$(tput bold)
reset=$(tput sgr0)
generate_deck() {
deck_size="${1:-52}"
for value in 2 3 4 5 6 7 8 9 10 J Q K A; do
for suit in ♠ ♥ ♣ ♦; do
deck+=("${value}${suit}")
done
done
# if ((${#deck[@]} != deck_size)); then
# echo "bad deck size"
# exit 1
# fi
}
shuffle() {
local times="${1:-3}"
#echo -en "shuffling...\r"
for i in $(seq 0 $times); do
deck=($(shuf --random-source=/dev/random -e "${deck[@]}"))
done
# royal flush
#deck=(A♠ K♠ Q♠ J♠ 10♠)
# straight flush
#deck=(2♠ 3♠ 4♠ 5♠ 6♠)
# 4 of a kind
#deck=(A♠ A♥ A♣ A♦ 2♦)
# full house
#deck=(7♠ 7♥ 7♣ 2♣ 2♦)
# flush
#deck=(2♠ 4♠ 6♠ 8♠ 10♠)
# straight
#deck=(2♠ 3♥ 4♣ 5♦ 6♦)
#deck=(2♠ A♥ 4♣ 5♦ 3♦)
# 3 of a kind
#deck=(A♠ A♥ A♣ 1♦ 2♦)
# 2 pair
#deck=(A♠ A♥ 2♣ 2♦ 3♦)
# 1 pair
#deck=(A♠ A♥ 2♣ 5♦ 8♦)
# king high
#deck=(2♠ 3♥ 4♣ 5♦ K♦)
}
deal_hand() {
local t=1
hand_size=${1:-5}
for i in "${deck[@]:0:hand_size}"; do
[[ ${i::2} == 10 ]] &&
declare -g num$t=${i::2} || declare -g num$t=${i::1}
declare -g suit$t=${i: -1}
case "${i: -1}" in
♥|♦) declare -g color$t=$red ;;
*) declare -g color$t=$black ;;
esac
((t++))
done
}
score() {
local -A value_count suit_count map
local r=$reset b=$bold
map["A"]=14; t+=(A); map["K"]=13; t+=(K); map["Q"]=12; t+=(Q); map["J"]=11; t+=(J);
map["10"]=10; t+=(10); map["9"]=9; t+=(9); map["8"]=8; t+=(8);
map["7"]=7; t+=(7); map["6"]=6; t+=(6); map["5"]=5; t+=(5);
map["4"]=4; t+=(4); map["3"]=3; t+=(3); map["2"]=2; t+=(2);
for card in "${deck[@]:0:hand_size}"; do
values+=(${card:0:-1})
suits+=(${card: -1})
done
for i in "${values[@]}"; do
((value_count[$i]++))
done
for i in "${suits[@]}"; do
((suit_count[$i]++))
done
#echo " suit_count: ${suit_count[*]}"
#echo "value_count: ${value_count[*]}"
readarray -t sorted < <(for i in "${values[@]}"; do echo "${map[$i]}"; done |sort -nr)
#echo " sorted: ${sorted[*]}"
(( ${#suit_count[@]} == 1 )) &&
flush=1
(( ${#value_count[@]} == 5 )) && {
if [[ $(( sorted[0] - sorted[4] )) -eq 4 || "${sorted[*]}" == "14 5 4 3 2" ]]; then
straight=1
fi
}
if (( flush && straight )); then
(( sorted[0] == 14 )) &&
result=("${b}Royal" "$b Flush") ||
result=("${b}Straight$r" "${b}Flush")
elif [[ ${value_count[@]} =~ 4 ]]; then
result=("4 of" "a kind")
elif [[ ${value_count[@]} =~ 3 &&
${value_count[@]} =~ 2 ]]; then
result=("full" "house")
elif (( flush )); then
result=("flush")
elif (( straight )); then
result=("straight")
elif [[ ${value_count[@]} =~ 3 ]]; then
result=("3 of" "a kind")
elif [[ ${value_count[@]} =~ "2 2" ]]; then
result=("two" "pair")
elif [[ ${value_count[@]} =~ 2 ]]; then
result=("one" "pair")
else
for i in "${t[@]}"; do
if (( ${map[$i]} == ${sorted[0]} )); then
local g=$grey
result=("$g[$r$i$g]" " High")
break
fi
done
fi
}
display() {
local r=$reset bg=$white_bg
# if ((hand_size > deck_size)); then
# echo "hand_size bigger than deck"
# hand_size=$deck_size
# fi
for i in $(seq 1 $hand_size); do
top+=" ╔══╗"
done
echo "$top"
for i in $(seq 1 $hand_size); do
c="color$i"
n="num$i"
printf " ║${bg}%s%-2s${r}║" ${!c} ${!n}
done
printf " %s" "${result[0]}${r}"
printf "\n"
for i in $(seq 1 $hand_size); do
c="color$i"
n="suit$i"
printf " ║${bg}%s %s${r}║" ${!c} ${!n}
done
printf " %s" "${result[1]}${r}"
printf "\n"
for i in $(seq 1 $hand_size); do
bot+=" ╚══╝"
done
echo "$bot"
# ╔══╗ ╔══╗ ╔══╗ ╔══╗ ╔══╗
# ║K ║ ║3 ║ ║9 ║ ║10║ ║A ║
# ║ ♣║ ║ ♠║ ║ ♣║ ║ ♠║ ║ ♦║
# ╚══╝ ╚══╝ ╚══╝ ╚══╝ ╚══╝
}
generate_deck # 52 total cards
shuffle 3 # times to shuffle
deal_hand 5 # how many cards to deal
score
display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment