0Learn commited on
Commit
39ed164
·
verified ·
1 Parent(s): 614f34a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -3
app.py CHANGED
@@ -2,18 +2,139 @@ import gradio as gr
2
  import random
3
  import time
4
  import threading
 
5
 
6
- # (Keep all the previous code for game_state, money_levels, timer_prompts, questions, and game logic functions)
 
 
 
 
 
 
 
 
 
 
7
 
8
- # ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Modify the chat function to clear the input
11
  def chat(message, history):
12
  history = history or []
13
  bot_message = get_bot_response(message)
14
  history.append((message, bot_message))
15
  return history, ""
16
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Create the Gradio interface
18
  with gr.Blocks() as demo:
19
  chatbot = gr.Chatbot()
 
2
  import random
3
  import time
4
  import threading
5
+ from questions import questions
6
 
7
+ # Game state
8
+ game_state = {
9
+ "game_started": False,
10
+ "current_question": None,
11
+ "money": 0,
12
+ "lifelines": {"fifty_fifty": True, "skip": True, "clue": True},
13
+ "safe_levels": [5, 10],
14
+ "timer": 60,
15
+ "timer_active": False,
16
+ "questions_asked": set()
17
+ }
18
 
19
+ # Money levels
20
+ money_levels = [100, 200, 300, 500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, 125000, 250000, 500000, 1000000]
21
+
22
+ # Timer prompts
23
+ timer_prompts = [
24
+ "Tick tock! 50 seconds left. No pressure, but also... lots of pressure!",
25
+ "40 seconds remaining. Is that your final answer? (No, seriously, is it?)",
26
+ "30 seconds on the clock! Time flies when you're trying to be a millionaire!",
27
+ "20 seconds left! Don't let the clock psyche you out!",
28
+ "Only 10 seconds remaining! Quick, phone a fr... oh wait, wrong lifeline!",
29
+ ]
30
+
31
+ def start_timer():
32
+ game_state["timer"] = 60
33
+ game_state["timer_active"] = True
34
+ while game_state["timer"] > 0 and game_state["timer_active"]:
35
+ time.sleep(1)
36
+ game_state["timer"] -= 1
37
+
38
+ def get_random_question():
39
+ available_questions = set(range(len(questions))) - game_state["questions_asked"]
40
+ if not available_questions:
41
+ return None
42
+ question_index = random.choice(list(available_questions))
43
+ game_state["questions_asked"].add(question_index)
44
+ return questions[question_index]
45
+
46
+ def get_bot_response(message):
47
+ message = message.lower().strip()
48
+
49
+ if not game_state["game_started"]:
50
+ if "start game" in message or "play game" in message:
51
+ game_state["game_started"] = True
52
+ game_state["current_question"] = None
53
+ game_state["money"] = 0
54
+ game_state["lifelines"] = {"fifty_fifty": True, "skip": True, "clue": True}
55
+ game_state["questions_asked"] = set()
56
+ return "Welcome to Who Wants to Be a Millionaire: Slang Edition! Let's start with the first question. Type 'ready' when you're set to go!"
57
+ else:
58
+ return "I'm a Who Wants to Be a Millionaire: Slang Edition game bot. Type 'start game' to begin playing!"
59
+
60
+ if message == "ready":
61
+ game_state["current_question"] = get_random_question()
62
+ if game_state["current_question"] is None:
63
+ game_state["game_started"] = False
64
+ return "Congratulations! You've answered all available questions. Your final score is ${game_state['money']}. Type 'start game' to play again!"
65
+
66
+ question = game_state["current_question"]
67
+ options = "\n".join(question["options"])
68
+ threading.Thread(target=start_timer).start()
69
+ return f"Question for ${money_levels[len(game_state['questions_asked']) - 1]}:\n{question['question']}\n{options}\nLifelines available: {', '.join([k for k, v in game_state['lifelines'].items() if v])}\nYou have 60 seconds. Go!"
70
+
71
+ if message in ["a", "b", "c", "d"]:
72
+ game_state["timer_active"] = False
73
+ question = game_state["current_question"]
74
+ if message.upper() == question["correct"]:
75
+ game_state["money"] = money_levels[len(game_state["questions_asked"]) - 1]
76
+ if len(game_state["questions_asked"]) == len(questions):
77
+ game_state["game_started"] = False
78
+ return f"Congratulations! You've won the grand prize of $1,000,000! Game over. Type 'start game' to play again."
79
+ elif len(game_state["questions_asked"]) in game_state["safe_levels"]:
80
+ return f"Correct! You've reached a safe level of ${game_state['money']}. Type 'ready' for the next question."
81
+ else:
82
+ return f"Correct! You've won ${game_state['money']}. Type 'ready' for the next question or 'quit' to leave with your winnings."
83
+ else:
84
+ game_state["game_started"] = False
85
+ safe_money = max([money_levels[level - 1] for level in game_state["safe_levels"] if level <= len(game_state["questions_asked"])], default=0)
86
+ return f"I'm sorry, that's incorrect. The correct answer was {question['correct']}. You leave with ${safe_money}. Game over. Type 'start game' to play again."
87
+
88
+ if message == "quit":
89
+ game_state["timer_active"] = False
90
+ game_state["game_started"] = False
91
+ return f"You've decided to quit. You leave with ${game_state['money']}. Thanks for playing! Type 'start game' to play again."
92
+
93
+ if message == "fifty_fifty" and game_state["lifelines"]["fifty_fifty"]:
94
+ game_state["lifelines"]["fifty_fifty"] = False
95
+ question = game_state["current_question"]
96
+ correct = question["correct"]
97
+ options = [opt[0].upper() for opt in question["options"]]
98
+ options.remove(correct)
99
+ remove = random.sample(options, 2)
100
+ new_options = [opt for opt in question["options"] if opt[0].upper() not in remove]
101
+ return f"Fifty-Fifty used. Remaining options:\n" + "\n".join(new_options)
102
+
103
+ if message == "skip" and game_state["lifelines"]["skip"]:
104
+ game_state["lifelines"]["skip"] = False
105
+ game_state["timer_active"] = False
106
+ return "Skip used. Moving to the next question. Type 'ready' to see it."
107
+
108
+ if message == "clue" and game_state["lifelines"]["clue"]:
109
+ game_state["lifelines"]["clue"] = False
110
+ question = game_state["current_question"]
111
+ correct = question["correct"]
112
+ options = [opt[0].upper() for opt in question["options"]]
113
+ options.remove(correct)
114
+ remove = random.choice(options)
115
+ new_options = [opt for opt in question["options"] if opt[0].upper() != remove]
116
+ return f"Clue used. One wrong option removed. Remaining options:\n" + "\n".join(new_options)
117
+
118
+ return "I don't understand that command. Type 'ready' for the question, A/B/C/D to answer, or use a lifeline (fifty_fifty/skip/clue)."
119
 
 
120
  def chat(message, history):
121
  history = history or []
122
  bot_message = get_bot_response(message)
123
  history.append((message, bot_message))
124
  return history, ""
125
 
126
+ def timer_update(history):
127
+ if game_state["timer_active"]:
128
+ if game_state["timer"] == 0:
129
+ game_state["timer_active"] = False
130
+ game_state["game_started"] = False
131
+ safe_money = max([money_levels[level - 1] for level in game_state["safe_levels"] if level <= len(game_state["questions_asked"])], default=0)
132
+ return history + [("System", f"Time's up! You've run out of time. You leave with ${safe_money}. Game over. Type 'start game' to play again.")]
133
+ elif game_state["timer"] % 10 == 0:
134
+ prompt_index = 5 - (game_state["timer"] // 10)
135
+ return history + [("System", timer_prompts[prompt_index])]
136
+ return history
137
+
138
  # Create the Gradio interface
139
  with gr.Blocks() as demo:
140
  chatbot = gr.Chatbot()