Skip to content

Commit 1bdc78d

Browse files
authored
Merge pull request #4 from gmook9/dev
Fixed game logic and styling
2 parents f9602ca + 4b434b5 commit 1bdc78d

File tree

4 files changed

+54
-75
lines changed

4 files changed

+54
-75
lines changed

ai_bot.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
1-
import os
21
from random import choice
32
from langchain_community.llms import Ollama
4-
from dotenv import load_dotenv
53

6-
# Load environment variables from .env file
7-
load_dotenv()
4+
# TO-DO FIX THIS
5+
class QuestionGenerator:
6+
def __init__(self, ai_bot):
7+
self.ai_bot = ai_bot
8+
9+
def generate_random_question(self, case_synopsis):
10+
# Use the provided case synopsis instead of generating a new one
11+
question_prompt = (
12+
f"You are playing the role of a detective in a text-based detective game. Below is a fictional case synopsis:\n\n"
13+
f"'{case_synopsis}'\n\n"
14+
f"Your task is to generate a question that is appropriate for this scenario. "
15+
f"Remember, this is a fictional game setting, so avoid any references to real-life guidance or advice. "
16+
f"The question should relate only to the details provided in the synopsis and should aim to uncover more information about the case.\n"
17+
f"Please provide only the question."
18+
)
19+
return self.ai_bot.llm.invoke(question_prompt)
820

921
class AIBot:
1022
def __init__(self):
1123
self.llm = Ollama(model="llama3")
1224
self.role = choice(["guilty", "innocent"])
13-
self.prompt = os.getenv("PROMPT")
25+
self.prompt = "Generate a random crime scenario for a detective game in 3-5 sentences. Describe a situation that has just happened. At the end of your response, specify your role as the person being interviewed at the police station. You might be a witness, bystander, or actively involved, and you could be innocent or guilty. For example: (Interviewee: Wife). Also do not put anything like (Here's a random crime scenario:) in it."
1426
self.conversation_history = [] # Store previous questions and answers
27+
self.question_generator = QuestionGenerator(self) # Initialize the QuestionGenerator with AIBot
1528

1629
def generate_synopsis(self):
1730
return self.llm.invoke(self.prompt)
1831

1932
def respond(self, question):
20-
self.conversation_history.append(f"Question: {question}") # Add question to convo history
33+
self.conversation_history.append(f"Question: {question}") # Add question to convo history
2134
context = "\n".join(self.conversation_history)
2235
response_prompt = (
2336
f"Role: {self.role}. You are being questioned. "
2437
f"Here is the context so far:\n{context}\n\n"
25-
f"Now respond to the latest question: {question}"
38+
f"Now respond to the latest question: {question}. Only response in 1-2 setnences."
2639
)
27-
response = self.llm.invoke(response_prompt) # Get AI Response
28-
self.conversation_history.append(f"Response: {response}") # Add response to convo history
40+
response = self.llm.invoke(response_prompt) # Get AI Response
41+
self.conversation_history.append(f"Response: {response}") # Add response to convo history
2942

3043
return response
3144

3245
def is_guilty(self):
3346
return self.role == "guilty"
3447

35-
def generate_random_question(self):
36-
question_prompt = (
37-
"Generate a detective-style question related to a crime investigation, similar to these examples: "
38-
"\"Where were you on the night of the crime?\", "
39-
"\"Do you have an alibi?\", "
40-
"\"Why were you near the crime scene?\""
41-
)
42-
return self.llm.invoke(question_prompt)
48+
# Modify this method to accept synopsis as an argument
49+
def generate_random_question(self, case_synopsis):
50+
return self.question_generator.generate_random_question(case_synopsis)

game.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ def __init__(self):
1010
self.max_questions = 9
1111
self.questions_asked = 0
1212
self.exit_game = False # Flag to track if the game is exited
13+
self.synopsis = None # Variable to store the generated synopsis
1314

1415
def start(self):
15-
self.console.print("[bold green]Welcome, Detective![/bold green]")
16-
self.console.print() # Add space
17-
synopsis = self.ai_bot.generate_synopsis()
18-
self.console.print(Panel(synopsis, title="Case Synopsis"))
19-
self.console.print() # Add space
16+
# Welcome message
17+
self.console.print("[bold green]Welcome, Detective![/bold green]\n")
18+
self.console.print("[bold yellow]Loading, please wait...[/bold yellow]\n")
19+
20+
self.synopsis = self.ai_bot.generate_synopsis() # Store the synopsis
21+
self.console.print(Panel(self.synopsis, title="Case Synopsis"))
22+
self.console.print("\n")
2023

2124
while self.questions_asked < self.max_questions and not self.exit_game:
2225
action = self.select_action()
@@ -29,71 +32,64 @@ def start(self):
2932
break
3033

