Skip to content

Commit 511f769

Browse files
committed
Added AI_animal_game and deleted the morse code
1 parent 7c772cb commit 511f769

File tree

3 files changed

+81
-76
lines changed

3 files changed

+81
-76
lines changed

Game-Galore/AI_Guess_Animal.py

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

Game-Galore/morse.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

animals.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"questions": {"mammal": "his teeth"}, "animals": ["dog", "cat", "elephant", "tiger", "lion", "rabbit", "fish", "tiger"]}

0 commit comments

Comments
 (0)