MarcosAutuori commited on
Commit
a2f6240
·
verified ·
1 Parent(s): 0ff348b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -0
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+
4
+ # Dados idênticos ao jogo original
5
+ GRID_NUMBERS = [
6
+ 10, 12, 14, 15, 16, 18, 20, 21,
7
+ 24, 25, 27, 28, 30, 32, 35, 36,
8
+ 40, 42, 45, 48, 50, 54, 56, 60,
9
+ 63, 64, 70, 72, 80, 81, 90, 100
10
+ ]
11
+ KEYBOARD_KEYS = [2, 3, 4, 5, 6, 7, 8, 9, 10]
12
+ MAX_TIME = 150
13
+
14
+ def get_empty_state():
15
+ return {
16
+ "solved": [False] * 32,
17
+ "selected_idx": None,
18
+ "current_factors": [],
19
+ "start_time": time.time(),
20
+ "game_over": False,
21
+ "score": MAX_TIME
22
+ }
23
+
24
+ def format_grid_button(idx, solved_list, selected_idx):
25
+ label = str(GRID_NUMBERS[idx])
26
+ if solved_list[idx]:
27
+ return gr.update(value=f"✅ {label}", variant="success", interactive=False)
28
+ if selected_idx == idx:
29
+ return gr.update(variant="primary", interactive=True)
30
+ return gr.update(value=label, variant="secondary", interactive=True)
31
+
32
+ def handle_game_logic(state, action_type, value=None):
33
+ if state["game_over"]:
34
+ return state, gr.update()
35
+
36
+ feedback = ""
37
+
38
+ if action_type == "select_target":
39
+ state["selected_idx"] = value
40
+ state["current_factors"] = []
41
+ feedback = f"Fatorar o número: {GRID_NUMBERS[value]}"
42
+
43
+ elif action_type == "select_factor":
44
+ if state["selected_idx"] is None:
45
+ feedback = "Selecione um número na grade primeiro!"
46
+ else:
47
+ state["current_factors"].append(value)
48
+ feedback = f"Fatores: {' x '.join(map(str, state['current_factors']))}"
49
+
50
+ if len(state["current_factors"]) == 2:
51
+ a, b = state["current_factors"]
52
+ target = GRID_NUMBERS[state["selected_idx"]]
53
+
54
+ if a * b == target:
55
+ state["solved"][state["selected_idx"]] = True
56
+ feedback = "✔️ Correto!"
57
+ state["selected_idx"] = None
58
+ state["current_factors"] = []
59
+ else:
60
+ feedback = "❌ Incorreto! Tente novamente."
61
+ state["current_factors"] = []
62
+
63
+ # Atualiza Score e Verifica Fim de Jogo
64
+ elapsed = int(time.time() - state["start_time"])
65
+ state["score"] = max(0, MAX_TIME - elapsed)
66
+
67
+ if all(state["solved"]):
68
+ state["game_over"] = True
69
+ feedback = f"🏆 JOGO CONCLUÍDO! Pontuação Final: {state['score']}"
70
+
71
+ return state, feedback
72
+
73
+ with gr.Blocks(css=".grid-btn { height: 80px !important; font-size: 20px !important; }") as demo:
74
+ state = gr.State(get_empty_state())
75
+
76
+ gr.Markdown(f"# 🎮 Fatoração Interativa v1.4 EN\n**Objetivo:** Selecione um número e então seus dois fatores.")
77
+
78
+ with gr.Row():
79
+ with gr.Column(scale=2):
80
+ # Grade 4x8
81
+ grid_buttons = []
82
+ for r in range(4):
83
+ with gr.Row():
84
+ for c in range(8):
85
+ idx = r * 8 + c
86
+ btn = gr.Button(str(GRID_NUMBERS[idx]), elem_classes="grid-btn")
87
+ grid_buttons.append(btn)
88
+
89
+ with gr.Column(scale=1):
90
+ # Painel de Status
91
+ status_box = gr.Textbox(label="Status do Jogo", value="Selecione um número na grade", interactive=False)
92
+ score_display = gr.Number(label="Pontos (Tempo)", value=MAX_TIME, interactive=False)
93
+
94
+ gr.Markdown("### Teclado Numérico")
95
+ keyboard_btns = []
96
+ with gr.Row():
97
+ for i, val in enumerate(KEYBOARD_KEYS):
98
+ if i % 3 == 0 and i > 0:
99
+ gr.Markdown("---") # Quebra de linha visual
100
+ kb_btn = gr.Button(str(val), variant="secondary")
101
+ keyboard_btns.append(kb_btn)
102
+
103
+ reset_btn = gr.Button("Reiniciar Jogo", variant="stop")
104
+
105
+ # Eventos da Grade
106
+ for i, btn in enumerate(grid_buttons):
107
+ btn.click(
108
+ fn=lambda s, idx=i: handle_game_logic(s, "select_target", idx),
109
+ inputs=[state],
110
+ outputs=[state, status_box]
111
+ ).then(
112
+ fn=lambda s: [format_grid_button(j, s["solved"], s["selected_idx"]) for j in range(32)],
113
+ inputs=[state],
114
+ outputs=grid_buttons
115
+ ).then(
116
+ fn=lambda s: s["score"], inputs=[state], outputs=[score_display]
117
+ )
118
+
119
+ # Eventos do Teclado
120
+ for btn in keyboard_btns:
121
+ btn.click(
122
+ fn=lambda s, v=int(btn.value) if isinstance(btn.value, str) else btn.value: handle_game_logic(s, "select_factor", int(btn.value)),
123
+ inputs=[state],
124
+ outputs=[state, status_box]
125
+ ).then(
126
+ fn=lambda s: [format_grid_button(j, s["solved"], s["selected_idx"]) for j in range(32)],
127
+ inputs=[state],
128
+ outputs=grid_buttons
129
+ ).then(
130
+ fn=lambda s: s["score"], inputs=[state], outputs=[score_display]
131
+ )
132
+
133
+ # Reset
134
+ def reset_all():
135
+ new_state = get_empty_state()
136
+ return [new_state, "Jogo reiniciado!"] + [gr.update(value=str(GRID_NUMBERS[j]), variant="secondary", interactive=True) for j in range(32)] + [MAX_TIME]
137
+
138
+ reset_btn.click(
139
+ fn=reset_all,
140
+ outputs=[state, status_box] + grid_buttons + [score_display]
141
+ )
142
+
143
+ if __name__ == "__main__":
144
+ demo.launch()