3134
if not self.exit_game:
32-
self.console.print() # Add space
33-
self.console.print("[bold red]Game over! You've used all your questions.[/bold red]")
35+
self.console.print("[bold red]Game over! You've used all your questions.[/bold red]\n")
3436

3537
def select_action(self):
3638
self.console.print("[bold blue]Select an action:[/bold blue]")
3739
self.console.print("1. Ask a question")
3840
self.console.print("2. Make a final decision")
39-
self.console.print("3. Exit")
40-
self.console.print() # Add space
41-
choice = input("Enter your choice (1/2/3): ").strip()
41+
self.console.print("3. Exit\n")
42+
choice = self.console.input("[bold yellow]Enter your choice (1/2/3): [/bold yellow]").strip()
4243
if choice == "1":
4344
return "Ask a question"
4445
elif choice == "2":
4546
return "Make a final decision"
4647
elif choice == "3":
4748
return "Exit"
4849
else:
49-
self.console.print("[bold red]Invalid choice. Please try again.[/bold red]")
50-
self.console.print() # Add space
50+
self.console.print("[bold red]Invalid choice. Please try again.[/bold red]\n")
5151
return self.select_action()
5252

5353
def ask_question(self):
54-
self.console.print("[bold blue]Question Type:[/bold blue]")
55-
self.console.print("1. Type my own")
56-
self.console.print("2. Random")
57-
self.console.print() # Add space
58-
choice = input("Enter your choice (1/2): ").strip()
54+
self.console.print("\n[bold magenta]Question Type:[/bold magenta]")
55+
self.console.print("1. Random")
56+
self.console.print("2. Type my own\n")
57+
choice = self.console.input("[bold yellow]Enter your choice (1/2): [/bold yellow]").strip()
5958

6059
if choice == "1":
61-
user_question = input("Type your question below: ").strip()
60+
user_question = self.ai_bot.generate_random_question(self.synopsis) # Pass the stored synopsis
6261
elif choice == "2":
63-
user_question = self.ai_bot.generate_random_question()
62+
user_question = self.console.input("Type your question below: ").strip()
6463
else:
65-
self.console.print("[bold red]Invalid choice. Please try again.[/bold red]")
66-
self.console.print() # Add space
64+
self.console.print("[bold red]Invalid choice. Please try again.[/bold red]\n")
6765
return self.ask_question()
6866

6967
self.questions_asked += 1
70-
self.console.print() # Add space
68+
self.console.print("\n")
7169
self.console.print(Text(f"Question {self.questions_asked}: {user_question}", style="bold red"))
7270
response = self.ai_bot.respond(user_question)
7371
self.console.print(Panel(response, title=f"Response [{self.questions_asked}/{self.max_questions}]"))
74-
self.console.print() # Add space
72+
self.console.print("\n")
7573

7674
def make_final_decision(self):
77-
self.console.print("[bold blue]Final Decision:[/bold blue]")
75+
self.console.print("\n[bold bright_magenta]Final Decision:[/bold bright_magenta]")
7876
self.console.print("1. Release")
79-
self.console.print("2. Throw behind bars")
80-
self.console.print() # Add space
81-
choice = input("Enter your choice (1/2): ").strip()
77+
self.console.print("2. Throw behind bars\n")
78+
choice = self.console.input("[bold yellow]Enter your choice (1/2): [/bold yellow]").strip()
8279

8380
if choice == "1":
8481
decision = "Release"
8582
elif choice == "2":
8683
decision = "Throw behind bars"
8784
else:
88-
self.console.print("[bold red]Invalid choice. Please try again.[/bold red]")
89-
self.console.print() # Add space
85+
self.console.print("[bold red]Invalid choice. Please try again.[/bold red]\n")
9086
return self.make_final_decision()
9187

92-
self.console.print() # Add space
88+
self.console.print("\n")
9389
if self.ai_bot.is_guilty() and decision == "Throw behind bars":
94-
self.console.print("[bold green]Correct! The suspect was guilty.[/bold green]")
90+
self.console.print("[bold green]Correct! The suspect was guilty.[/bold green]\n")
9591
elif not self.ai_bot.is_guilty() and decision == "Release":
96-
self.console.print("[bold green]Correct! The suspect was innocent.[/bold green]")
92+
self.console.print("[bold green]Correct! The suspect was innocent.[/bold green]\n")
9793
else:
98-
self.console.print("[bold red]Wrong decision! You lost.[/bold red]")
94+
self.console.print("[bold red]Wrong decision! You lost.[/bold red]\n")
9995
exit()

questions.json

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

questions.py

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

0 commit comments

Comments
 (0)