Skip to content

Instantly share code, notes, and snippets.

@adamdehaven
Created February 7, 2022 02:37
Show Gist options
  • Save adamdehaven/26f2e0c4d1c538cc8ab335390e51d215 to your computer and use it in GitHub Desktop.
Save adamdehaven/26f2e0c4d1c538cc8ab335390e51d215 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
mapfile -t words < <(grep -x '[a-z]\{5\}' "${WORDLIST:-/usr/share/dict/words}")
word=${words[RANDOM % ${#words[@]}]} pool=abcdefghijklmnopqrstuvwxyz
for ((round=1; round <= ${ROUNDS:=6}; round++)); do
while chars="" && read -rp "$round/$ROUNDS: " guess || exit 1; do
case " ${words[@]} " in *" ${guess,,} "*) guess=${guess,,}; break; esac
done; for ((i=0; i < ${#word}; i++)); do
[[ ${word:i:1} != ${guess:i:1} ]] && chars+=${word:i:1}
done; i=0; while char=${guess:i:1} && ((i < ${#guess})) ; do
{ [[ ${word:i++:1} == $char ]] && color=2; } ||
{ [[ $chars == *$char* ]] && color=3 chars=${chars/$char/}; } ||
{ color=7; }
[[ $word != *$char* ]] && pool=${pool/$char/}
printf '\033[30;4%dm %s \033[0m' "$color" "${char^^}"
done
printf ' %s\n' "$pool"
[[ $guess == $word ]] && exit
done
printf 'The word was "%s"\n' "$word"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment