From ece8854e8ff1306e6cac974e88f628ff7bf2eada Mon Sep 17 00:00:00 2001 From: nomsolence Date: Sat, 3 Oct 2020 02:50:34 -0700 Subject: [PATCH] Refactor (#2) --- GeographyTrivia/geographyTrivia-v3 | 154 +++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100755 GeographyTrivia/geographyTrivia-v3 diff --git a/GeographyTrivia/geographyTrivia-v3 b/GeographyTrivia/geographyTrivia-v3 new file mode 100755 index 0000000..0a2c092 --- /dev/null +++ b/GeographyTrivia/geographyTrivia-v3 @@ -0,0 +1,154 @@ +#!/bin/bash + +cat << "EOF" + + + /$$$$$$ /$$ /$$$$$$$$ /$$ /$$ + /$$__ $$ | $$ |__ $$__/ |__/ |__/ + | $$ \__/ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$| $$$$$$$ /$$ /$$ | $$ /$$$$$$ /$$/$$ /$$/$$ /$$$$$$ + | $$ /$$$$/$$__ $$/$$__ $$/$$__ $$/$$__ $|____ $$/$$__ $| $$__ $| $$ | $$ | $$/$$__ $| $| $$ /$$| $$|____ $$ + | $$|_ $| $$$$$$$| $$ \ $| $$ \ $| $$ \__//$$$$$$| $$ \ $| $$ \ $| $$ | $$ | $| $$ \__| $$\ $$/$$/| $$ /$$$$$$$ + | $$ \ $| $$_____| $$ | $| $$ | $| $$ /$$__ $| $$ | $| $$ | $| $$ | $$ | $| $$ | $$ \ $$$/ | $$/$$__ $$ + | $$$$$$| $$$$$$| $$$$$$| $$$$$$| $$ | $$$$$$| $$$$$$$| $$ | $| $$$$$$$ | $| $$ | $$ \ $/ | $| $$$$$$$ + \______/ \_______/\______/ \____ $|__/ \_______| $$____/|__/ |__/\____ $$ |__|__/ |__/ \_/ |__/\_______/ + /$$ \ $$ | $$ /$$ | $$ + | $$$$$$/ | $$ | $$$$$$/ + \______/ |__/ \______/ + + +EOF + + +# print-then-wait function +printwait() { + delay="$1" + shift + echo -e "$@" + sleep "$delay" +} + +# letter printing function +letterprint() { + i=0 + while [ $i -lt ${#1} ]; do + letter="${1:$i:1}" + if [ "$letter" = "|" ]; then + echo + else + echo -n "$letter" + fi + sleep "${2:-0.08s}" + ((i++)) + done +} + + +for line in \ + "Welcome to Geography Trivia, a game to test your worldliness!" \ + "Here's how it works:\n" \ + "* You will be presented a question and alotted 6 seconds to read the quesion." \ + "* All questions will be either [t]rue or [f]alse. Select the appropriate letter for the answer." \ + "* After reading the question, you will have 10 seconds to choose the correct answer."; +do + printwait 2s "$line" +done + +printwait 3s "\nReady, player?" +echo + + +declare -i score +score=0 +declare -i question +question=1 + + +# Administer questions (questions at the end of the while loop) +while read -r questionline; do + + # Parse answer line + read -r answerline + answer="${answerline%%|*}" # ${var%%pat} means "remove largest suffix" + wrong="${answerline##*|}" # ${var##pat} means "remove largest prefix" + correct="${answerline#*|}" # ${var#pat} means "remove smallest prefix" + correct="${correct%%|*}" + + # Print question + echo + letterprint "$questionline" + echo " [T/f]?" + + # Redirect stdin to file descriptor 3 + exec 3<&1 + + # Timeout, wait for answer + timeout=10 + while [ $timeout -ge 0 ]; do + echo -ne "\r[TIME $timeout] " + if read -n1 -t1 -r -s input <&3; then + [ -z "$timeout" ] && continue # no key pressed + case "$input" in + t|T|y|Y) ans=T; echo true; printwait 1s ;; + f|F|n|N) ans=F; echo false; printwait 1s ;; + *) ((timeout--)); continue ;; + esac + if [ "$ans" = "$answer" ]; then + echo "$correct" + ((score++)) + break + else + echo "$wrong" + break + fi + fi + ((timeout--)) + done + + if [ $timeout -lt 0 ]; then + echo -e "Time's up! Better luck next time.\n" + fi + + ((question++)) + +# Questions are formatted: +# QUESTION +# T/F|CORRECT|WRONG +# Use | in the question to add a newline +done <