Upload 2 files
Browse files- app.py +95 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
import numpy as np
|
| 4 |
+
import simpleaudio as sa
|
| 5 |
+
|
| 6 |
+
class FactorizationGame:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.score = 0
|
| 9 |
+
self.start_time = None
|
| 10 |
+
self.grid_size = (4, 8)
|
| 11 |
+
self.numbers = list(range(2, 11))
|
| 12 |
+
self.target_number = None
|
| 13 |
+
self.user_input = None
|
| 14 |
+
self.sound_buffer = None
|
| 15 |
+
|
| 16 |
+
def generate_sound(self, number):
|
| 17 |
+
frequency = 440 + (number - 2) * 20 # Simple sound frequency mapping
|
| 18 |
+
t = np.linspace(0, 0.5, int(44100 * 0.5), False)
|
| 19 |
+
tone = np.sin(frequency * t * 2 * np.pi)
|
| 20 |
+
tone *= 32767 / np.max(np.abs(tone)) # Scale to int16
|
| 21 |
+
self.sound_buffer = tone.astype(np.int16)
|
| 22 |
+
|
| 23 |
+
def generate_target(self):
|
| 24 |
+
self.target_number = np.random.choice(self.numbers)
|
| 25 |
+
self.generate_sound(self.target_number) # Generate sound for the target
|
| 26 |
+
|
| 27 |
+
def check_answer(self):
|
| 28 |
+
if self.user_input is not None and self.target_number:
|
| 29 |
+
if self.user_input in self.factorize(self.target_number):
|
| 30 |
+
feedback = "Correct!"
|
| 31 |
+
self.score += 1
|
| 32 |
+
else:
|
| 33 |
+
feedback = "Incorrect."
|
| 34 |
+
self.user_input = None
|
| 35 |
+
self.generate_target() # Update target number
|
| 36 |
+
return feedback, self.score
|
| 37 |
+
|
| 38 |
+
def factorize(self, num):
|
| 39 |
+
factors = []
|
| 40 |
+
for i in range(2, num + 1):
|
| 41 |
+
if num % i == 0:
|
| 42 |
+
factors.append(i)
|
| 43 |
+
return factors[:3] # Return first three factors for simplicity
|
| 44 |
+
|
| 45 |
+
def start_game(self):
|
| 46 |
+
self.start_time = time.time()
|
| 47 |
+
self.score = 0
|
| 48 |
+
self.generate_target() # Start with a target number
|
| 49 |
+
|
| 50 |
+
def get_time_remaining(self):
|
| 51 |
+
elapsed_time = time.time() - self.start_time
|
| 52 |
+
return max(0, 150 - elapsed_time) # Returns time remaining in seconds
|
| 53 |
+
|
| 54 |
+
def get_game_state(self):
|
| 55 |
+
return self.target_number, self.get_time_remaining(), self.score
|
| 56 |
+
|
| 57 |
+
def play_sound(self):
|
| 58 |
+
if self.sound_buffer is not None:
|
| 59 |
+
wave_obj = sa.WaveObject(self.sound_buffer, 1, 2, 44100)
|
| 60 |
+
wave_obj.play()
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def main():
|
| 64 |
+
game = FactorizationGame()
|
| 65 |
+
|
| 66 |
+
def reset_game():
|
| 67 |
+
game.start_game()
|
| 68 |
+
return game.get_game_state()
|
| 69 |
+
|
| 70 |
+
def answer(number):
|
| 71 |
+
game.user_input = number
|
| 72 |
+
feedback, score = game.check_answer()
|
| 73 |
+
game.play_sound() # Play sound feedback
|
| 74 |
+
return feedback, game.get_game_state()
|
| 75 |
+
|
| 76 |
+
with gr.Blocks() as demo:
|
| 77 |
+
gr.Markdown("# Factorization Game")
|
| 78 |
+
with gr.Row():
|
| 79 |
+
target_display = gr.Textbox(label="Target Number", interactive=False)
|
| 80 |
+
time_display = gr.Textbox(label="Time Remaining", interactive=False)
|
| 81 |
+
score_display = gr.Textbox(label="Score", interactive=False)
|
| 82 |
+
with gr.Row():
|
| 83 |
+
number_buttons = [gr.Button(str(num), elem_id=str(num)) for num in game.numbers]
|
| 84 |
+
for button in number_buttons:
|
| 85 |
+
button.click(fn=answer, inputs=button, outputs=["feedback", (target_display, time_display, score_display)])
|
| 86 |
+
reset_btn = gr.Button("Reset Game")
|
| 87 |
+
reset_btn.click(reset_game, outputs=[target_display, time_display, score_display])
|
| 88 |
+
|
| 89 |
+
# Initial state
|
| 90 |
+
reset_game()
|
| 91 |
+
|
| 92 |
+
demo.launch()
|
| 93 |
+
|
| 94 |
+
if __name__ == '__main__':
|
| 95 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.0.0
|
| 2 |
+
numpy==1.24.0
|
| 3 |
+
simpleaudio==1.1.24
|
| 4 |
+
pygame==2.5.2
|