MarcosAutuori commited on
Commit
3f125d8
ยท
verified ยท
1 Parent(s): c28973f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +133 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ import numpy as np
4
+
5
+ # =========================
6
+ # CONFIG
7
+ # =========================
8
+ grid = [
9
+ [10, 12, 14, 15, 16, 18, 20, 21],
10
+ [24, 25, 27, 28, 30, 32, 35, 36],
11
+ [40, 42, 45, 48, 50, 54, 56, 60],
12
+ [63, 64, 70, 72, 80, 81, 90, 100]
13
+ ]
14
+
15
+ max_time = 150
16
+
17
+ # =========================
18
+ # SOUND SYNTHESIS
19
+ # =========================
20
+ def tone(freq, duration=0.15, sr=44100):
21
+ t = np.linspace(0, duration, int(sr * duration), False)
22
+ wave = 0.5 * np.sin(2 * np.pi * freq * t)
23
+ return sr, wave.astype(np.float32)
24
+
25
+ def sound_click(): return tone(800, 0.05)
26
+ def sound_correct(): return tone(1000, 0.2)
27
+ def sound_error(): return tone(400, 0.2)
28
+ def sound_complete(): return tone(1200, 0.3)
29
+ def sound_button(): return tone(900, 0.1)
30
+
31
+ # =========================
32
+ # STATE
33
+ # =========================
34
+ def init_game():
35
+ return {
36
+ "solved": [[False]*8 for _ in range(4)],
37
+ "start_time": time.time(),
38
+ "selected": None,
39
+ "score": max_time,
40
+ "message": "Select a number"
41
+ }
42
+
43
+ # =========================
44
+ # LOGIC
45
+ # =========================
46
+ def render_grid(state):
47
+ text = ""
48
+ for i in range(4):
49
+ for j in range(8):
50
+ val = grid[i][j]
51
+ if state["solved"][i][j]:
52
+ text += f"[โœ”๏ธ {val}] "
53
+ else:
54
+ text += f"[{val}] "
55
+ text += "\n"
56
+ return text
57
+
58
+ def select_number(state, num):
59
+ state["selected"] = num
60
+ state["message"] = f"Factor: {num}"
61
+ return state, render_grid(state), state["message"], sound_click()
62
+
63
+ def check_factors(state, a, b):
64
+ if state["selected"] is None:
65
+ return state, render_grid(state), "Select a number first", None
66
+
67
+ num = state["selected"]
68
+
69
+ if a * b == num:
70
+ state["message"] = "โœ”๏ธ Correct!"
71
+ snd = sound_correct()
72
+
73
+ for i in range(4):
74
+ for j in range(8):
75
+ if grid[i][j] == num:
76
+ state["solved"][i][j] = True
77
+
78
+ if all(all(row) for row in state["solved"]):
79
+ snd = sound_complete()
80
+ state["message"] = "๐Ÿ† Game completed!"
81
+
82
+ else:
83
+ state["message"] = "โŒ Incorrect"
84
+ snd = sound_error()
85
+
86
+ state["selected"] = None
87
+
88
+ elapsed = int(time.time() - state["start_time"])
89
+ state["score"] = max(0, max_time - elapsed)
90
+
91
+ return state, render_grid(state), f'{state["message"]} | Score: {state["score"]}', snd
92
+
93
+ def restart():
94
+ state = init_game()
95
+ return state, render_grid(state), "๐Ÿ”„ New game started", sound_button()
96
+
97
+ # =========================
98
+ # UI
99
+ # =========================
100
+ state = gr.State(init_game())
101
+
102
+ with gr.Blocks() as app:
103
+ gr.Markdown("# ๐Ÿง ๐Ÿ”Š Factorization Trainer")
104
+
105
+ grid_display = gr.Textbox(label="Grid", lines=10)
106
+ msg = gr.Textbox(label="Status")
107
+
108
+ audio = gr.Audio(autoplay=True)
109
+
110
+ with gr.Row():
111
+ f1 = gr.Number(label="Factor 1")
112
+ f2 = gr.Number(label="Factor 2")
113
+
114
+ check_btn = gr.Button("Check")
115
+ restart_btn = gr.Button("Restart")
116
+
117
+ check_btn.click(
118
+ check_factors,
119
+ inputs=[state, f1, f2],
120
+ outputs=[state, grid_display, msg, audio]
121
+ )
122
+
123
+ restart_btn.click(
124
+ restart,
125
+ outputs=[state, grid_display, msg, audio]
126
+ )
127
+
128
+ app.load(
129
+ lambda: (init_game(), render_grid(init_game()), "Game started", None),
130
+ outputs=[state, grid_display, msg, audio]
131
+ )
132
+
133
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ numpy