Created
February 2, 2022 07:22
-
-
Save fastfingertips/16089496057697792912be031c198fd3 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
url="https://gist.githubusercontent.com/zneak/53f885f1fc5856741cb4/raw/a17a81d15acb8109cda8524c743186901dd269b6/words.txt" | |
words=($(curl -s $url | grep '^\w\w\w\w\w$' | tr '[a-z]' '[A-Z]')) | |
actual=${words[$[$RANDOM % ${#words[@]}]]} | |
end=false | |
guess_count=0 | |
max_guess=6 | |
if [[ $1 == "unlimit" ]]; then | |
max_guess=999999 | |
fi | |
while [[ $end != true ]]; do | |
guess_count=$(( $guess_count + 1 )) | |
if [[ $guess_count -le $max_guess ]]; then | |
echo "Enter your guess ($guess_count / $max_guess):" | |
read guess | |
guess=$(echo $guess | tr '[a-z]' '[A-Z]') | |
if [[ " ${words[*]} " =~ " $guess " ]]; then | |
if [[ $actual == $guess ]]; then | |
echo "You guessed right!" | |
output="" | |
for ((i = 0; i < ${#actual}; i++)); do | |
output+="\033[30;102m ${guess:$i:1} \033[0m" | |
done | |
printf "$output\n" | |
end=true | |
else | |
output="" | |
c_actual=$actual | |
for ((i = 0; i < ${#actual}; i++)); do | |
if [[ "${c_actual:$i:1}" != "${guess:$i:1}" ]]; then | |
if [[ "$c_actual" == *"${guess:$i:1}"* ]]; then | |
output+="\033[30;103m ${guess:$i:1} \033[0m" | |
else | |
output+="\033[30;107m ${guess:$i:1} \033[0m" | |
fi | |
else | |
output+="\033[30;102m ${guess:$i:1} \033[0m" | |
fi | |
c_actual=$(echo $c_actual | sed "s/${guess:$i:1}//") | |
done | |
printf "$output\n" | |
fi | |
else | |
echo "Please enter a valid word with 5 letters!"; | |
guess_count=$(( $guess_count - 1 )) | |
fi | |
else | |
echo "You lose! The word is:" | |
echo $actual | |
end=true | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment