Created
April 11, 2025 22:43
-
-
Save sleekweasel/8e069d600353d1fee8a90ef8b6163e76 to your computer and use it in GitHub Desktop.
For Max
This file contains hidden or 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
#!/bin/bash | |
yesno() { | |
while true ; do | |
echo "--> $1" | |
read | |
case "$REPLY" in | |
y*) return 0 ;; | |
n*) return 1 ;; | |
*) echo "Please answer y or n" ;; | |
esac | |
done | |
} | |
ANIMALS=db.animals | |
touch $ANIMALS | |
ix=0 | |
echo "Think of an animal, I'll try to guess it" | |
while true ; do | |
last=$ix | |
read ix yes no question <<<"$(grep -E "^$ix" $ANIMALS)" | |
if [ "$question" ] ; then | |
if yesno "$question"; then ix=$yes ; else ix=$no ; fi | |
ix=$(echo "$ix" | cut -d~ -f2) | |
elif [ "$yes" ] ; then | |
yes=$(echo "$yes" | cut -d~ -f2) | |
if yesno "Is it a $yes?" ; then | |
echo "I win! Hurrah!" | |
exit | |
else | |
echo "You win! What was it you were thinking of?" | |
read thing | |
read kix kyes kno kquestion <<<"$(grep -E " y~$thing " $ANIMALS)" | |
if [ "$kix" ] ; then | |
echo "That's odd - I see $thing already in my database under these different questions and answers:" | |
while [[ kix -ne 0 ]] ; do | |
ix=$kix | |
read kix kyes kno kquestion <<<"$(grep -E "~$kix " $ANIMALS)" | |
echo -n " * $kquestion -> " | |
if [[ "y~$ix" == "$kyes" ]] ; then echo YES ; else echo NO ; fi | |
kix=$( echo $kix | cut -d~ -f 2 ) | |
done | tac | |
exit | |
fi | |
echo "Great! Please give me a yes/no question I can ask you next time to distinguish a $yes from a $thing" | |
while true ; do | |
read question | |
if yesno "Thanks! Now: if you're thinking of a $yes, would you answer yes or no to $question?" ; then | |
qyes=true | |
else | |
qyes=false | |
fi | |
if yesno "And to check: if you're thinking of a $thing, would you answer yes or no to $question?" ; then | |
qthing=true | |
else | |
qthing=false | |
fi | |
if [[ "$qyes" != "$qthing" ]] ; then | |
break | |
fi | |
echo "Oh... Can you type a question where the answer is different for $yes and $thing?" | |
done | |
echo "Nice! I'll add that to my database!" | |
ll=$(wc -l < $ANIMALS) | |
sed -i "s/^$ix .*/$ix y~$(( ll + 1 )) n~$(( ll + 2 )) $question/" $ANIMALS | |
if $qyes ; then | |
echo "$(( ll + 1 )) y~$yes n~" >> $ANIMALS | |
echo "$(( ll + 2 )) y~$thing n~" >> $ANIMALS | |
else | |
echo "$(( ll + 1 )) y~$thing n~" >> $ANIMALS | |
echo "$(( ll + 2 )) y~$yes n~" >> $ANIMALS | |
fi | |
exit | |
fi | |
else | |
echo "Actually... I don't know any animals yet. What were you thinking of?" | |
read | |
echo "Cool! I'll add that to my database." | |
echo "0 y~$REPLY" >> $ANIMALS | |
exit | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tim@omoensis:
$ cat db.animals1 n0 y
2 does it have stripes?12 n1 y
13 Is it in the weasel family?3 n2 y
4 Is it extinct?Gorgonposid3 y
4 y
5 n6 does it look like a mini bear5 y
9 n10 does it have a prehensile tail.6 y
7 n8 Does it have a white tipped tail and is it red7 y
Fox n8 y
mink n9 y
binturong n10 y
wolverine n12 y
badger n13 y
tiger n