Skip to content

Instantly share code, notes, and snippets.

@wd5gnr
Last active March 29, 2023 20:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wd5gnr/b432955a4cb89f9cc2036479382ef61b to your computer and use it in GitHub Desktop.
Save wd5gnr/b432955a4cb89f9cc2036479382ef61b to your computer and use it in GitHub Desktop.
A simple hilo game using Gum (https://github.com/charmbracelet/gum)
#!/bin/bash
clear
# init colors
FG="#00ffff"
BFG="#00ff00"
# if you wonder why true is in there https://www.shellcheck.net/wiki/SC2015
gum confirm --default --affirmative="Play now" --negative="Dump source first" --timeout=10s "Would you like to view the source code before playing?" && true || ( gum format -t code <"$0" )
# get initial info
gum style --foreground "$FG" --border-foreground "$BFG" --border double --align center --width 64 --margin "1 2" --padding "2 4" \
"Welcome to Hackaday's HiLo Game!" \
"First, tell me your name and then pick the higest number YOU want me to guess"
echo "What's your first name?"
NAME=$( gum input --placeholder="Tell me your name, please..." )
NAME="*$NAME*"
if [ "$NAME" == "**" ]
then
gum style --foreground "$FG" --border-foreground "$BFG" --border double --align center --width 64 --margin "1 2" --padding "2 4" "Oh well, see you later"
exit 0
fi
echo "Hi $NAME. Now tell me the highest number you might pick. Then pick a number less than or equal to that and I'll guess it." | gum format
MIN=1
MAX=X
while [[ "$MAX" == "X" || "$MAX" == "" ]]
do
MAX=$( gum choose --selected="100" 100 1000 10000 exit )
done
if [ "$MAX" == exit ]
then
gum style --foreground "$FG" --border-foreground "$BFG" --border double --align center --width 64 --margin "1 2" --padding "2 4" "See you next time"
exit 0
fi
TRUEMAX=$MAX # save for next game
# here's the main game loop
while true
do
CT=$(( CT + 1 )) # number of guesses
if [ "$MAX" -le "$MIN" ] # detect cheating
then
gum style --foreground "$FG" --border-foreground "$BFG" --border double --align center --width 64 --margin "1 2" --padding "2 4" "I'm not playing if you are going to cheat!"
exit 0
fi
G=$(( (MAX+MIN)/2 )) # calculate guess
gum spin -s meter --title "Thinking..." sleep 2 # make it look hard
echo "Well $NAME... I guess $G" | gum format
RES=$( gum choose low high right quit )
if [ "$RES" == quit ]
then
gum style --foreground "$FG" --border-foreground "$BFG" --border double --align center --width 64 --margin "1 2" --padding "2 4" "That was fun anyway"
exit 0
fi
# got it!
if [ "$RES" == right ]
then
gum style --foreground "$FG" --border-foreground "$BFG" --border double --align center --width 64 --margin "1 2" --padding "2 4" "Yeah! I knew I would get it! Took me $CT tries"
CT=0
MIN=1
MAX=$TRUEMAX # set up for next game, maybe
# continue can't return false (https://www.shellcheck.net/wiki/SC2015)
gum confirm "Play again?" && continue || break
exit 0
fi
# didn't get it, so adjust
if [ "$RES" == low ]
then
MIN=$G
fi
if [ "$RES" == high ]
then
MAX=$G
fi
done
# exit
gum style --foreground "$FG" --border-foreground "$BFG" --border double --align center --width 64 --margin "1 2" --padding "2 4" "That was fun!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment