#!/bin/bash 
#

# I did not write this. I found it someplace, and have moded it for color and high scores.

# changes 
# Thu Feb 05 2026 Added emoji output
# Thu Feb 05 2026 Added defaults for play and bet 

# Colors - uncomment if needed
# ----------------------------
BK=$(tput setaf 0)
R=$(tput setaf 1)                          # red
BR=$(tput setaf 1; tput bold)              # bold red
G=$(tput setaf 2)                          # green
BG=$(tput setaf 2; tput bold)              # bold green
Y=$(tput setaf 3)                          # yellow
BY=$(tput setaf 3; tput bold)              # bold yellow
B=$(tput setaf 4)                          # blue
BM=$(tput setaf 5; tput bold)              # bold magenta
BC=$(tput setaf 6; tput bold)              # bold cyan
BL=$(tput setaf 7; tput bold)              # bold light grey
BLD=$(tput bold)                           # bold
N=$(tput sgr0)                             # normal
SIT=$(tput sitm)                           # italics
RIT=$(tput ritm)                           # remove italics
UL=$(tput smul)                            # turn underline on
NL=$(tput rmul)                            # turn underline off
RV=$(tput rev)                             # turn on reverse mode
# colors - background
RBG=$(tput setab 1)                         # red background
BRBG=$(tput setab 1 ; tput bold )           # light red background
GBG=$(tput setab 2)                         # green background
BGBG=$(tput setab 2 ; tput bold )           # light green background
YBG=$(tput setab 3)                         # yellow background
BYBG=$(tput setab 3 ; tput bold )           # light yellow background
BBG=$(tput setab 4)                         # blue background
BBBG=$(tput setab 4 ; tput bold )           # light blue background
MBG=$(tput setab 5)                         # purple background
BMBG=$(tput setab 5 ; tput bold )           # light purple background
CBG=$(tput setab 6)                         # cyan background
BCBG=$(tput setab 6 ; tput bold )           # light cyan background
WBG=$(tput setab 7)                         # white background
BWBG=$(tput setab 7 ; tput bold )           # light white background
ROWS=$(tput lines)
COLS=$(tput cols)

## Emoji

EMOJI=(🤠 🍎 🍐 🍋 🍌 🍉 🍇 🍒 )

clear
echo -e "${RV}Welcome to the Casino! - This is the slot machine program${N}\n"

if [ -s ~/etc/slots.bank -a "$(cat ~/etc/slots.bank)" -lt 1 ]
then
    read  -p "${BC}Enter the amount to start with: ${N}" amount
else
    amount=$(cat ~/etc/slots.bank)
    echo "${SIT}Your bank is:${ROT} ${BG}$amount${N}"
    sleep 2
fi

while true; do
    clear
    read -n 1 -p "${BY}1${N} to spin, ${BR}2${N} to exit:" val
    [ -z "$val" ] && val=1
    if [ ! "$val" -ne 2 ]; then
        echo -e "\nYour amount is : ${BG}$amount${N}"
        echo "$(date +%F)  $amount" >>~/etc/slots.scores
        echo -e "\n -[ Top Ten ]-"
        sort -k 2nr ~/etc/slots.scores | head -n 10 |cat -n | sed s/${amount}$/$(tput setaf 1; tput bold)${amount}$(tput sgr0)/1
        [ "$amount" -gt 0 ] && echo "$amount" >~/etc/slots.bank
        exit 0
    else
        if [[ -z "$amount" ]] || [[ "$amount" -le 1 ]]
        then
            echo " "
            read -p "Enter the amount of money to add to your account: " addamount
            amount=$((addamount + amount))
            echo -e "\nAdded $addamount to account, total balance is : ${BG}$amount${N}\n"
        fi
        echo -e "\n"
        while true; do
            read -p "Enter the wager (defaults to 2): " wager
            [ -z "$wager" ] && wager=2
            if [ "$wager" -gt "$amount" ]; then
                echo -e "\nWager is greater than amount, enter an amount less than or equal to amount\n"
            else
                echo -e "\nWager is : $wager\n"
                break
            fi
        done
        amount=$((amount-wager))
        echo -e "${SIT}Spinning${RIT}\n"
        numbers=""
        sleep 1
        randomnumber=$(shuf -i 1-7 -n 1)
        numbers="$randomnumber"
        echo -n "${EMOJI[$randomnumber]} "
        sleep 1
        randomnumber=$(shuf -i 1-7 -n 1)
        numbers="${numbers}${randomnumber}"
        echo -n "${EMOJI[$randomnumber]} "
        sleep 1
        randomnumber=$(shuf -i 1-7 -n 1)
        number="${numbers}${randomnumber}"
        echo -e "${EMOJI[$randomnumber]} \n"
        letter1=${number:0:1}
        letter2=${number:1:1}
        letter3=${number:2:1}
        #echo "Letter 1 : $letter1, Letter 2 : $letter2, Letter 3 : $letter3"
        if [ "$letter1" == "$letter2" -a "$letter1" == "$letter3" -a "$letter1" -eq 7 ]; then
            amountwon=$((wager * 10000))
            echo "${REV}Congratulations! Jackpot victory : Payout -> $amountwon!${N}"
            amount=$((amount+amountwon))
            echo -e "\nYour amount : ${BG}$amount${N}\n"
        elif [ "$letter1" -eq "$letter2" -a "$letter2" -eq "$letter3" -a "$letter1" -ne 7 ]; then
            amountwon=$((wager * 20))
            echo "Congratulations! You won $amountwon!"
            amount=$((amount + amountwon))
            echo -e "\nYour amount : ${BG}$amount${N}\n"
        elif [ "$letter1" == "$letter2" -a "$letter1" -eq 7 ]; then
            amountWon=$((wager * 10))
            echo "Congratulations! You won $amountwon"
            amount=$((amount + amountwon))
            echo -e "\nYour amount : ${BG}$amount${N}\n"
        elif [ "$letter2" -eq "$letter3" -a "$letter3" -eq 7 ]; then
            amountwon=$((wager * 10))
            echo "Congratulations! You won $amountwon"
            amount=$((amountwon + amount))
            echo -e "\nYour amount : ${BG}$amount${N}\n"
        elif [ "$letter1" == "$letter2" ]; then
            amountwon=$((wager * 5))
            echo "Congratulations! You won $amountwon"
            amount=$((amount+amountwon))
            echo -e "\nYour amount : ${BG}$amount${N}\n"
        elif [ "$letter1" == "$letter3" ]; then
            amountwon=$((wager * 5))
            echo "Congratulations! You won $amountwon"
            amount=$((amount+amountwon))
            echo -e "\nYour amount : ${BG}$amount${N}\n"
        elif [ "$letter2" == "$letter3" ]; then
            amountwon=$((wager * 5))
            echo "Congratulations! You won $amountwon"
            amount=$((amount+amountwon))
            echo -e "\nYour amount : ${BG}$amount${N}\n"
        else
            echo -e "\nYour amount : ${BY}$amount${N}\n"
            echo "next round"
        fi
    fi
    read -p "${BY}Enter to continue:${N}" -n 1 toss
done