cli-games/simon/simon
2017-06-20 17:04:12 -04:00

48 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
# Simon notes
notes=("sq E4" "sq C#4" "sq A3" "sq E3")
#Original note.
pattern=($(($RANDOM % 4)))
#Here is the main loop of the program
continue=1
while [ $continue -ne 0 ]; do
# Play the pattern to match
for i in ${pattern[@]} ; do
play -qV0 -n synth .5 ${notes[$i]} norm -9 &> /dev/null
done
# Loop to read the player's input and compare it to the pattern
# Clear the player variable
unset player
i=0
while read -sn1 key ; do
key="$(echo "$key" | tr 'fk' '0')"
key="$(echo "$key" | tr 'dj' '1')"
key="$(echo "$key" | tr 'ir' '2')"
key="$(echo "$key" | tr 'eu' '3')"
player=(${player[@]} $key)
# make sure the pressed key exists in the array.
if [[ "$key" =~ [0-3] ]]; then
play -qV0 -n synth .5 ${notes[$key]} norm -9 & &> /dev/null
fi
# make sure the player pattern is the same as the computer generated one
if [ "${player[$i]}" != "${pattern[$i]}" ]; then
continue=0
break
fi
# If the arrays are equal then break the loop, else increment i.
if [ ${#player[@]} -eq ${#pattern[@]} ]; then
break
else
((i++))
fi
done
if [ $continue -ne 0 ]; then
# sleep slightly longer than the length of one note.
sleep .6
pattern=(${pattern[@]} $(($RANDOM % 4)))
fi
done
echo "You got $((${#pattern[@]} - 1)) correct."
exit 0