From f693cb88bdb44a81584b9506c543dd9f34402daf Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Mon, 1 Mar 2021 16:25:14 -0300 Subject: [PATCH 1/9] Added main code of animal game (not fully tested) --- 03 Animal/python/animal.py | 94 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 03 Animal/python/animal.py diff --git a/03 Animal/python/animal.py b/03 Animal/python/animal.py new file mode 100644 index 00000000..50d9b115 --- /dev/null +++ b/03 Animal/python/animal.py @@ -0,0 +1,94 @@ +class Node: + """ + Nodes of the binary tree of questions. + """ + + def __init__(self, text, yes_node, no_node, is_leaf): + self.text = text + self.yes_node = yes_node + self.no_node = no_node + # the nodes that are leafs have as text the animal's name + self.is_leaf = is_leaf + + def update_node(self, new_question, answer_new_ques, new_animal): + # update the yes or no leaf with a question + old_animal = self.text + # we replace the animal with a new question + self.text = new_question + + if answer_new_ques == 'y': + self.yes_node = Node(new_animal, None, None, True) + self.no_node = Node(old_animal, None, None, True) + else: + self.yes_node = Node(old_animal, None, None, True) + self.no_node = Node(new_animal, None, None, True) + + self.is_leaf = False + + +def parse_input(message): + # only accepts yes or no inputs + correct_input = False + while not correct_input: + try: + inp = input(message) + token = inp[0].lower() + if token == 'y' or token == 'n': + correct_input = True + except IndexError: + pass + + return token + + +def avoid_void_input(message): + answer = '' + while answer == '': + answer = input(message) + return answer + + +# Initial tree +yes_child = Node('Fish', None, None, True) +no_child = Node('Bird', None, None, True) +root = Node('Does it swim?', yes_child, no_child, False) + +# Main loop of game +keep_playing = parse_input('Are you thinking of an animal?') == 'y' +while keep_playing: + keep_asking = True + # Start traversing the tree by the root + actual_node = root + + while keep_asking: + + if not actual_node.is_leaf: + # we have to keep asking i.e. traversing nodes + answer = parse_input(actual_node.text) + + if answer == 'y': + actual_node = actual_node.yes_node + else: + actual_node = actual_node.no_node + else: + # we have reached a possible answer + answer = parse_input('Is it a {}?'.format(actual_node.text)) + if answer == 'n': + # add the animal to the tree + new_animal = avoid_void_input( + 'The animal you were thinking of was a ?') + new_question = avoid_void_input( + 'Please type in a question that would distinguish a {} from a {}:'.format(new_animal, actual_node.text)) + print("new_animal:" + new_animal) + answer_new_question = parse_input( + 'for a {} the answer would be:'.format(new_animal)) + + actual_node.update_node( + new_question+'?', answer_new_question, new_animal) + + else: + print("Why not try another animal?") + + keep_asking = False + + keep_playing = parse_input('Are you thinking of an animal?') == 'y' From f2e9c3a346e424c05edc6c496f1e6391db31b1ea Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Mon, 1 Mar 2021 16:53:09 -0300 Subject: [PATCH 2/9] Updated Node tree data structure --- 03 Animal/python/animal.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/03 Animal/python/animal.py b/03 Animal/python/animal.py index 50d9b115..6f9cdffe 100644 --- a/03 Animal/python/animal.py +++ b/03 Animal/python/animal.py @@ -3,27 +3,29 @@ class Node: Nodes of the binary tree of questions. """ - def __init__(self, text, yes_node, no_node, is_leaf): + def __init__(self, text, yes_node, no_node): + # the nodes that are leafs have as text the animal's name, otherwise + # a yes/no question self.text = text self.yes_node = yes_node self.no_node = no_node - # the nodes that are leafs have as text the animal's name - self.is_leaf = is_leaf def update_node(self, new_question, answer_new_ques, new_animal): - # update the yes or no leaf with a question + # update the leaf with a question old_animal = self.text # we replace the animal with a new question self.text = new_question if answer_new_ques == 'y': - self.yes_node = Node(new_animal, None, None, True) - self.no_node = Node(old_animal, None, None, True) + self.yes_node = Node(new_animal, None, None) + self.no_node = Node(old_animal, None, None) else: - self.yes_node = Node(old_animal, None, None, True) - self.no_node = Node(new_animal, None, None, True) + self.yes_node = Node(old_animal, None, None) + self.no_node = Node(new_animal, None, None) - self.is_leaf = False + # the leafs have as children None + def is_leaf(self): + return self.yes_node == None and self.no_node == None def parse_input(message): @@ -49,9 +51,9 @@ def avoid_void_input(message): # Initial tree -yes_child = Node('Fish', None, None, True) -no_child = Node('Bird', None, None, True) -root = Node('Does it swim?', yes_child, no_child, False) +yes_child = Node('Fish', None, None) +no_child = Node('Bird', None, None) +root = Node('Does it swim?', yes_child, no_child) # Main loop of game keep_playing = parse_input('Are you thinking of an animal?') == 'y' @@ -62,7 +64,7 @@ while keep_playing: while keep_asking: - if not actual_node.is_leaf: + if not actual_node.is_leaf(): # we have to keep asking i.e. traversing nodes answer = parse_input(actual_node.text) @@ -74,12 +76,11 @@ while keep_playing: # we have reached a possible answer answer = parse_input('Is it a {}?'.format(actual_node.text)) if answer == 'n': - # add the animal to the tree + # add the new animal to the tree new_animal = avoid_void_input( 'The animal you were thinking of was a ?') new_question = avoid_void_input( 'Please type in a question that would distinguish a {} from a {}:'.format(new_animal, actual_node.text)) - print("new_animal:" + new_animal) answer_new_question = parse_input( 'for a {} the answer would be:'.format(new_animal)) From 7fe09d6d2345b3b14f66718ea7e006aeda691b4b Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Mon, 1 Mar 2021 18:51:39 -0300 Subject: [PATCH 3/9] Added List command and updated parse to recognize that operation --- 03 Animal/python/animal.py | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/03 Animal/python/animal.py b/03 Animal/python/animal.py index 6f9cdffe..da62a367 100644 --- a/03 Animal/python/animal.py +++ b/03 Animal/python/animal.py @@ -28,12 +28,34 @@ class Node: return self.yes_node == None and self.no_node == None -def parse_input(message): +def list_known_animals(root_node): + # Traversing the tree by recursion until we reach the leafs + if root_node == None: + return + + if root_node.is_leaf(): + print(root_node.text, end=' '*11) + return + + if root_node.yes_node: + list_known_animals(root_node.yes_node) + + if root_node.no_node: + list_known_animals(root_node.no_node) + + +def parse_input(message, check_list, root_node): # only accepts yes or no inputs correct_input = False while not correct_input: try: inp = input(message) + + if check_list and inp.lower() == 'list': + print('Animals I already know are:') + list_known_animals(root_node) + print('\n') + token = inp[0].lower() if token == 'y' or token == 'n': correct_input = True @@ -56,7 +78,7 @@ no_child = Node('Bird', None, None) root = Node('Does it swim?', yes_child, no_child) # Main loop of game -keep_playing = parse_input('Are you thinking of an animal?') == 'y' +keep_playing = parse_input('Are you thinking of an animal?', True, root) == 'y' while keep_playing: keep_asking = True # Start traversing the tree by the root @@ -66,7 +88,7 @@ while keep_playing: if not actual_node.is_leaf(): # we have to keep asking i.e. traversing nodes - answer = parse_input(actual_node.text) + answer = parse_input(actual_node.text, False, None) if answer == 'y': actual_node = actual_node.yes_node @@ -74,7 +96,8 @@ while keep_playing: actual_node = actual_node.no_node else: # we have reached a possible answer - answer = parse_input('Is it a {}?'.format(actual_node.text)) + answer = parse_input('Is it a {}?'.format( + actual_node.text), False, None) if answer == 'n': # add the new animal to the tree new_animal = avoid_void_input( @@ -82,7 +105,7 @@ while keep_playing: new_question = avoid_void_input( 'Please type in a question that would distinguish a {} from a {}:'.format(new_animal, actual_node.text)) answer_new_question = parse_input( - 'for a {} the answer would be:'.format(new_animal)) + 'for a {} the answer would be:'.format(new_animal), False, None) actual_node.update_node( new_question+'?', answer_new_question, new_animal) @@ -92,4 +115,5 @@ while keep_playing: keep_asking = False - keep_playing = parse_input('Are you thinking of an animal?') == 'y' + keep_playing = parse_input( + 'Are you thinking of an animal?', True, root) == 'y' From 2ec485c2b342efbf44f5ee3d3b7c384cb9c18086 Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Mon, 1 Mar 2021 19:18:24 -0300 Subject: [PATCH 4/9] Added initial message --- 03 Animal/python/animal.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/03 Animal/python/animal.py b/03 Animal/python/animal.py index da62a367..99e55f45 100644 --- a/03 Animal/python/animal.py +++ b/03 Animal/python/animal.py @@ -45,7 +45,7 @@ def list_known_animals(root_node): def parse_input(message, check_list, root_node): - # only accepts yes or no inputs + # only accepts yes or no inputs and recognizes list operation correct_input = False while not correct_input: try: @@ -72,13 +72,21 @@ def avoid_void_input(message): return answer +def initial_message(): + print(' '*32 + 'Animal') + print(' '*15 + 'Creative Computing Morristown, New Jersey\n') + print('Play ´Guess the Animal´') + print('Think of an animal and the computer will try to guess it.\n') + + # Initial tree yes_child = Node('Fish', None, None) no_child = Node('Bird', None, None) root = Node('Does it swim?', yes_child, no_child) # Main loop of game -keep_playing = parse_input('Are you thinking of an animal?', True, root) == 'y' +initial_message() +keep_playing = parse_input('Are you thinking of an animal? ', True, root) == 'y' while keep_playing: keep_asking = True # Start traversing the tree by the root @@ -96,16 +104,16 @@ while keep_playing: actual_node = actual_node.no_node else: # we have reached a possible answer - answer = parse_input('Is it a {}?'.format( + answer = parse_input('Is it a {}? '.format( actual_node.text), False, None) if answer == 'n': # add the new animal to the tree new_animal = avoid_void_input( - 'The animal you were thinking of was a ?') + 'The animal you were thinking of was a ? ') new_question = avoid_void_input( - 'Please type in a question that would distinguish a {} from a {}:'.format(new_animal, actual_node.text)) + 'Please type in a question that would distinguish a {} from a {}: '.format(new_animal, actual_node.text)) answer_new_question = parse_input( - 'for a {} the answer would be:'.format(new_animal), False, None) + 'for a {} the answer would be: '.format(new_animal), False, None) actual_node.update_node( new_question+'?', answer_new_question, new_animal) @@ -116,4 +124,4 @@ while keep_playing: keep_asking = False keep_playing = parse_input( - 'Are you thinking of an animal?', True, root) == 'y' + 'Are you thinking of an animal? ', True, root) == 'y' From cb61baaee1d8cacfa522c227b72dd2886273d387 Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Mon, 1 Mar 2021 16:25:14 -0300 Subject: [PATCH 5/9] Added main code of animal game (not fully tested) --- 03 Animal/python/animal.py | 94 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 03 Animal/python/animal.py diff --git a/03 Animal/python/animal.py b/03 Animal/python/animal.py new file mode 100644 index 00000000..50d9b115 --- /dev/null +++ b/03 Animal/python/animal.py @@ -0,0 +1,94 @@ +class Node: + """ + Nodes of the binary tree of questions. + """ + + def __init__(self, text, yes_node, no_node, is_leaf): + self.text = text + self.yes_node = yes_node + self.no_node = no_node + # the nodes that are leafs have as text the animal's name + self.is_leaf = is_leaf + + def update_node(self, new_question, answer_new_ques, new_animal): + # update the yes or no leaf with a question + old_animal = self.text + # we replace the animal with a new question + self.text = new_question + + if answer_new_ques == 'y': + self.yes_node = Node(new_animal, None, None, True) + self.no_node = Node(old_animal, None, None, True) + else: + self.yes_node = Node(old_animal, None, None, True) + self.no_node = Node(new_animal, None, None, True) + + self.is_leaf = False + + +def parse_input(message): + # only accepts yes or no inputs + correct_input = False + while not correct_input: + try: + inp = input(message) + token = inp[0].lower() + if token == 'y' or token == 'n': + correct_input = True + except IndexError: + pass + + return token + + +def avoid_void_input(message): + answer = '' + while answer == '': + answer = input(message) + return answer + + +# Initial tree +yes_child = Node('Fish', None, None, True) +no_child = Node('Bird', None, None, True) +root = Node('Does it swim?', yes_child, no_child, False) + +# Main loop of game +keep_playing = parse_input('Are you thinking of an animal?') == 'y' +while keep_playing: + keep_asking = True + # Start traversing the tree by the root + actual_node = root + + while keep_asking: + + if not actual_node.is_leaf: + # we have to keep asking i.e. traversing nodes + answer = parse_input(actual_node.text) + + if answer == 'y': + actual_node = actual_node.yes_node + else: + actual_node = actual_node.no_node + else: + # we have reached a possible answer + answer = parse_input('Is it a {}?'.format(actual_node.text)) + if answer == 'n': + # add the animal to the tree + new_animal = avoid_void_input( + 'The animal you were thinking of was a ?') + new_question = avoid_void_input( + 'Please type in a question that would distinguish a {} from a {}:'.format(new_animal, actual_node.text)) + print("new_animal:" + new_animal) + answer_new_question = parse_input( + 'for a {} the answer would be:'.format(new_animal)) + + actual_node.update_node( + new_question+'?', answer_new_question, new_animal) + + else: + print("Why not try another animal?") + + keep_asking = False + + keep_playing = parse_input('Are you thinking of an animal?') == 'y' From 42ff2c852575c9ebe3f72ed3b3689c807f2920d4 Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Mon, 1 Mar 2021 16:53:09 -0300 Subject: [PATCH 6/9] Updated Node tree data structure --- 03 Animal/python/animal.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/03 Animal/python/animal.py b/03 Animal/python/animal.py index 50d9b115..6f9cdffe 100644 --- a/03 Animal/python/animal.py +++ b/03 Animal/python/animal.py @@ -3,27 +3,29 @@ class Node: Nodes of the binary tree of questions. """ - def __init__(self, text, yes_node, no_node, is_leaf): + def __init__(self, text, yes_node, no_node): + # the nodes that are leafs have as text the animal's name, otherwise + # a yes/no question self.text = text self.yes_node = yes_node self.no_node = no_node - # the nodes that are leafs have as text the animal's name - self.is_leaf = is_leaf def update_node(self, new_question, answer_new_ques, new_animal): - # update the yes or no leaf with a question + # update the leaf with a question old_animal = self.text # we replace the animal with a new question self.text = new_question if answer_new_ques == 'y': - self.yes_node = Node(new_animal, None, None, True) - self.no_node = Node(old_animal, None, None, True) + self.yes_node = Node(new_animal, None, None) + self.no_node = Node(old_animal, None, None) else: - self.yes_node = Node(old_animal, None, None, True) - self.no_node = Node(new_animal, None, None, True) + self.yes_node = Node(old_animal, None, None) + self.no_node = Node(new_animal, None, None) - self.is_leaf = False + # the leafs have as children None + def is_leaf(self): + return self.yes_node == None and self.no_node == None def parse_input(message): @@ -49,9 +51,9 @@ def avoid_void_input(message): # Initial tree -yes_child = Node('Fish', None, None, True) -no_child = Node('Bird', None, None, True) -root = Node('Does it swim?', yes_child, no_child, False) +yes_child = Node('Fish', None, None) +no_child = Node('Bird', None, None) +root = Node('Does it swim?', yes_child, no_child) # Main loop of game keep_playing = parse_input('Are you thinking of an animal?') == 'y' @@ -62,7 +64,7 @@ while keep_playing: while keep_asking: - if not actual_node.is_leaf: + if not actual_node.is_leaf(): # we have to keep asking i.e. traversing nodes answer = parse_input(actual_node.text) @@ -74,12 +76,11 @@ while keep_playing: # we have reached a possible answer answer = parse_input('Is it a {}?'.format(actual_node.text)) if answer == 'n': - # add the animal to the tree + # add the new animal to the tree new_animal = avoid_void_input( 'The animal you were thinking of was a ?') new_question = avoid_void_input( 'Please type in a question that would distinguish a {} from a {}:'.format(new_animal, actual_node.text)) - print("new_animal:" + new_animal) answer_new_question = parse_input( 'for a {} the answer would be:'.format(new_animal)) From bf7028276b24cc6ed570741c81534698ddf0dec4 Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Mon, 1 Mar 2021 18:51:39 -0300 Subject: [PATCH 7/9] Added List command and updated parse to recognize that operation --- 03 Animal/python/animal.py | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/03 Animal/python/animal.py b/03 Animal/python/animal.py index 6f9cdffe..da62a367 100644 --- a/03 Animal/python/animal.py +++ b/03 Animal/python/animal.py @@ -28,12 +28,34 @@ class Node: return self.yes_node == None and self.no_node == None -def parse_input(message): +def list_known_animals(root_node): + # Traversing the tree by recursion until we reach the leafs + if root_node == None: + return + + if root_node.is_leaf(): + print(root_node.text, end=' '*11) + return + + if root_node.yes_node: + list_known_animals(root_node.yes_node) + + if root_node.no_node: + list_known_animals(root_node.no_node) + + +def parse_input(message, check_list, root_node): # only accepts yes or no inputs correct_input = False while not correct_input: try: inp = input(message) + + if check_list and inp.lower() == 'list': + print('Animals I already know are:') + list_known_animals(root_node) + print('\n') + token = inp[0].lower() if token == 'y' or token == 'n': correct_input = True @@ -56,7 +78,7 @@ no_child = Node('Bird', None, None) root = Node('Does it swim?', yes_child, no_child) # Main loop of game -keep_playing = parse_input('Are you thinking of an animal?') == 'y' +keep_playing = parse_input('Are you thinking of an animal?', True, root) == 'y' while keep_playing: keep_asking = True # Start traversing the tree by the root @@ -66,7 +88,7 @@ while keep_playing: if not actual_node.is_leaf(): # we have to keep asking i.e. traversing nodes - answer = parse_input(actual_node.text) + answer = parse_input(actual_node.text, False, None) if answer == 'y': actual_node = actual_node.yes_node @@ -74,7 +96,8 @@ while keep_playing: actual_node = actual_node.no_node else: # we have reached a possible answer - answer = parse_input('Is it a {}?'.format(actual_node.text)) + answer = parse_input('Is it a {}?'.format( + actual_node.text), False, None) if answer == 'n': # add the new animal to the tree new_animal = avoid_void_input( @@ -82,7 +105,7 @@ while keep_playing: new_question = avoid_void_input( 'Please type in a question that would distinguish a {} from a {}:'.format(new_animal, actual_node.text)) answer_new_question = parse_input( - 'for a {} the answer would be:'.format(new_animal)) + 'for a {} the answer would be:'.format(new_animal), False, None) actual_node.update_node( new_question+'?', answer_new_question, new_animal) @@ -92,4 +115,5 @@ while keep_playing: keep_asking = False - keep_playing = parse_input('Are you thinking of an animal?') == 'y' + keep_playing = parse_input( + 'Are you thinking of an animal?', True, root) == 'y' From cb20c15585a5f39472168e1091291e9806e34e08 Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Mon, 1 Mar 2021 19:18:24 -0300 Subject: [PATCH 8/9] Added initial message --- 03 Animal/python/animal.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/03 Animal/python/animal.py b/03 Animal/python/animal.py index da62a367..99e55f45 100644 --- a/03 Animal/python/animal.py +++ b/03 Animal/python/animal.py @@ -45,7 +45,7 @@ def list_known_animals(root_node): def parse_input(message, check_list, root_node): - # only accepts yes or no inputs + # only accepts yes or no inputs and recognizes list operation correct_input = False while not correct_input: try: @@ -72,13 +72,21 @@ def avoid_void_input(message): return answer +def initial_message(): + print(' '*32 + 'Animal') + print(' '*15 + 'Creative Computing Morristown, New Jersey\n') + print('Play ´Guess the Animal´') + print('Think of an animal and the computer will try to guess it.\n') + + # Initial tree yes_child = Node('Fish', None, None) no_child = Node('Bird', None, None) root = Node('Does it swim?', yes_child, no_child) # Main loop of game -keep_playing = parse_input('Are you thinking of an animal?', True, root) == 'y' +initial_message() +keep_playing = parse_input('Are you thinking of an animal? ', True, root) == 'y' while keep_playing: keep_asking = True # Start traversing the tree by the root @@ -96,16 +104,16 @@ while keep_playing: actual_node = actual_node.no_node else: # we have reached a possible answer - answer = parse_input('Is it a {}?'.format( + answer = parse_input('Is it a {}? '.format( actual_node.text), False, None) if answer == 'n': # add the new animal to the tree new_animal = avoid_void_input( - 'The animal you were thinking of was a ?') + 'The animal you were thinking of was a ? ') new_question = avoid_void_input( - 'Please type in a question that would distinguish a {} from a {}:'.format(new_animal, actual_node.text)) + 'Please type in a question that would distinguish a {} from a {}: '.format(new_animal, actual_node.text)) answer_new_question = parse_input( - 'for a {} the answer would be:'.format(new_animal), False, None) + 'for a {} the answer would be: '.format(new_animal), False, None) actual_node.update_node( new_question+'?', answer_new_question, new_animal) @@ -116,4 +124,4 @@ while keep_playing: keep_asking = False keep_playing = parse_input( - 'Are you thinking of an animal?', True, root) == 'y' + 'Are you thinking of an animal? ', True, root) == 'y' From aa6b075165ca6c01aa3d9bfa6574a0ff5a7f5530 Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Mon, 1 Mar 2021 20:39:40 -0300 Subject: [PATCH 9/9] Added history comment and porting notes --- 03 Animal/python/animal.py | 69 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/03 Animal/python/animal.py b/03 Animal/python/animal.py index 99e55f45..65263917 100644 --- a/03 Animal/python/animal.py +++ b/03 Animal/python/animal.py @@ -1,6 +1,51 @@ +######################################################## +# +# Animal +# +# From: Basic computer Games(1978) +# +# Unlike other computer games in which the computer +# picks a number or letter and you must guess what it is, +# in this game you think of an animal and the computer asks +# you questions and tries to guess the name of your animal. +# If the computer guesses incorrectly, it will ask you for a +# question that differentiates the animal it guessed +# from the one you were thinking of. In this way the +# computer "learns" new animals. Questions to differentiate +# new animals should be input without a question mark. +# This version of the game does not have a SAVE feature. +# If your sistem allows, you may modify the program to +# save array A$, then reload the array when you want +# to play the game again. This way you can save what the +# computer learns over a series of games. +# At any time if you reply 'LIST' to the question "ARE YOU +# THINKING OF AN ANIMAL", the computer will tell you all the +# animals it knows so far. +# The program starts originally by knowing only FISH and BIRD. +# As you build up a file of animals you should use broad, +# general questions first and then narrow down to more specific +# ones with later animals. For example, If an elephant was to be +# your first animal, the computer would ask for a question to distinguish +# an elephant from a bird. Naturally there are hundreds of possibilities, +# however, if you plan to build a large file of animals a good question +# would be "IS IT A MAMAL". +# This program can be easily modified to deal with categories of +# things other than animals by simply modifying the initial data +# in Line 530 and the dialogue references to animal in Lines 10, +# 40, 50, 130, 230, 240 and 600. In an educational environment, this +# would be a valuable program to teach the distinguishing chacteristics +# of many classes of objects -- rock formations, geography, marine life, +# cell structures, etc. +# Originally developed by Arthur Luehrmann at Dartmouth College, +# Animal was subsequently shortened and modified by Nathan Teichholtz at +# DEC and Steve North at Creative Computing +# +######################################################## + + class Node: """ - Nodes of the binary tree of questions. + Node of the binary tree of questions. """ def __init__(self, text, yes_node, no_node): @@ -86,7 +131,8 @@ root = Node('Does it swim?', yes_child, no_child) # Main loop of game initial_message() -keep_playing = parse_input('Are you thinking of an animal? ', True, root) == 'y' +keep_playing = parse_input( + 'Are you thinking of an animal? ', True, root) == 'y' while keep_playing: keep_asking = True # Start traversing the tree by the root @@ -125,3 +171,22 @@ while keep_playing: keep_playing = parse_input( 'Are you thinking of an animal? ', True, root) == 'y' + + +######################################################## +# Porting Notes +# +# The data structure used for storing questions and +# animals is a binary tree where each non-leaf node +# has a question, while the leafs store the animals. +# +# As the original program, this program doesn't store +# old questions and animals. A good modification would +# be to add a database to store the tree. +# Also as the original program, this one can be easily +# modified to not only make guesses about animals, by +# modyfing the initial data of the tree, the questions +# that are asked to the user and the initial message +# function (Lines 120 to 130, 135, 158, 160, 168, 173) + +########################################################