one-line-games/games.sh

116 lines
4.4 KiB
Bash

#!/bin/bash
# Copyright 2020, Storm Dragon, <storm_dragon@linux-a11y.org>
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this package; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# One line games
# Each game is a function, the code is one line, not including the function name and comments.
# Array with list of game functions.
# Add new games to this array.
gameList=(
match_the_letter
guess_1_to_10
numnastics
spelling_game
math_game
pig
helecopter
pitch_shifter
keyboard
exit
)
# Games start here
match_the_letter() {
#Match the letter 197 characters
stty -echo;s=99;while [ : ];do t=$(shuf -n1 -e {a..z});banner $t;read -t0.$s -n1 -p " $t " p;if [ "$t" != "$p" ];then play -nqV0 synth pi fade q 0 1 0.5;break;else play -nqV0 synth .05 tri C8:C3 norm -7;fi;((s--));clear;done;stty echo
}
guess_1_to_10() {
#guess 1 to 10
a=Boo;read -p"Guess 1-10 " n;[ $n -eq $(shuf -en1 {1..10}) ]&&a=Yay;echo $a\!
}
numnastics() {
#order the numbers 1-9 type the number to reverse the string from that number to the right
c=0;n=$(shuf -zi1-9);while [ $n -ne 123456789 ];do read -n1 -p$n i;l=${n%$i*};l=${#l};a=${n:0:$l};b=$(echo ${n:$l}|rev);n=$a$b;((c++));echo;done;echo $n $c tries
}
spelling_game() {
#spelling game with changable list
l=(champagne neighbor tortoise);clear;s="espeak -v en-us -a 200";for i in ${l[@]};do $s $i;read w;if [ "$w" = "$i" ];then $s "Got it!";else c=0;while [ $c -lt ${#i} ];do $s ${i:$c:1};((c++));done;fi;done &> /dev/null
}
math_game() {
#Simple configurable math game, change o for different types, set n min and x max numbers used.
n=0;x=10;o=+;m=0;r=0;s=5;t=0;a=-;while [ "${a^}" != "Q" ];do p="$(shuf -n1 -i$n-$x)$o$(shuf -n1 -i$n-$x)";read -t$s -p "$p " a;if [ $a -eq $(($p)) ] ; then echo "Yay!";((r++));s=$(echo "scale=2;$s-0.05" | bc);else echo "Wrong: $(($p))";((m++));fi 2> /dev/null;((t++));done;echo -e "\n$r right and $(($m-1)) missed of $(($t-1)) total."
}
pig() {
#Pig dice game. any letter to roll, space or enter to bank.
c=0;p=0;s=0;t=$(shuf -n1 -e c p);while [ $c -lt 100 -a $p -lt 100 ];do if [ $t = c ];then a=$(shuf -n1 -i0-6);case $a in 0)c=$(($c+$s));s=0;t=p;echo "CPU banked $c.";;1)echo "CPU Pigged\!";s=0;t=p;;*)s=$(($s+$a));echo "CPU $s : $c";esac;else read -n1 -p "? " a;if [ -z $a ];then t=c;p=$(($s+$p));echo "You banked $p.";s=0;else a=$(shuf -n1 -i1-6);if [ $a -eq 1 ];then s=0;t=c;echo "You Pigged\!";else s=$(($s+$a));echo "You $s : $p";fi;fi;fi;done;echo "CPU $c : You $p"
}
helecopter() {
#Shoot down the helecopter you hear when sound is centered. Random speeds.
h=0;n=1;s=20;while [ $n -eq 1 ];do x=9;y=0;while [ $x -gt 0 ];do espeak --stdout [[schXh | play -qV0 - norm remix v0.$x v0.$y&read -st0.$s -n1 p;[ -n "$p" ]&&if [ $x -eq 5 -a $y -eq 4 -o $x -eq 4 -a $y -eq 5 ];then x=9;y=0;play -nqV0 synth pi fade h 0 1 1 pad 0 1 reverb overdrive riaa speed 32 repeat 2;play -nqV0 synth pi fade h 0 1 1 norm -18 pad 0 1 reverb overdrive riaa;((h++));s=$(seq 10 20|shuf -n1);else n=0;fi;((x--));((y++));done;done;echo "$h kills."
}
pitch_shifter() {
#Talking CLIcat (pitch shifter).
e="norm pitch 1000";echo q exits.;f=`mktemp`;while ! read -n1 -t.5 ;do rec -qtwav $f silence 0 1 .5 25%&&play -q $f $e;done;\rm $f
}
keyboard() {
# Small polyphonic music synthesizer.
n=CDEFGAB;l=asdfghj;while read -n1 k;do x=$(tr $l $n<<<$k);play -qn synth pl ${x}3 fade 0 .7 & >/dev/null;done
}
# Add games above this line.
# Menu system is here.
trap menu SIGINT
trap menu SIGTERM
menu() {
clear
while : ; do
menuLength=${#gameList[@]}
menuLength=$((menuLength - 1))
echo
for i in 0 $(seq $menuLength) ; do
echo "$i: ${gameList[$i]}"
done
echo
echo "Enter a number:"
read choice
eval "${gameList[$choice]}"
echo
read -n1 -p "Press any key to continue" choice
echo
done
}
menu