-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmachine.py
More file actions
327 lines (295 loc) · 9.69 KB
/
Copy pathmachine.py
File metadata and controls
327 lines (295 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import streamlit as st
import streamlit.components.v1 as components
st.set_page_config(page_title="Máquina Matemática", layout="wide")
st.title("🤖 La Máquina Matemática")
st.write("¡Aprende a sumar y restar arrastrando cajas a la máquina!")
# Controls for the machine
col1, col2 = st.columns([1, 2])
with col1:
machine_value = st.number_input("Valor de la Máquina (+)", min_value=1, max_value=10, value=3, step=1)
initial_boxes = st.number_input("Cajas Disponibles Iniciales", min_value=1, max_value=20, value=5, step=1)
with col2:
st.info(f"💡 **Instrucciones Avanzadas**\n"
f"- **Múltiples cajas**: Haz clic sobre las cajas para seleccionar varias a la vez (se pondrán naranjas).\n"
f"- **Suma**: Arrastra tu grupo de la Izquierda a la Máquina. ¡Se le sumarán {machine_value} al total del grupo!\n"
f"- **Resta (Inversa)**: Arrastra cajas del Resultado a la Máquina. ¡La máquina absorberá {machine_value} cajas y devolverá el resto a la izquierda!\n"
f"- Límite del resultado derecho: Mínimo 0, Máximo 10.")
# HTML/JS for the drag-and-drop Component
html_code = f"""
<!DOCTYPE html>
<html>
<head>
<style>
body {{
font-family: 'Comic Sans MS', 'Inter', sans-serif;
user-select: none;
margin: 0;
padding: 20px;
background-color: #f0f8ff;
border-radius: 15px;
box-sizing: border-box;
}}
.container {{
display: flex;
justify-content: space-between;
align-items: stretch;
gap: 20px;
min-height: 350px;
}}
.zone {{
flex: 1;
border: 4px dashed #ccc;
border-radius: 15px;
padding: 20px;
display: flex;
flex-wrap: wrap;
align-content: flex-start;
gap: 10px;
background-color: white;
min-height: 250px;
position: relative;
padding-bottom: 40px; /* Space for the counter */
}}
.zone h3 {{
width: 100%;
text-align: center;
margin-top: 0;
color: #333;
font-size: 1.2rem;
}}
#left-zone {{
border-color: #4CAF50;
background-color: #e8f5e9;
}}
#machine-zone {{
border-color: #ff9800;
background-color: #fff3e0;
justify-content: center;
align-items: center;
flex-direction: column;
text-align: center;
transition: transform 0.2s, background-color 0.2s;
}}
#machine-zone.dragover {{
background-color: #ffe0b2;
transform: scale(1.05);
}}
#right-zone {{
border-color: #2196F3;
background-color: #e3f2fd;
}}
.box {{
width: 50px;
height: 50px;
background-color: #8bc34a;
border: 3px solid #558b2f;
border-radius: 10px;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
font-weight: bold;
color: white;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
transition: transform 0.1s, background-color 0.2s;
}}
.box:hover {{
transform: translateY(-2px);
}}
.box.selected {{
background-color: #ff9800;
border-color: #e65100;
transform: scale(1.1);
box-shadow: 0 6px 12px rgba(255, 152, 0, 0.4);
}}
.ghost {{
opacity: 0.3;
}}
.machine-img {{
font-size: 70px;
margin-bottom: 5px;
}}
.machine-value {{
font-size: 26px;
font-weight: bold;
color: #e65100;
background: white;
padding: 5px 20px;
border-radius: 20px;
border: 2px solid #ff9800;
margin-top: 10px;
}}
.count-badge {{
position: absolute;
bottom: 15px;
right: 15px;
font-size: 22px;
font-weight: bold;
color: #555;
background: rgba(255,255,255,0.9);
padding: 5px 15px;
border-radius: 12px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}}
.alert-msg {{
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
background: #f44336;
color: white;
padding: 15px 30px;
border-radius: 10px;
font-size: 18px;
font-weight: bold;
opacity: 0;
transition: opacity 0.3s;
pointer-events: none;
z-index: 1000;
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}}
.zone-instruction {{
width: 100%;
text-align: center;
font-size: 12px;
color: #666;
margin-bottom: 10px;
}}
</style>
</head>
<body>
<div class="alert-msg" id="alert">Mensaje</div>
<div class="container">
<div id="left-zone" class="zone">
<h3>Disponibles</h3>
<div class="zone-instruction">Haz clic para seleccionar varias</div>
<!-- Las cajas se renderizan aquí -->
<div class="count-badge" id="left-count">0</div>
</div>
<div id="machine-zone" class="zone" ondragover="allowDrop(event)" ondragleave="dragLeave(event)" ondrop="drop(event)">
<h3>Máquina</h3>
<div class="machine-img">⚙️🤖</div>
<div class="machine-value">+{machine_value}</div>
</div>
<div id="right-zone" class="zone">
<h3>Resultado</h3>
<div class="zone-instruction">Arrastra de regreso para restar</div>
<!-- Las cajas de resultado se renderizan aquí -->
<div class="count-badge" id="right-count">0</div>
</div>
</div>
<script>
let machineValue = parseInt("{machine_value}");
let currentLeftBoxes = parseInt("{initial_boxes}");
let currentRightBoxes = 0;
const MAX_RESULT_BOXES = 10;
const MIN_RESULT_BOXES = 0;
function showAlert(msg, type="error") {{
let el = document.getElementById("alert");
el.innerHTML = msg;
el.style.background = type === "error" ? "#f44336" : "#4CAF50";
el.style.opacity = 1;
setTimeout(() => {{ el.style.opacity = 0; }}, 3500);
}}
function renderBoxes() {{
const leftZone = document.getElementById('left-zone');
const rightZone = document.getElementById('right-zone');
// Limpiar cajas anteriores (evitando borrar h3 y contadores)
leftZone.querySelectorAll('.box').forEach(b => b.remove());
rightZone.querySelectorAll('.box').forEach(b => b.remove());
// Dibujar nuevas cajas
for(let i=0; i<currentLeftBoxes; i++) {{
leftZone.appendChild(createBox('left'));
}}
for(let i=0; i<currentRightBoxes; i++) {{
rightZone.appendChild(createBox('right'));
}}
document.getElementById('right-count').innerText = currentRightBoxes;
document.getElementById('left-count').innerText = currentLeftBoxes;
}}
function createBox(zone) {{
let newBox = document.createElement('div');
newBox.className = 'box draggable';
newBox.draggable = true;
newBox.innerHTML = '📦';
// Al hacer clic, activamos el "seleccionado"
newBox.onclick = function() {{
this.classList.toggle('selected');
}};
// Evento de arrastre
newBox.ondragstart = (e) => dragStart(e, zone, newBox);
return newBox;
}}
function dragStart(event, source, boxElement) {{
// Si la caja que estoy arrastrando no estaba seleccionada,
// significa que quiero arrastrar SOLAMENTE esa caja.
if (!boxElement.classList.contains('selected')) {{
// Deselecciono todas las de esa zona
document.getElementById(source + '-zone')
.querySelectorAll('.box.selected')
.forEach(b => b.classList.remove('selected'));
// Y selecciono esta
boxElement.classList.add('selected');
}}
// Cuento cuántas están seleccionadas en la zona de origen
let selectedCount = document.getElementById(source + '-zone')
.querySelectorAll('.box.selected').length;
event.dataTransfer.setData("source", source);
event.dataTransfer.setData("count", selectedCount);
// Efecto visual: hacer semi transparentes las cajas que se están moviendo
setTimeout(() => {{
document.getElementById(source + '-zone')
.querySelectorAll('.box.selected')
.forEach(b => b.classList.add('ghost'));
}}, 10);
}}
document.addEventListener('dragend', (e) => {{
// Limpiar efectos ghost
document.querySelectorAll('.ghost').forEach(b => b.classList.remove('ghost'));
// Limpiar selecciones
document.querySelectorAll('.box.selected').forEach(b => b.classList.remove('selected'));
}});
function allowDrop(event) {{
event.preventDefault(); // Permitir soltar
document.getElementById('machine-zone').classList.add('dragover');
}}
function dragLeave(event) {{
document.getElementById('machine-zone').classList.remove('dragover');
}}
function drop(event) {{
event.preventDefault();
document.getElementById('machine-zone').classList.remove('dragover');
let source = event.dataTransfer.getData("source");
let inputCount = parseInt(event.dataTransfer.getData("count"));
if(!source || isNaN(inputCount) || inputCount === 0) return;
if (source === 'left') {{
let boxesToAdd = inputCount + machineValue;
if (currentRightBoxes + boxesToAdd > MAX_RESULT_BOXES) {{
showAlert("¡Límite! El lado derecho excedería el máximo de " + MAX_RESULT_BOXES + " cajas.");
}} else {{
currentLeftBoxes -= inputCount;
currentRightBoxes += boxesToAdd;
renderBoxes();
showAlert("¡Enviadas " + inputCount + " cajas! La máquina les sumó " + machineValue + ".", "success");
}}
}} else if (source === 'right') {{
let boxesReturned = inputCount - machineValue;
if (boxesReturned < MIN_RESULT_BOXES) {{
showAlert("¡Error invertido! La máquina absorbe " + machineValue + " cajas. Necesitas pasar mínimo esa cantidad para devolver algo positivo (pasaste " + inputCount + ").");
}} else {{
currentRightBoxes -= inputCount;
currentLeftBoxes += boxesReturned;
renderBoxes();
showAlert("¡Regresadas " + inputCount + " cajas! La máquina tomó " + machineValue + " y devolvieron " + boxesReturned + " a la izquierda.", "success");
}}
}}
}}
// Inicializar
renderBoxes();
</script>
</body>
</html>
"""
# Render the HTML component in Streamlit
components.html(html_code, height=550)