|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +class AnimalGame: |
| 5 | + def __init__(self): |
| 6 | + # Load animal data from a JSON file or create a default one if it doesn't exist |
| 7 | + self.folder_name = 'Game-Galore/AI-Animal-Guess' |
| 8 | + self.data_file = os.path.join(self.folder_name, 'animals.json') |
| 9 | + self.animals = self.load_data() |
| 10 | + |
| 11 | + def load_data(self): |
| 12 | + """Load animal data from a JSON file.""" |
| 13 | + try: |
| 14 | + with open(self.data_file, 'r') as file: |
| 15 | + return json.load(file) |
| 16 | + except FileNotFoundError: |
| 17 | + return { |
| 18 | + 'questions': {}, |
| 19 | + 'animals': ['dog', 'cat', 'elephant', 'tiger', 'lion', 'rabbit', 'fish'] |
| 20 | + } |
| 21 | + |
| 22 | + def save_data(self): |
| 23 | + """Save animal data to a JSON file.""" |
| 24 | + with open(self.data_file, 'w') as file: |
| 25 | + json.dump(self.animals, file) |
| 26 | + |
| 27 | + def play_game(self): |
| 28 | + print("Think of an animal, and I will try to guess it!") |
| 29 | + self.ask_question('Is it a mammal?', 'mammal') |
| 30 | + |
| 31 | + def ask_question(self, question, key): |
| 32 | + """Ask a question and navigate through the game.""" |
| 33 | + answer = input(f"{question} (yes/no): ").strip().lower() |
| 34 | + |
| 35 | + if answer == 'yes': |
| 36 | + if key in self.animals['questions']: |
| 37 | + next_question = self.animals['questions'][key] |
| 38 | + self.ask_question(next_question, key) |
| 39 | + else: |
| 40 | + animal = input("What animal did you think of? ") |
| 41 | + self.animals['questions'][key] = input("What question would distinguish this animal? ") |
| 42 | + self.animals['animals'].append(animal) |
| 43 | + self.save_data() |
| 44 | + print(f"Got it! I'll remember that a {animal} is a {key}.") |
| 45 | + elif answer == 'no': |
| 46 | + print("I couldn't guess it. Can you help me learn?") |
| 47 | + new_animal = input("What was the animal? ") |
| 48 | + new_question = input(f"What question would distinguish a {new_animal} from a {key}? ") |
| 49 | + self.animals['questions'][key] = new_question |
| 50 | + self.animals['animals'].append(new_animal) |
| 51 | + self.save_data() |
| 52 | + print(f"Thanks! I'll remember that a {new_animal} is different from a {key}.") |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + game = AnimalGame() |
| 56 | + game.play_game() |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +# Game Instructions: |
| 64 | + |
| 65 | +# 1.) Think of an animal. |
| 66 | + |
| 67 | +# 2.) The AI will ask you yes/no questions to guess the animal. |
| 68 | + |
| 69 | +# 3.) If the AI can't guess it, you can teach it by providing the name of the animal and a distinguishing question. |
| 70 | + |
| 71 | +# 4.) The AI learns from your inputs and will improve its guessing ability over time. |
| 72 | + |
| 73 | + |
| 74 | +# Data Storage: |
| 75 | + |
| 76 | +# The game stores the questions and animals in a animals.json file. The first time you run the game, it will create this file with default data. |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
0 commit comments