#!/bin/bash

# Title......: g
# Description: A menu for games
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Tue Mar 10 09:31:37 AM CDT 2026
#----------------------------------

# Sections: Changes variables functions setup main
# use '#' in vi/vim to jump to word under cursor

: ' Changes
Fri Jan 30 2026 Added Nethack
Thu Jan 29 2026 Added slots
Thu Jan 29 2026 Added iyhat
Mon Jun 02 2025 Added nethack
Sat Mar 22 2025 Added TUI Battleship
Tue Mar 04 2025 fixed list output
'

trap "continue" 2     # allows you to break a bad connection

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
DATE=$(date +"%a %b %d")                   # sets up the date display
PS4='$SECONDS $LINENO: '                   # debug prompt
OS=$(uname -s)                             # OS type: SunOS Linux
NAME=${0##*/}                              # name of the script
TMOUT=60                                   # time before screen saver kicks in
export PATH=/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin:$PATH

# color setup
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

# functions
#----------------------------------

html(){ ## mark up code
    vim -f +"syn on" +"set nonu" +"set foldenable!" +"set nospell" +"run! syntax/2html.vim" +"wq" +"q" $1
}

pause(){ ## Simple routine to stop execution
    echo " "
    echo -n "Hit any key to continue: "
    read -n 1 x
}

xtitle(){ ## set window title
    [ "$DEBUG" == "1" ] && set -x
    printf "\033]0;${*}🌀${HOSTNAME}\007"
}

log(){ ## creates a basic log entry $LOG must be defined
    # Use: log {entry}  
    [ "$DEBUG" == "1" ] && set -x
    logger -i -t "$NAME" "$*"
}

version(){ ## display version and change history
    grep -E '^# Updated' $0
    echo " "
    sed -n "/' Changes/,/^ *$/p" <$0 |grep  -E -v 'sed -n|exit 0|}'
    exit 0
}

# setup
#----------------------------------

# this provides a quick way to edit all my scripts on the fly
if [ "$1" == "-E" ]
then
    vim $0
    sed -i -e "7s/.*/# Updated....: $(date)/" $0
    log "Updated $0"
    html $0
    cp $0 /var/www/unix
    mv $0.html /var/www/unix
    log "Updated $0"
    exit
fi

# display version and change history
if [ "$1" == "-v" ] || [ "$1" == "--version" ]
then
    version
fi


# install checks
#--------------------------- 
command -v klondike2 >/dev/null || wget http://crn.hopto.org/unix/klondike2
command -v boxes >/dev/null || sudo apt install boxes -yy
command -v rogue >/dev/null || sudo apt install bsdgames-nonfree -yy
command -v nettoe >/dev/null || sudo apt install nettoe -yy
command -v freesweep >/dev/null || sudo apt install freesweep -yy
command -v vitetris >/dev/null || sudo apt install vitetris -yy
command -v ninvaders >/dev/null || sudo apt install ninvaders
command -v nethack >/dev/null || sudo apt install nethack-console

# main
#--------------------------- 
[ "$DEBUG" == "1" ] && set -x

while :
do
    if [ $# -lt 1 ] # check to see if a command line argument is passed
    then
    clear
    xtitle "Games"
echo "-[ ${UL}Games${NL} ]-${N}

${BY}$(fclock)${BC}

2. 🎴 Blackjack
b. 🟠 Backgammon
B. 🚢 TUI Battleship
h. 🤓 Hangman
i. 👾 Invaders
k. 🎴 Klondike
m. 💣 Minesweeper
n. ⚔️  Nethack
p. 🎴 Poker
r. 🏹 Roguelike (Nethack or BSD rogue)
s. 🎴 Solitaire
S. 🎰 Slots
t. 🔳 Tetris
T. 🔖 Tic-Tac-Toe
y. 🎲 Yahtzee
w. 🔍 Word Search
$([ -d ~/Windows ] && echo 'W. 🪟 Windows games')
${UL}                    ${NL}
${BR}q. Quit${N}
"
read -p Selection: -n 1 CHOICE
else
CHOICE=$(echo "$1" |cut -d'-' -f2)
[ "$1" == "-E" ] && CHOICE=E
fi

    case $CHOICE in
        b) # backgammon
            xtitle "🟠 Backgammon"
            clear
            echo $BC
            /usr/games/backgammon -n -w
            echo ${N}
            ;;
        B) # TUI Babttleship
            clear
            cd ~/bin/bs-2.13 && ./bs
            cd -
            clear
            ;;
        2) # blackjack
            xtitle "🎴 21"
            cd  ~/bin/blackjack-py
            python3  blackjack.py
            cd -
            ;;
        h) # hangman
            xtitle "🤓 Hangman"
            clear
            echo "${Y}"
            /usr/games/hangman -d ~/etc/words.txt # my custom word list, with Bibles words added
            echo ${N}
            ;;
        i) # ninvaders
            xtitle "👾 Ninvaders"
            ninvaders
            ;;
        k) # klondike
            xtitle "🎴 Klondike"
            klondike2
            ;;
        l) # list games
            clear
            if [ -d ~/Windows ]
            then
                grep ') #' $0| egrep -v '[q!=*]'| sed 's/^[ \t]*//'|tr -d ')#' |grep -v grep | /home/mitch/.local/bin/tte -R
            else
                grep ') #' $0| egrep -v '[q!=*]|Windows'| sed 's/^[ \t]*//'|tr -d ')#' |grep -v grep | /home/mitch/.local/bin/tte -R
            fi
            ;;
        m) # minesweeper
            xtitle  "💣 Minesweeper"
            minesweeper
            ;;
        n) # nethack
            nethack
            pause
            ;;
        p) # poker
            xtitle "🎴 Poker"
            #poker
            clear
            videopoker -mh
            pause
            ;;
        r) # rogue
        xtitle "🏹 Rogue"
            clear
            echo "$BC"
            cd ~/etc
            if [ -f rogue.save ]
            then
                vim -c RogueRestore -c :q!
            else
                vim -c Rogue -c :q!
            fi
            if [ -f rogue.save ]
            then
                ft "Game saved!"
            else
                    ft "RIP fallen hero!"
            fi
           sleep 3
           ;;
        s) # ttysolitaire
            xtitle "🎴 Solitaire"
            cd ~/bin/csol-master && ./csol 2>/dev/null
            cd - >/dev/null
            ;;
        S) # slots
            ~/bin/slots
            pause
            ;;
        t) # tetris
            xtitle "🔳 Tetris"
            clear
            vitetris
            pause
            ;;
        T) # tic-tac-toe
            xtitle "🔖 tic-tac-toe"
            clear
            nettoe
            clear
            ;;
        y) # yahtzee
            xtitle "🎲 Yahtzee"
            clear;echo ${BC}
            iyaht
            pause
            ;;
        q) # quit
            clear
            ft Bye
            echo -e "\n"
            exit
            ;;
        w) # wordsearch
            xtitle "🔍 Word Search"
            clear
            cd ~/bin/wordsearch-master
            ./wordsearch -t black 2>/dev/null
            ./wordsearch -s
            pause
            cd -
            ;;
        W) # Windows games
            xtitle "🪟 Windows games"
            cd ~/Windows
            CHOICE=$(\ls -F| grep '*$' |tr -d '*' |fzf --ansi --reverse --color fg:-1,bg:-1,hl:46,fg+:40,bg+:233,hl+:46 --color prompt:166,border:46 --height 70%  --border=sharp --prompt="" --pointer="" --marker="");
            clear
            echo -n "Now playing: $CHOICE "
            spinit ./${CHOICE}
            cd -
            clear
            ;;
        !) # run command
            clear
            echo "${C}Hit return with no command to end.${N}"
            while :
            do
                echo -n "[menu] $PWD $ "
                read EXEC
                [ -z "$EXEC" ] && break
                eval "$EXEC"
            done
            pause
            ;;
        =) # reset term and restart script
            reset
            exec $0
            ;;
        *) # screen saver
            xtitle "🌘 Screen saver"
            xs
            ;;
    esac
    [ $# -eq 1 ] && exit
done