A word typing game generated by ChatGPT
Question: Write a shell script word typing game with 100 words and each word to type is 5 seconds and have to keep typing words until the user types 'end' to finish the game and shows the result score
Awnser:
Creating a shell script for a word typing game involves using a loop to present words to the user, measuring the time it takes for each word, and keeping track of the score. Here's a simple implementation of the game:
#!/bin/bash
# Array containing 100 random words
words=(
"apple" "banana" "orange" "grape" "kiwi" "strawberry" "melon" "pineapple" "pear" "mango"
"cherry" "blueberry" "peach" "lemon" "watermelon" "apricot" "plum" "pomegranate" "coconut" "raspberry"
# Add more words here...
)
# Function to display a random word
get_random_word() {
local word_index=$((RANDOM % ${#words[@]}))
echo "${words[$word_index]}"
}
# Function to calculate score
calculate_score() {
local correct_words=$1
local total_words=${#words[@]}
local percentage=$((correct_words * 100 / total_words))
echo "Your Score: $correct_words / $total_words ($percentage%)"
}
echo "Welcome to the Word Typing Game!"
echo "Type each word and press Enter. You have 5 seconds per word."
echo "Type 'end' to finish the game."
correct_count=0
while true; do
word_to_type=$(get_random_word)
echo "Word: $word_to_type"
# Start measuring time
start_time=$(date +%s)
# Read user input
read -t 5 user_input
# End the game if the user types 'end'
if [ "$user_input" = "end" ]; then
break
fi
# End measuring time
end_time=$(date +%s)
time_taken=$((end_time - start_time))
if [ "$user_input" = "$word_to_type" ] && [ "$time_taken" -le 5 ]; then
echo "Correct!"
((correct_count++))
else
echo "Wrong or Time's up!"
fi
done
calculate_score $correct_count
Save this script to a file, e.g., word_typing_game.sh, and make it executable using chmod +x word_typing_game.sh. Then, run the script with ./word_typing_game.sh.
The game will present a random word from the list and give the user 5 seconds to type the word correctly. The user can keep typing words until they type 'end' to finish the game. After the game ends, the script will display the user's score based on the number of correctly typed words out of 100.