Created
March 29, 2023 07:13
-
-
Save hexagon5un/44fbe4711183106f6ade69b2d7cd46cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
Code by Al Williams for his Hackaday article on gum: https://hackaday.com/2023/03/29/linux-fu-gum-up-your-script/