Skip to content

Instantly share code, notes, and snippets.

@sleekweasel
Created April 11, 2025 22:43
Show Gist options
  • Save sleekweasel/8e069d600353d1fee8a90ef8b6163e76 to your computer and use it in GitHub Desktop.
Save sleekweasel/8e069d600353d1fee8a90ef8b6163e76 to your computer and use it in GitHub Desktop.
For Max
#!/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
@sleekweasel
Copy link
Author

tim@omoensis:$ cat db.animals
0 y
1 n2 does it have stripes?
1 y
12 n13 Is it in the weasel family?
2 y
3 n4 Is it extinct?
3 y
Gorgonposid
4 y5 n6 does it look like a mini bear
5 y9 n10 does it have a prehensile tail.
6 y7 n8 Does it have a white tipped tail and is it red
7 yFox n
8 ymink n
9 ybinturong n
10 ywolverine n
12 ybadger n
13 ytiger n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment