Skip to content

Instantly share code, notes, and snippets.

@mariecrane
Forked from huytd/wordle.md
Last active February 5, 2022 21:33
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 mariecrane/1c9ad6fe5220d452176c97f32fcf7155 to your computer and use it in GitHub Desktop.
Save mariecrane/1c9ad6fe5220d452176c97f32fcf7155 to your computer and use it in GitHub Desktop.
Wordle in less than 60 lines of Bash

Screen Shot 2022-02-04 at 2 31 39 PM

Adapted from https://gist.github.com/huytd (modified to limit possible guesses and answers and to output emoji summary at the end, to more closely resemble Josh Wardle's original game)

Lists of possible guesses and answers from https://gist.github.com/cfreshman

How to use:

  1. Download list of answers from https://gist.github.com/cfreshman/a03ef2cba789d8cf00c08f767e0fad7b
  2. Download list of all possible guesses from https://gist.github.com/mariecrane/661cf5d0fad7f16b52fd4f8c771d91f9
  3. Download wordle.sh and make sure it's in the same directory as the two text files
  4. Navigate to that directory and enter:
bash wordle.sh
guesses=($(grep '^\w\w\w\w\w$' wordle-allowed-guesses-with-answers.txt | tr '[a-z]' '[A-Z]'))
solutions=($(grep '^\w\w\w\w\w$' wordle-answers-alphabetical.txt | tr '[a-z]' '[A-Z]'))
actual=${solutions[$[$RANDOM % ${#solutions[@]}]]} end=false guess_count=0 max_guess=6 summary=""
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 [[ " ${guesses[*]} " =~ " $guess " ]]; then
output="" remaining=""
if [[ $actual == $guess ]]; then
echo "You guessed right!"
for ((i = 0; i < ${#actual}; i++)); do
output+="\033[30;102m ${guess:$i:1} \033[0m"
summary+="\xf0\x9f\x9f\xa9"
done
printf "$output\n"
printf "\n$guess_count / $max_guess"
printf "\n$summary\n"
end=true
else
for ((i = 0; i < ${#actual}; i++)); do
if [[ "${actual:$i:1}" != "${guess:$i:1}" ]]; then
remaining+=${actual:$i:1}
fi
done
for ((i = 0; i < ${#actual}; i++)); do
if [[ "${actual:$i:1}" != "${guess:$i:1}" ]]; then
if [[ "$remaining" == *"${guess:$i:1}"* ]]; then
output+="\033[30;103m ${guess:$i:1} \033[0m"
summary+="\xf0\x9f\x9f\xa8"
remaining=${remaining/"${guess:$i:1}"/}
else
output+="\033[30;107m ${guess:$i:1} \033[0m"
summary+="\xe2\xac\x9b\xef\xb8\x8f"
fi
else
output+="\033[30;102m ${guess:$i:1} \033[0m"
summary+="\xf0\x9f\x9f\xa9"
fi
done
printf "$output\n"
summary+="\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
printf "\n$summary\n"
end=true
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment