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/4] 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/4] 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/4] 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/4] 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'