Created
June 18, 2014 21:54
-
-
Save evanthebouncy/ca59c5d6a072f64d7300 to your computer and use it in GitHub Desktop.
A simple animal game
This file contains 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
# An interactive game where the computer can learn to recognize | |
# more animals! | |
# Not sure if this is best practice for clarity, but i kept the | |
# data structure more like data rather than objects with methods | |
# so the recursion looks cleaner. | |
# an animal, like a dog or a cat | |
class Animal: | |
def __init__(self, animal): | |
self.animal = animal | |
self.parent = None | |
def get_question(self): | |
return 'is it a '+self.animal+'? (yes/no)\n' | |
# a trait has 2 groups of animals, group1 and group2 | |
# it also keeps a trait, such as "having wings" which | |
# all the animals in group1 has and in group2 does not | |
class Trait: | |
def __init__(self, trait, group1, group2): | |
self.trait = trait | |
self.parent = None | |
self.group1 = group1 | |
self.group2 = group2 | |
group1.parent = self | |
group2.parent = self | |
def get_question(self): | |
return 'does it have '+self.trait+'? (yes/no)\n' | |
# basic function on the animal data structure to get user answer | |
def is_animal(animal): | |
user_answer = raw_input(animal.get_question()) | |
return (user_answer, animal) | |
# recursively dive into the right groups, attempting to | |
# identify the animal the user have in mind | |
def attempt_identify(trait): | |
user_answer = raw_input(trait.get_question()) | |
group1, group2 = trait.group1, trait.group2 | |
if user_answer == 'yes': | |
if isinstance(group1, Animal): | |
return is_animal(group1) | |
else: | |
return attempt_identify(group1) | |
else: | |
if isinstance(group2, Animal): | |
return is_animal(group2) | |
else: | |
return attempt_identify(group2) | |
# initial knowledge of the animals | |
parrot = Animal("parrot") | |
mouse = Animal("mouse") | |
wings = Trait("wings", parrot, mouse) | |
# root trait node, basically wings though it doesn't have to be | |
root = wings | |
# we re-write the tree structure to include the new animal | |
# along with the right trait so we can recognize it in the future! | |
# just some basic pointer manipulations to insert the new trait and animals into | |
# the old tree | |
def learn(last_animal_attempt, new_animal, new_trait): | |
# get the old parent trait, we need to change one of its children to the new one | |
old_parent = last_animal_attempt.parent | |
# create the new animal | |
new_animal_node = Animal(new_animal) | |
# here is the new trait that will distringuish the new animal with | |
# the last attempted animal that got wrong | |
new_trait_node = Trait(new_trait, new_animal_node, last_animal_attempt) | |
# now we change the child of the old parent trait | |
if old_parent.group1 == last_animal_attempt: | |
old_parent.group1 = new_trait_node | |
else: | |
old_parent.group2 = new_trait_node | |
# an interactive loop to grow our model | |
while(True): | |
raw_input('press any key to start new game') | |
print '\n' | |
print '===========lets play the animal game=============' | |
success, last_animal_attempt = attempt_identify(root) | |
if success == 'yes': | |
print 'haha I win! (o^^)o' | |
else: | |
prompt_animal = 'fascinating! please tell me what animal is this?\n' | |
new_animal = raw_input(prompt_animal) | |
old_animal = last_animal_attempt.animal | |
prompt_trait = 'tell me a thing that '+new_animal+' has that '+old_animal+' does not \n' | |
new_trait = raw_input(prompt_trait) | |
print new_animal, 'has', new_trait,'? you dont say! I\'ll remember that!' | |
learn(last_animal_attempt, new_animal, new_trait) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment