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
+
================================================================================
|
| 3 |
+
INTERACTIVE FACTORIZATION v1.4 EN
|
| 4 |
+
================================================================================
|
| 5 |
+
Main Developer: Marcos Autuori | Author: Autuori
|
| 6 |
+
================================================================================
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import time
|
| 11 |
+
import wave
|
| 12 |
+
import struct
|
| 13 |
+
import math
|
| 14 |
+
import os
|
| 15 |
+
|
| 16 |
+
# Configuration and Data
|
| 17 |
+
GRID_NUMBERS = [
|
| 18 |
+
10, 12, 14, 15, 16, 18, 20, 21,
|
| 19 |
+
24, 25, 27, 28, 30, 32, 35, 36,
|
| 20 |
+
40, 42, 45, 48, 50, 54, 56, 60,
|
| 21 |
+
63, 64, 70, 72, 80, 81, 90, 100
|
| 22 |
+
]
|
| 23 |
+
KEYBOARD_KEYS = [2, 3, 4, 5, 6, 7, 8, 9, 10]
|
| 24 |
+
MAX_TIME = 150
|
| 25 |
+
|
| 26 |
+
# --- AUDIO GENERATION (Same logic as your original script) ---
|
| 27 |
+
def generate_wav(filename, freq, duration=0.1, volume=0.3):
|
| 28 |
+
sample_rate = 44100
|
| 29 |
+
n_samples = int(sample_rate * duration)
|
| 30 |
+
with wave.open(filename, 'w') as f:
|
| 31 |
+
f.setnchannels(1)
|
| 32 |
+
f.setsampwidth(2)
|
| 33 |
+
f.setframerate(sample_rate)
|
| 34 |
+
for i in range(n_samples):
|
| 35 |
+
val = int(volume * 32767.0 * math.sin(2.0 * math.pi * freq * i / sample_rate))
|
| 36 |
+
data = struct.pack('<h', val)
|
| 37 |
+
f.writeframesraw(data)
|
| 38 |
+
|
| 39 |
+
# Pre-generate sound files
|
| 40 |
+
if not os.path.exists("sounds"): os.makedirs("sounds")
|
| 41 |
+
generate_wav("sounds/click.wav", 440, 0.05)
|
| 42 |
+
generate_wav("sounds/correct.wav", 880, 0.2)
|
| 43 |
+
generate_wav("sounds/wrong.wav", 220, 0.3)
|
| 44 |
+
|
| 45 |
+
def get_empty_state():
|
| 46 |
+
return {
|
| 47 |
+
"solved": [False] * 32,
|
| 48 |
+
"selected_idx": None,
|
| 49 |
+
"current_factors": [],
|
| 50 |
+
"start_time": time.time(),
|
| 51 |
+
"game_over": False,
|
| 52 |
+
"score": MAX_TIME
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
def format_grid_button(idx, solved_list, selected_idx):
|
| 56 |
+
label = str(GRID_NUMBERS[idx])
|
| 57 |
+
if solved_list[idx]:
|
| 58 |
+
return gr.update(value=f"✅ {label}", variant="success", interactive=False)
|
| 59 |
+
if selected_idx == idx:
|
| 60 |
+
return gr.update(variant="primary", interactive=True)
|
| 61 |
+
return gr.update(value=label, variant="secondary", interactive=True)
|
| 62 |
+
|
| 63 |
+
def handle_game_logic(state, action_type, value=None):
|
| 64 |
+
if state["game_over"]:
|
| 65 |
+
return state, "Game Over!", None
|
| 66 |
+
|
| 67 |
+
feedback = "Select a number..."
|
| 68 |
+
sound = "sounds/click.wav" # Default sound
|
| 69 |
+
|
| 70 |
+
if action_type == "select_target":
|
| 71 |
+
state["selected_idx"] = value
|
| 72 |
+
state["current_factors"] = []
|
| 73 |
+
feedback = f"Factoring: {GRID_NUMBERS[value]}"
|
| 74 |
+
|
| 75 |
+
elif action_type == "select_factor":
|
| 76 |
+
if state["selected_idx"] is None:
|
| 77 |
+
feedback = "⚠️ Select a grid number first!"
|
| 78 |
+
else:
|
| 79 |
+
state["current_factors"].append(int(value))
|
| 80 |
+
if len(state["current_factors"]) == 2:
|
| 81 |
+
a, b = state["current_factors"]
|
| 82 |
+
target = GRID_NUMBERS[state["selected_idx"]]
|
| 83 |
+
|
| 84 |
+
if a * b == target:
|
| 85 |
+
state["solved"][state["selected_idx"]] = True
|
| 86 |
+
feedback = f"✔️ {a} x {b} = {target}!"
|
| 87 |
+
sound = "sounds/correct.wav"
|
| 88 |
+
state["selected_idx"] = None
|
| 89 |
+
else:
|
| 90 |
+
feedback = f"❌ {a}* {b} = {a*b} (Wrong for {target})."
|
| 91 |
+
sound = "sounds/wrong.wav"
|
| 92 |
+
state["current_factors"] = []
|
| 93 |
+
|
| 94 |
+
elapsed = int(time.time() - state["start_time"])
|
| 95 |
+
state["score"] = max(0, MAX_TIME - elapsed)
|
| 96 |
+
|
| 97 |
+
if all(state["solved"]):
|
| 98 |
+
state["game_over"] = True
|
| 99 |
+
feedback = "🏆 VICTORY!"
|
| 100 |
+
|
| 101 |
+
return state, feedback, sound
|
| 102 |
+
|
| 103 |
+
custom_css = """
|
| 104 |
+
.grid-btn { min-width: 80px !important; height: 80px !important; font-size: 20px !important; }
|
| 105 |
+
.kb-btn { min-width: 60px !important; height: 50px !important; }
|
| 106 |
+
"""
|
| 107 |
+
|
| 108 |
+
with gr.Blocks(title="Autuori - Factorization Game", css=custom_css) as demo:
|
| 109 |
+
state = gr.State(get_empty_state())
|
| 110 |
+
|
| 111 |
+
# Hidden audio component for sound effects
|
| 112 |
+
audio_player = gr.Audio(interactive=False, visible=False, autoplay=True)
|
| 113 |
+
|
| 114 |
+
gr.Markdown("# 🎮 Interactive Factorization v1.4 EN\n**Developed by Marcos Autuori**")
|
| 115 |
+
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column(scale=4):
|
| 118 |
+
grid_buttons = []
|
| 119 |
+
for r in range(4):
|
| 120 |
+
with gr.Row():
|
| 121 |
+
for c in range(8):
|
| 122 |
+
idx = r * 8 + c
|
| 123 |
+
btn = gr.Button(str(GRID_NUMBERS[idx]), elem_classes="grid-btn")
|
| 124 |
+
grid_buttons.append(btn)
|
| 125 |
+
|
| 126 |
+
with gr.Column(scale=1):
|
| 127 |
+
status_box = gr.Textbox(label="Status", value="Select a blue number", interactive=False)
|
| 128 |
+
score_display = gr.Number(label="Score", value=MAX_TIME, interactive=False)
|
| 129 |
+
keyboard_btns = [gr.Button(str(val), variant="secondary", elem_classes="kb-btn") for val in KEYBOARD_KEYS]
|
| 130 |
+
reset_btn = gr.Button("Restart", variant="stop")
|
| 131 |
+
|
| 132 |
+
def update_ui(s):
|
| 133 |
+
updates = [format_grid_button(i, s["solved"], s["selected_idx"]) for i in range(32)]
|
| 134 |
+
return updates + [s["score"]]
|
| 135 |
+
|
| 136 |
+
# Grid Click Events
|
| 137 |
+
for i, btn in enumerate(grid_buttons):
|
| 138 |
+
btn.click(
|
| 139 |
+
fn=lambda s, idx=i: handle_game_logic(s, "select_target", idx),
|
| 140 |
+
inputs=[state],
|
| 141 |
+
outputs=[state, status_box, audio_player]
|
| 142 |
+
).then(fn=update_ui, inputs=[state], outputs=grid_buttons + [score_display])
|
| 143 |
+
|
| 144 |
+
# Keyboard Click Events
|
| 145 |
+
for kb_btn in keyboard_btns:
|
| 146 |
+
kb_btn.click(
|
| 147 |
+
fn=lambda s, v=kb_btn.value: handle_game_logic(s, "select_factor", v),
|
| 148 |
+
inputs=[state],
|
| 149 |
+
outputs=[state, status_box, audio_player]
|
| 150 |
+
).then(fn=update_ui, inputs=[state], outputs=grid_buttons + [score_display])
|
| 151 |
+
|
| 152 |
+
def reset_all():
|
| 153 |
+
new_state = get_empty_state()
|
| 154 |
+
updates = [gr.update(value=str(num), variant="secondary", interactive=True) for num in GRID_NUMBERS]
|
| 155 |
+
return [new_state, "Game Restarted!", None] + updates + [MAX_TIME]
|
| 156 |
+
|
| 157 |
+
reset_btn.click(fn=reset_all, outputs=[state, status_box, audio_player] + grid_buttons + [score_display])
|
| 158 |
+
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
+
demo.launch()
|