MarcosAutuori commited on
Commit
bd268da
·
verified ·
1 Parent(s): 1ac1267

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -25
app.py CHANGED
@@ -1,7 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import time
3
 
4
- # Dados extraídos do seu factorization_game.py
5
  GRID_NUMBERS = [
6
  10, 12, 14, 15, 16, 18, 20, 21,
7
  24, 25, 27, 28, 30, 32, 35, 36,
@@ -12,6 +27,7 @@ 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,
@@ -31,22 +47,22 @@ def format_grid_button(idx, solved_list, selected_idx):
31
 
32
  def handle_game_logic(state, action_type, value=None):
33
  if state["game_over"]:
34
- return state, "Fim de jogo! Reinicie para jogar."
35
 
36
- feedback = "Selecione um número na grade..."
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(int(value))
48
  factors_str = " x ".join(map(str, state["current_factors"]))
49
- feedback = f"Multiplicando: {factors_str}"
50
 
51
  if len(state["current_factors"]) == 2:
52
  a, b = state["current_factors"]
@@ -54,44 +70,49 @@ def handle_game_logic(state, action_type, value=None):
54
 
55
  if a * b == target:
56
  state["solved"][state["selected_idx"]] = True
57
- feedback = f"✔️ {a} x {b} = {target}! Correto!"
58
  state["selected_idx"] = None
59
  state["current_factors"] = []
60
  else:
61
- feedback = f"❌ {a} x {b} = {a*b} (Errado para {target})."
62
  state["current_factors"] = []
63
 
 
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"🏆 COMPLETO! Pontuação: {state['score']}"
70
 
71
  return state, feedback
72
 
73
- # CSS customizado para garantir retângulos idênticos
74
  custom_css = """
75
  .grid-btn {
76
- min-width: 70px !important;
77
- height: 70px !important;
78
- font-size: 18px !important;
79
  margin: 2px !important;
80
  }
81
  .kb-btn {
82
- min-width: 50px !important;
83
  height: 50px !important;
84
  }
85
  """
86
 
87
- with gr.Blocks(title="Factorization Game v1.4", css=custom_css) as demo:
88
  state = gr.State(get_empty_state())
89
 
90
- gr.Markdown("# 🎮 Interactive Factorization v1.4 EN\n**Desenvolvedor:** Marcos Autuori")
 
 
 
 
91
 
92
  with gr.Row():
93
  with gr.Column(scale=4):
94
- # Grade 4x8 Uniforme
95
  grid_buttons = []
96
  for r in range(4):
97
  with gr.Row():
@@ -101,12 +122,12 @@ with gr.Blocks(title="Factorization Game v1.4", css=custom_css) as demo:
101
  grid_buttons.append(btn)
102
 
103
  with gr.Column(scale=1):
104
- status_box = gr.Textbox(label="Status", value="Selecione um número", interactive=False)
105
- score_display = gr.Number(label="Pontos", value=MAX_TIME, interactive=False)
 
106
 
107
- gr.Markdown("### Fatores")
108
  keyboard_btns = []
109
- # Organiza o teclado em 3 linhas para melhor visual no painel lateral
110
  for r_kb in range(3):
111
  with gr.Row():
112
  for c_kb in range(3):
@@ -114,13 +135,14 @@ with gr.Blocks(title="Factorization Game v1.4", css=custom_css) as demo:
114
  kb_btn = gr.Button(str(val), variant="secondary", elem_classes="kb-btn")
115
  keyboard_btns.append(kb_btn)
116
 
117
- reset_btn = gr.Button("Reiniciar", variant="stop")
118
 
 
119
  def update_ui(s):
120
  updates = [format_grid_button(i, s["solved"], s["selected_idx"]) for i in range(32)]
121
  return updates + [s["score"]]
122
 
123
- # Eventos da Grade
124
  for i, btn in enumerate(grid_buttons):
125
  btn.click(
126
  fn=lambda s, idx=i: handle_game_logic(s, "select_target", idx),
@@ -128,7 +150,7 @@ with gr.Blocks(title="Factorization Game v1.4", css=custom_css) as demo:
128
  outputs=[state, status_box]
129
  ).then(fn=update_ui, inputs=[state], outputs=grid_buttons + [score_display])
130
 
131
- # Eventos do Teclado (Correção de loop/closure)
132
  for kb_btn in keyboard_btns:
133
  kb_btn.click(
134
  fn=lambda s, v=kb_btn.value: handle_game_logic(s, "select_factor", v),
@@ -136,10 +158,11 @@ with gr.Blocks(title="Factorization Game v1.4", css=custom_css) as demo:
136
  outputs=[state, status_box]
137
  ).then(fn=update_ui, inputs=[state], outputs=grid_buttons + [score_display])
138
 
 
139
  def reset_all():
140
  new_state = get_empty_state()
141
  updates = [gr.update(value=str(num), variant="secondary", interactive=True) for num in GRID_NUMBERS]
142
- return [new_state, "Jogo reiniciado!"] + updates + [MAX_TIME]
143
 
144
  reset_btn.click(fn=reset_all, outputs=[state, status_box] + grid_buttons + [score_display])
145
 
 
1
+ """
2
+ ================================================================================
3
+ INTERACTIVE FACTORIZATION v1.4 EN
4
+ ================================================================================
5
+ Main Developer:
6
+ Marcos Autuori (MarcosAutuori)
7
+
8
+ Development Assistance:
9
+ Gemini / GitHub Copilot
10
+
11
+ Technologies Used:
12
+ Python 3.12, Gradio
13
+ ================================================================================
14
+ """
15
+
16
  import gradio as gr
17
  import time
18
 
19
+ # Data from the original v1.4 EN script
20
  GRID_NUMBERS = [
21
  10, 12, 14, 15, 16, 18, 20, 21,
22
  24, 25, 27, 28, 30, 32, 35, 36,
 
27
  MAX_TIME = 150
28
 
29
  def get_empty_state():
30
+ """Initializes game state"""
31
  return {
32
  "solved": [False] * 32,
33
  "selected_idx": None,
 
47
 
48
  def handle_game_logic(state, action_type, value=None):
49
  if state["game_over"]:
50
+ return state, "Game Over! Restart to play again."
51
 
52
+ feedback = "Select a number from the grid..."
53
 
54
  if action_type == "select_target":
55
  state["selected_idx"] = value
56
  state["current_factors"] = []
57
+ feedback = f"Factoring: {GRID_NUMBERS[value]}"
58
 
59
  elif action_type == "select_factor":
60
  if state["selected_idx"] is None:
61
+ feedback = "⚠️ Select a grid number first!"
62
  else:
63
  state["current_factors"].append(int(value))
64
  factors_str = " x ".join(map(str, state["current_factors"]))
65
+ feedback = f"Current Factors: {factors_str}"
66
 
67
  if len(state["current_factors"]) == 2:
68
  a, b = state["current_factors"]
 
70
 
71
  if a * b == target:
72
  state["solved"][state["selected_idx"]] = True
73
+ feedback = f"✔️ {a} x {b} = {target}! Correct!"
74
  state["selected_idx"] = None
75
  state["current_factors"] = []
76
  else:
77
+ feedback = f"❌ {a} x {b} = {a*b} (Wrong for {target})."
78
  state["current_factors"] = []
79
 
80
+ # Update Score and Check Completion
81
  elapsed = int(time.time() - state["start_time"])
82
  state["score"] = max(0, MAX_TIME - elapsed)
83
 
84
  if all(state["solved"]):
85
  state["game_over"] = True
86
+ feedback = f"🏆 COMPLETE! Final Score: {state['score']}"
87
 
88
  return state, feedback
89
 
90
+ # CSS for 4x8 uniform grid and consistent layout
91
  custom_css = """
92
  .grid-btn {
93
+ min-width: 80px !important;
94
+ height: 80px !important;
95
+ font-size: 20px !important;
96
  margin: 2px !important;
97
  }
98
  .kb-btn {
99
+ min-width: 60px !important;
100
  height: 50px !important;
101
  }
102
  """
103
 
104
+ with gr.Blocks(title="Autuori - Factorization Game", css=custom_css) as demo:
105
  state = gr.State(get_empty_state())
106
 
107
+ with gr.Row():
108
+ gr.Markdown("""
109
+ # 🎮 Interactive Factorization v1.4 EN
110
+ **Developed by Marcos Autuori** | Select a target and match its factors!
111
+ """)
112
 
113
  with gr.Row():
114
  with gr.Column(scale=4):
115
+ # 4x8 Grid
116
  grid_buttons = []
117
  for r in range(4):
118
  with gr.Row():
 
122
  grid_buttons.append(btn)
123
 
124
  with gr.Column(scale=1):
125
+ # Side Status Panel
126
+ status_box = gr.Textbox(label="Status", value="Select a blue number", interactive=False)
127
+ score_display = gr.Number(label="Score (Time)", value=MAX_TIME, interactive=False)
128
 
129
+ gr.Markdown("### Keyboard")
130
  keyboard_btns = []
 
131
  for r_kb in range(3):
132
  with gr.Row():
133
  for c_kb in range(3):
 
135
  kb_btn = gr.Button(str(val), variant="secondary", elem_classes="kb-btn")
136
  keyboard_btns.append(kb_btn)
137
 
138
+ reset_btn = gr.Button("Restart Game", variant="stop")
139
 
140
+ # Helper function to refresh the whole UI
141
  def update_ui(s):
142
  updates = [format_grid_button(i, s["solved"], s["selected_idx"]) for i in range(32)]
143
  return updates + [s["score"]]
144
 
145
+ # Grid Events
146
  for i, btn in enumerate(grid_buttons):
147
  btn.click(
148
  fn=lambda s, idx=i: handle_game_logic(s, "select_target", idx),
 
150
  outputs=[state, status_box]
151
  ).then(fn=update_ui, inputs=[state], outputs=grid_buttons + [score_display])
152
 
153
+ # Keyboard Events
154
  for kb_btn in keyboard_btns:
155
  kb_btn.click(
156
  fn=lambda s, v=kb_btn.value: handle_game_logic(s, "select_factor", v),
 
158
  outputs=[state, status_box]
159
  ).then(fn=update_ui, inputs=[state], outputs=grid_buttons + [score_display])
160
 
161
+ # Reset Event
162
  def reset_all():
163
  new_state = get_empty_state()
164
  updates = [gr.update(value=str(num), variant="secondary", interactive=True) for num in GRID_NUMBERS]
165
+ return [new_state, "Game Restarted!"] + updates + [MAX_TIME]
166
 
167
  reset_btn.click(fn=reset_all, outputs=[state, status_box] + grid_buttons + [score_display])
168