-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
1288 lines (1133 loc) · 49.6 KB
/
Copy pathscripts.js
File metadata and controls
1288 lines (1133 loc) · 49.6 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Immediate Theme initialization to prevent style flashing before DOM renders
(function() {
const savedTheme = localStorage.getItem('theme') || 'auto';
let themeToApply = savedTheme;
if (savedTheme === 'auto') {
const dailyThemes = ['solar', 'cosmic', 'cyberpunk', 'ocean', 'forest', 'aurora', 'cyberpunk'];
const day = new Date().getDay();
themeToApply = dailyThemes[day];
}
document.documentElement.setAttribute('data-theme', themeToApply);
})();
function showPage(pageId) {
// Hide all pages
const pages = document.querySelectorAll('.page');
pages.forEach(page => {
page.classList.remove('active');
});
// Show the selected page
document.getElementById(pageId).classList.add('active');
// Scroll to top for better UX
window.scrollTo({ top: 0, behavior: 'smooth' });
}
function openFullscreen(iframeId) {
const iframe = document.getElementById(iframeId);
iframe.style.display = 'block';
document.body.style.overflow = 'hidden';
// Force layout recalculation on mobile
setTimeout(() => {
window.scrollTo(0, 0);
const inner = iframe.querySelector('iframe');
if (inner && typeof inner.focus === 'function') inner.focus();
}, 100);
// Add touch event handler
iframe.addEventListener('touchmove', function(e) {
e.stopPropagation();
}, { passive: true });
}
function closeFullscreen(iframeId) {
const iframe = document.getElementById(iframeId);
iframe.style.display = 'none';
document.body.style.overflow = 'auto';
// Remove touch event handler (using a named function would be cleaner)
// This best-effort removal mirrors the add above; if not removed it's harmless
}
// Add touch events for mobile gestures
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchmove', handleTouchMove, false);
let xDown = null;
let yDown = null;
function handleTouchStart(evt) {
xDown = evt.touches[0].clientX;
yDown = evt.touches[0].clientY;
}
function handleTouchMove(evt) {
if (!xDown || !yDown) {
return;
}
const xUp = evt.touches[0].clientX;
const yUp = evt.touches[0].clientY;
const xDiff = xDown - xUp;
const yDiff = yDown - yUp;
// Detect swipe down to close
if (Math.abs(xDiff) < Math.abs(yDiff) && yDiff < -100) {
const fullscreenIframes = document.querySelectorAll('.iframe-fullscreen');
fullscreenIframes.forEach(iframe => {
if (iframe.style.display === 'block') {
iframe.style.display = 'none';
document.body.style.overflow = 'auto';
}
});
}
xDown = null;
yDown = null;
}
// Optional: Add keyboard navigation
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
showPage('home');
}
});
// Open mapped URL by key
function openMapped(key){
const url = window.URLS && window.URLS[key];
if (!url) return;
window.open(url, '_blank');
}
// Populate anchors and buttons that use data-url-key
document.addEventListener('DOMContentLoaded', () => {
const elems = document.querySelectorAll('[data-url-key]');
elems.forEach(el => {
const key = el.getAttribute('data-url-key');
const url = window.URLS && window.URLS[key];
if (!url) return;
if (el.tagName.toLowerCase() === 'a'){
el.setAttribute('href', url);
el.setAttribute('target', '_blank');
} else {
// store direct url on element dataset for other handlers
el.dataset.url = url;
}
});
updateVisitCount();
});
// Function to fetch and display page visit count
function updateVisitCount() {
const counterElement = document.getElementById('visit-count');
if (!counterElement) {
console.warn('Visit counter element not found');
return;
}
// Try external API first, fall back to localStorage
fetch('https://api.counterapi.dev/v2/rdrp-sna/pages/up', { mode: 'cors' })
.then(response => {
if (!response.ok) throw new Error('API error: ' + response.status);
return response.json();
})
.then(data => {
console.log('Counter API success:', data);
const count = data.value || data.count || '?';
counterElement.textContent = count;
})
.catch(err => {
console.log('Counter API failed, using localStorage fallback:', err.message);
// Fallback: use localStorage counter
let visits = parseInt(localStorage.getItem('pageVisits') || '0', 10) + 1;
localStorage.setItem('pageVisits', visits);
counterElement.textContent = visits;
console.log('Local visit count:', visits);
});
}
function formatCalcValue(value) {
return Number(value).toLocaleString('en-US', { maximumFractionDigits: 3 });
}
function showCalcResult(containerId, text) {
const container = document.getElementById(containerId);
if (container) container.innerHTML = text;
}
function initEngineerForms() {
const roadForm = document.getElementById('roadCalcForm');
if (roadForm) {
roadForm.addEventListener('submit', function(e) {
e.preventDefault();
const length = parseFloat(this.roadLength.value);
const width = parseFloat(this.roadWidth.value);
const thickness = parseFloat(this.roadThickness.value);
if (!length || !width || !thickness) {
showCalcResult('roadResult', 'Please enter valid road dimensions.');
return;
}
const area = length * width;
const volume = area * (thickness / 1000);
const asphaltWeight = volume * 2.35;
showCalcResult('roadResult', `Surface area: <strong>${formatCalcValue(area)}</strong> m²<br>Volume: <strong>${formatCalcValue(volume)}</strong> m³<br>Estimated asphalt weight: <strong>${formatCalcValue(asphaltWeight)}</strong> tonnes (approx)`);
});
}
const bridgeForm = document.getElementById('bridgeCalcForm');
if (bridgeForm) {
bridgeForm.addEventListener('submit', function(e) {
e.preventDefault();
const span = parseFloat(this.bridgeSpan.value);
const totalLoad = parseFloat(this.totalLoad.value);
const beams = parseInt(this.numBeams.value, 10);
if (!span || !totalLoad || !beams) {
showCalcResult('bridgeResult', 'Please enter valid bridge span, load, and beam count.');
return;
}
const loadPerBeam = totalLoad / beams;
const loadPerMeter = totalLoad / span;
showCalcResult('bridgeResult', `Load per beam: <strong>${formatCalcValue(loadPerBeam)}</strong> kN<br>Uniform load per meter: <strong>${formatCalcValue(loadPerMeter)}</strong> kN/m`);
});
}
const buildingForm = document.getElementById('buildingCalcForm');
if (buildingForm) {
buildingForm.addEventListener('submit', function(e) {
e.preventDefault();
const length = parseFloat(this.buildingLength.value);
const width = parseFloat(this.buildingWidth.value);
const height = parseFloat(this.buildingHeight.value);
const thickness = parseFloat(this.wallThickness.value);
if (!length || !width || !height || !thickness) {
showCalcResult('buildingResult', 'Please enter valid building dimensions.');
return;
}
const floorArea = length * width;
const enclosedVolume = floorArea * height;
const wallVolume = 2 * (length + width) * height * (thickness / 1000);
showCalcResult('buildingResult', `Floor area: <strong>${formatCalcValue(floorArea)}</strong> m²<br>Enclosed volume: <strong>${formatCalcValue(enclosedVolume)}</strong> m³<br>Wall concrete volume: <strong>${formatCalcValue(wallVolume)}</strong> m³`);
});
}
const rebarForm = document.getElementById('rebarCalcForm');
if (rebarForm) {
rebarForm.addEventListener('submit', function(e) {
e.preventDefault();
const diameter = parseFloat(this.rebarDiameter.value);
const length = parseFloat(this.rebarLength.value);
const count = parseInt(this.rebarCount.value, 10);
if (!diameter || !length || !count) {
showCalcResult('rebarResult', 'Please enter valid rebar diameter, length, and quantity.');
return;
}
const area = Math.PI * Math.pow(diameter / 1000, 2) / 4;
const weightPerBar = area * length * 7850;
const totalWeight = weightPerBar * count;
showCalcResult('rebarResult', `Weight per bar: <strong>${formatCalcValue(weightPerBar)}</strong> kg<br>Total rebar weight: <strong>${formatCalcValue(totalWeight)}</strong> kg`);
});
}
const earthworkForm = document.getElementById('earthworkCalcForm');
if (earthworkForm) {
earthworkForm.addEventListener('submit', function(e) {
e.preventDefault();
const startArea = parseFloat(this.startArea.value);
const endArea = parseFloat(this.endArea.value);
const length = parseFloat(this.earthworkLength.value);
if (!startArea || !endArea || !length) {
showCalcResult('earthworkResult', 'Please enter valid earthwork areas and length.');
return;
}
const volume = ((startArea + endArea) / 2) * length;
showCalcResult('earthworkResult', `Earthwork volume: <strong>${formatCalcValue(volume)}</strong> m³<br>(Average end area method)`);
});
}
const concreteForm = document.getElementById('concreteCalcForm');
if (concreteForm) {
concreteForm.addEventListener('submit', function(e) {
e.preventDefault();
const length = parseFloat(this.concreteLength.value);
const width = parseFloat(this.concreteWidth.value);
const depth = parseFloat(this.concreteDepth.value);
if (!length || !width || !depth) {
showCalcResult('concreteResult', 'Please enter valid slab dimensions.');
return;
}
const volume = length * width * (depth / 1000);
const dryVolume = volume * 1.54;
const cementBags = dryVolume * 8.33;
showCalcResult('concreteResult', `Concrete volume: <strong>${formatCalcValue(volume)}</strong> m³<br>Dry volume: <strong>${formatCalcValue(dryVolume)}</strong> m³<br>Estimated cement: <strong>${formatCalcValue(cementBags)}</strong> bags (1 bag = 0.035 m³)`);
});
}
const clearButtons = document.querySelectorAll('.clear-btn');
clearButtons.forEach(button => {
button.addEventListener('click', function() {
const form = this.closest('form');
if (form) form.reset();
const resultId = this.dataset.result;
if (resultId) showCalcResult(resultId, 'Enter values and click calculate.');
});
});
}
function initNumericKeypad() {
const keypad = document.getElementById('numericKeypad');
if (!keypad) return;
const keys = ['7','8','9','4','5','6','1','2','3','0','.'];
keypad.innerHTML = keys.map(key => `<button type="button" class="key">${key}</button>`).join('') +
`<button type="button" class="action-key backspace-key" data-action="backspace">⌫</button>` +
`<button type="button" class="action-key enter-key" data-action="enter">Enter</button>`;
let activeInput = null;
let pointerInKeypad = false;
function updatePosition(input) {
const rect = input.getBoundingClientRect();
const keypadRect = keypad.getBoundingClientRect();
const left = Math.min(Math.max(rect.left, 10), window.innerWidth - keypadRect.width - 10);
let top = rect.bottom + 10;
if (top + keypadRect.height > window.innerHeight - 10) {
top = rect.top - keypadRect.height - 10;
}
keypad.style.left = `${left + window.scrollX}px`;
keypad.style.top = `${top + window.scrollY}px`;
}
function showKeypad(input) {
activeInput = input;
keypad.classList.remove('hidden');
keypad.setAttribute('aria-hidden', 'false');
updatePosition(input);
input.focus({preventScroll: true});
}
function hideKeypad() {
activeInput = null;
keypad.classList.add('hidden');
keypad.setAttribute('aria-hidden', 'true');
}
const numberInputs = document.querySelectorAll('input[type="number"]');
numberInputs.forEach(input => {
input.addEventListener('focus', () => showKeypad(input));
input.addEventListener('click', () => showKeypad(input));
input.addEventListener('keydown', e => {
if (e.key === 'Escape') {
hideKeypad();
}
});
});
keypad.addEventListener('pointerenter', () => { pointerInKeypad = true; });
keypad.addEventListener('pointerleave', () => { pointerInKeypad = false; });
keypad.addEventListener('click', function(event) {
const button = event.target.closest('button');
if (!button || !activeInput) return;
const action = button.dataset.action;
if (action === 'backspace') {
activeInput.value = activeInput.value.slice(0, -1);
activeInput.dispatchEvent(new Event('input', { bubbles: true }));
return;
}
if (action === 'enter') {
hideKeypad();
activeInput.blur();
return;
}
const value = button.textContent;
if (value === '.' && activeInput.value.includes('.')) return;
activeInput.value += value;
activeInput.dispatchEvent(new Event('input', { bubbles: true }));
});
document.addEventListener('pointerdown', event => {
if (!keypad.contains(event.target) && activeInput && event.target !== activeInput) {
if (!pointerInKeypad) hideKeypad();
}
});
window.addEventListener('resize', () => {
if (activeInput) updatePosition(activeInput);
});
}
// Optional: Add keyboard navigation
(function(){
const canvas = document.getElementById('bgCanvas');
if (!canvas) return; // bail if canvas missing
const ctx = canvas.getContext('2d');
let width = 0, height = 0, particles = [];
const mouse = { x: null, y: null };
function resize(){
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
initParticles();
}
function rand(min, max){ return Math.random() * (max - min) + min; }
function getParticleHueRange() {
const styles = getComputedStyle(document.documentElement);
const min = parseInt(styles.getPropertyValue('--particle-hue-min')) || 180;
const max = parseInt(styles.getPropertyValue('--particle-hue-max')) || 260;
return { min, max };
}
function Particle(x,y,r){
this.x = x || rand(0,width);
this.y = y || rand(0,height);
this.vx = rand(-0.35,0.35);
this.vy = rand(-0.35,0.35);
this.r = r || rand(1,3.2);
this.baseR = this.r;
const range = getParticleHueRange();
this.h = rand(range.min, range.max);
}
Particle.prototype.update = function(){
this.x += this.vx;
this.y += this.vy;
if(this.x < -10) this.x = width + 10;
if(this.x > width + 10) this.x = -10;
if(this.y < -10) this.y = height + 10;
if(this.y > height + 10) this.y = -10;
if(mouse.x !== null){
const dx = this.x - mouse.x;
const dy = this.y - mouse.y;
const dist = Math.sqrt(dx*dx + dy*dy) || 1;
if(dist < 120){
const force = (120 - dist) / 120;
this.x += (dx / dist) * force * 6;
this.y += (dy / dist) * force * 6;
}
}
};
Particle.prototype.draw = function(){
ctx.beginPath();
ctx.fillStyle = `hsla(${this.h}, 90%, 70%, 0.95)`;
ctx.shadowBlur = 10;
ctx.shadowColor = `hsla(${this.h}, 90%, 70%, 0.9)`;
ctx.arc(this.x, this.y, this.r, 0, Math.PI*2);
ctx.fill();
ctx.shadowBlur = 0;
};
function initParticles(){
particles = [];
const area = width * height;
const count = Math.max(18, Math.min(160, Math.floor(area/12000)));
for(let i=0;i<count;i++) particles.push(new Particle());
}
function connect(){
for(let i=0;i<particles.length;i++){
for(let j=i+1;j<particles.length;j++){
const a = particles[i];
const b = particles[j];
const dx = a.x - b.x;
const dy = a.y - b.y;
const d2 = dx*dx + dy*dy;
if(d2 < 16000){
const alpha = 0.18 - (d2 / 16000) * 0.16;
ctx.strokeStyle = `rgba(255,255,255,${alpha.toFixed(3)})`;
ctx.lineWidth = 0.9;
ctx.beginPath();
ctx.moveTo(a.x, a.y);
ctx.lineTo(b.x, b.y);
ctx.stroke();
}
}
}
}
function animate(){
ctx.clearRect(0,0,width,height);
for(const p of particles){ p.update(); p.draw(); }
connect();
requestAnimationFrame(animate);
}
window.addEventListener('mousemove', e => { mouse.x = e.clientX; mouse.y = e.clientY; });
window.addEventListener('mouseout', ()=> { mouse.x = null; mouse.y = null; });
window.addEventListener('resize', resize);
document.addEventListener('theme-changed', () => {
const range = getParticleHueRange();
particles.forEach(p => {
p.h = rand(range.min, range.max);
});
});
// styling canvas behind content without blocking interactions
canvas.style.position = 'fixed';
canvas.style.top = '0';
canvas.style.left = '0';
canvas.style.width = '100%';
canvas.style.height = '100%';
canvas.style.zIndex = '0';
canvas.style.pointerEvents = 'none';
const container = document.querySelector('.container');
if (container) {
container.style.position = 'relative';
container.style.zIndex = '1';
}
resize();
animate();
})();
function initThemeSwitcher() {
const themeBtn = document.getElementById('themeBtn');
const themeDropdown = document.getElementById('themeDropdown');
const themeOptions = document.querySelectorAll('.theme-option');
if (!themeBtn || !themeDropdown) return;
function setTheme(theme) {
let themeToApply = theme;
if (theme === 'auto') {
const dailyThemes = ['solar', 'cosmic', 'cyberpunk', 'ocean', 'forest', 'aurora', 'cyberpunk'];
const day = new Date().getDay();
themeToApply = dailyThemes[day];
}
document.documentElement.setAttribute('data-theme', themeToApply);
localStorage.setItem('theme', theme);
// Update active class on dropdown options
themeOptions.forEach(opt => {
if (opt.getAttribute('data-theme') === theme) {
opt.classList.add('active');
} else {
opt.classList.remove('active');
}
});
// Broadcast event for particles to update their hues
document.dispatchEvent(new CustomEvent('theme-changed'));
}
// Initialize dropdown active classes
const currentTheme = localStorage.getItem('theme') || 'auto';
setTheme(currentTheme);
// Toggle menu
themeBtn.addEventListener('click', (e) => {
e.stopPropagation();
const isExpanded = themeBtn.getAttribute('aria-expanded') === 'true';
themeBtn.setAttribute('aria-expanded', !isExpanded);
themeDropdown.classList.toggle('show');
});
// Option clicks
themeOptions.forEach(option => {
option.addEventListener('click', (e) => {
e.stopPropagation();
const theme = option.getAttribute('data-theme');
setTheme(theme);
themeDropdown.classList.remove('show');
themeBtn.setAttribute('aria-expanded', 'false');
});
});
// Close on outside clicks
document.addEventListener('click', (e) => {
if (!themeBtn.contains(e.target) && !themeDropdown.contains(e.target)) {
themeDropdown.classList.remove('show');
themeBtn.setAttribute('aria-expanded', 'false');
}
});
}
// Global search filter — filters visible cards by title or data-url-key
document.addEventListener('DOMContentLoaded', function() {
const searchInput = document.getElementById('globalSearch');
if (!searchInput) return;
searchInput.addEventListener('input', function() {
const q = this.value.trim().toLowerCase();
const cards = document.querySelectorAll('.card');
cards.forEach(card => {
const title = (card.querySelector('.card-title') && card.querySelector('.card-title').textContent || '').toLowerCase();
const key = (card.getAttribute('data-url-key') || '').toLowerCase();
const match = !q || title.includes(q) || key.includes(q);
card.style.display = match ? '' : 'none';
});
// Hide sections that don't have any visible cards
const sections = document.querySelectorAll('.section');
sections.forEach(section => {
const hasVisible = Array.from(section.querySelectorAll('.card')).some(card => card.style.display !== 'none');
section.style.display = hasVisible ? '' : 'none';
});
});
initEngineerForms();
initNumericKeypad();
initThemeSwitcher();
initGamesPage();
});
function initGamesPage() {
// ---------------------------------------------------------
// Arcade Switchboard / Tab Control
// ---------------------------------------------------------
const tabBtns = document.querySelectorAll('.arcade-tab-btn');
const panes = document.querySelectorAll('.game-pane');
tabBtns.forEach(btn => {
btn.addEventListener('click', () => {
tabBtns.forEach(b => {
b.classList.remove('active');
b.setAttribute('aria-selected', 'false');
});
panes.forEach(p => p.classList.remove('active'));
btn.classList.add('active');
btn.setAttribute('aria-selected', 'true');
const targetPane = document.getElementById('pane-' + btn.dataset.game);
if (targetPane) {
targetPane.classList.add('active');
// Focus input fields if applicable
if (btn.dataset.game === 'scramble') {
const scrambleGuessEl = document.getElementById('scrambleGuess');
if (scrambleGuessEl) scrambleGuessEl.focus();
} else if (btn.dataset.game === 'guess') {
const guessInput = document.getElementById('guessNumberInput');
if (guessInput) guessInput.focus();
} else if (btn.dataset.game === 'math') {
const mathInput = document.getElementById('mathAnswerInput');
if (mathInput) mathInput.focus();
}
}
});
});
// ---------------------------------------------------------
// Game 1: Word Scramble
// ---------------------------------------------------------
const scrambleWords = [
{ word: 'puzzle', hint: 'A game or problem that tests ingenuity.' },
{ word: 'mirror', hint: 'A reflective surface often found in bathrooms.' },
{ word: 'planet', hint: 'A large celestial body orbiting a star.' },
{ word: 'nature', hint: 'Everything around us outside built spaces.' },
{ word: 'garden', hint: 'A place to grow flowers, herbs, or vegetables.' },
{ word: 'castle', hint: 'A large fortified building from the Middle Ages.' }
];
const scrambleLettersEl = document.getElementById('scrambleLetters');
const scrambleHintEl = document.getElementById('scrambleHint');
const scrambleGuessEl = document.getElementById('scrambleGuess');
const scrambleFeedbackEl = document.getElementById('scrambleFeedback');
const newScrambleBtn = document.getElementById('newScrambleBtn');
const checkScrambleBtn = document.getElementById('checkScrambleBtn');
const scrambleStreakEl = document.getElementById('scrambleStreak');
let currentScramble = null;
let scrambleStreak = 0;
function shuffleWord(word) {
const arr = word.split('');
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr.join('');
}
function setNewScramble() {
if (!scrambleWords.length || !scrambleLettersEl) return;
currentScramble = scrambleWords[Math.floor(Math.random() * scrambleWords.length)];
let scrambled = currentScramble.word;
while (scrambled === currentScramble.word) {
scrambled = shuffleWord(currentScramble.word);
}
scrambleLettersEl.innerHTML = scrambled.split('').map(letter => `<span>${letter}</span>`).join('');
if (scrambleHintEl) scrambleHintEl.textContent = currentScramble.hint;
if (scrambleGuessEl) {
scrambleGuessEl.value = '';
scrambleGuessEl.focus();
}
if (scrambleFeedbackEl) {
scrambleFeedbackEl.textContent = 'Try solving the scramble above.';
scrambleFeedbackEl.className = 'game-message';
}
}
function checkScramble() {
if (!scrambleGuessEl || !scrambleFeedbackEl || !currentScramble) return;
const guess = scrambleGuessEl.value.trim().toLowerCase();
if (!guess) {
scrambleFeedbackEl.textContent = 'Enter your answer before checking.';
scrambleFeedbackEl.className = 'game-message error';
return;
}
if (guess === currentScramble.word) {
scrambleFeedbackEl.textContent = 'Great job! You solved it.';
scrambleFeedbackEl.className = 'game-message success';
scrambleStreak++;
} else {
scrambleFeedbackEl.textContent = `Not quite — try again! (Answer was: ${currentScramble.word})`;
scrambleFeedbackEl.className = 'game-message error';
scrambleStreak = 0;
}
if (scrambleStreakEl) scrambleStreakEl.textContent = scrambleStreak;
}
if (newScrambleBtn && checkScrambleBtn && scrambleLettersEl) {
newScrambleBtn.addEventListener('click', setNewScramble);
checkScrambleBtn.addEventListener('click', checkScramble);
scrambleGuessEl.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
event.preventDefault();
checkScramble();
}
});
setNewScramble();
}
// ---------------------------------------------------------
// Game 2: Sliding Puzzle
// ---------------------------------------------------------
const slidingBoardEl = document.getElementById('slidingBoard');
const slidingFeedbackEl = document.getElementById('slidingFeedback');
const shufflePuzzleBtn = document.getElementById('shufflePuzzleBtn');
const resetPuzzleBtn = document.getElementById('resetPuzzleBtn');
const slidingMovesEl = document.getElementById('slidingMoves');
const slidingTimerEl = document.getElementById('slidingTimer');
let slidingBoardState = [];
let slidingMoves = 0;
let slidingTime = 0;
let slidingTimerInterval = null;
const puzzleSize = 3;
function createSolvedBoard() {
return Array.from({ length: puzzleSize * puzzleSize }, (_, index) => index === puzzleSize * puzzleSize - 1 ? '' : String(index + 1));
}
function startSlidingTimer() {
if (slidingTimerInterval) return;
slidingTime = 0;
updateSlidingTimerDisplay();
slidingTimerInterval = setInterval(() => {
slidingTime++;
updateSlidingTimerDisplay();
}, 1000);
}
function stopSlidingTimer() {
if (slidingTimerInterval) {
clearInterval(slidingTimerInterval);
slidingTimerInterval = null;
}
}
function updateSlidingTimerDisplay() {
if (!slidingTimerEl) return;
const mins = String(Math.floor(slidingTime / 60)).padStart(2, '0');
const secs = String(slidingTime % 60).padStart(2, '0');
slidingTimerEl.textContent = `${mins}:${secs}`;
}
function renderSlidingBoard() {
if (!slidingBoardEl) return;
slidingBoardEl.innerHTML = slidingBoardState.map((value, index) => {
const emptyClass = value === '' ? 'empty' : '';
return `<button type="button" class="puzzle-tile ${emptyClass}" data-index="${index}" ${value === '' ? 'aria-hidden="true"' : ''}>${value}</button>`;
}).join('');
const tiles = slidingBoardEl.querySelectorAll('.puzzle-tile');
tiles.forEach(tile => tile.addEventListener('click', () => moveTile(parseInt(tile.dataset.index, 10))));
}
function getBlankIndex() {
return slidingBoardState.indexOf('');
}
function isAdjacent(indexA, indexB) {
const rowA = Math.floor(indexA / puzzleSize);
const colA = indexA % puzzleSize;
const rowB = Math.floor(indexB / puzzleSize);
const colB = indexB % puzzleSize;
return Math.abs(rowA - rowB) + Math.abs(colA - colB) === 1;
}
function moveTile(index) {
const blankIndex = getBlankIndex();
if (!isAdjacent(index, blankIndex)) return;
// Swapping values
[slidingBoardState[blankIndex], slidingBoardState[index]] = [slidingBoardState[index], slidingBoardState[blankIndex]];
// Start timer on first move
if (slidingMoves === 0) {
startSlidingTimer();
}
slidingMoves++;
if (slidingMovesEl) slidingMovesEl.textContent = slidingMoves;
renderSlidingBoard();
if (slidingBoardState.join(',') === createSolvedBoard().join(',')) {
stopSlidingTimer();
if (slidingFeedbackEl) {
slidingFeedbackEl.textContent = `🏆 Well done! Solved in ${slidingMoves} moves & ${slidingTimerEl.textContent}!`;
slidingFeedbackEl.className = 'game-message success';
}
} else {
if (slidingFeedbackEl) {
slidingFeedbackEl.textContent = 'Keep shifting tiles to match chronological order.';
slidingFeedbackEl.className = 'game-message';
}
}
}
function shuffleSlidingPuzzle(moves = 120) {
// Reset moves and timers first
slidingMoves = 0;
if (slidingMovesEl) slidingMovesEl.textContent = '0';
stopSlidingTimer();
slidingTime = 0;
updateSlidingTimerDisplay();
// Start with solved state and shuffle backwards
slidingBoardState = createSolvedBoard();
for (let i = 0; i < moves; i++) {
const blankIndex = getBlankIndex();
const adjacentTiles = slidingBoardState.map((_, idx) => idx).filter(idx => isAdjacent(idx, blankIndex));
const tileToMove = adjacentTiles[Math.floor(Math.random() * adjacentTiles.length)];
[slidingBoardState[blankIndex], slidingBoardState[tileToMove]] = [slidingBoardState[tileToMove], slidingBoardState[blankIndex]];
}
// Ensure not accidentally shuffled into solved state
if (slidingBoardState.join(',') === createSolvedBoard().join(',')) {
return shuffleSlidingPuzzle(15);
}
renderSlidingBoard();
if (slidingFeedbackEl) {
slidingFeedbackEl.textContent = 'Board shuffled! Make your first move to start the timer.';
slidingFeedbackEl.className = 'game-message';
}
}
function resetSlidingPuzzle() {
slidingBoardState = createSolvedBoard();
renderSlidingBoard();
if (slidingFeedbackEl) {
slidingFeedbackEl.textContent = 'Tap a tile next to the empty space to move it.';
slidingFeedbackEl.className = 'game-message';
}
slidingMoves = 0;
if (slidingMovesEl) slidingMovesEl.textContent = '0';
stopSlidingTimer();
slidingTime = 0;
updateSlidingTimerDisplay();
}
if (slidingBoardEl && shufflePuzzleBtn && resetPuzzleBtn) {
resetSlidingPuzzle();
shufflePuzzleBtn.addEventListener('click', () => shuffleSlidingPuzzle());
resetPuzzleBtn.addEventListener('click', resetSlidingPuzzle);
}
// ---------------------------------------------------------
// Game 3: Guess the Number
// ---------------------------------------------------------
const guessNumberInput = document.getElementById('guessNumberInput');
const guessNumberBtn = document.getElementById('guessNumberBtn');
const newGuessNumberBtn = document.getElementById('newGuessNumberBtn');
const guessNumberFeedback = document.getElementById('guessNumberFeedback');
const guessAttemptsEl = document.getElementById('guessNumberAttempts');
const guessHistoryEl = document.getElementById('guessHistory');
let secretNumber = null;
let guessAttempts = 0;
function setNewGuessNumber() {
secretNumber = Math.floor(Math.random() * 100) + 1;
guessAttempts = 0;
if (guessAttemptsEl) guessAttemptsEl.textContent = '0';
if (guessHistoryEl) guessHistoryEl.innerHTML = '';
if (guessNumberInput) {
guessNumberInput.value = '';
guessNumberInput.focus();
}
if (guessNumberFeedback) {
guessNumberFeedback.textContent = 'Guess the secret number between 1 and 100.';
guessNumberFeedback.className = 'game-message';
}
}
function checkGuessNumber() {
if (!guessNumberInput || !guessNumberFeedback) return;
const guess = parseInt(guessNumberInput.value, 10);
if (isNaN(guess) || guess < 1 || guess > 100) {
guessNumberFeedback.textContent = 'Please enter a valid number between 1 and 100.';
guessNumberFeedback.className = 'game-message error';
return;
}
guessAttempts++;
if (guessAttemptsEl) guessAttemptsEl.textContent = guessAttempts;
let badgeClass = '';
let badgeSymbol = '';
if (guess === secretNumber) {
guessNumberFeedback.textContent = `🎉 Correct! You found the number in ${guessAttempts} attempts.`;
guessNumberFeedback.className = 'game-message success';
badgeClass = 'correct';
badgeSymbol = '🎉';
} else if (guess < secretNumber) {
guessNumberFeedback.textContent = 'Too low. Try a higher number.';
guessNumberFeedback.className = 'game-message error';
badgeClass = 'too-low';
badgeSymbol = '↓';
} else {
guessNumberFeedback.textContent = 'Too high. Try a lower number.';
guessNumberFeedback.className = 'game-message error';
badgeClass = 'too-high';
badgeSymbol = '↑';
}
// Add to history list
if (guessHistoryEl) {
const badge = document.createElement('span');
badge.className = `guess-badge ${badgeClass}`;
badge.innerHTML = `<strong>${guess}</strong> ${badgeSymbol}`;
guessHistoryEl.appendChild(badge);
}
guessNumberInput.value = '';
guessNumberInput.focus();
}
if (guessNumberBtn && newGuessNumberBtn && guessNumberInput) {
newGuessNumberBtn.addEventListener('click', setNewGuessNumber);
guessNumberBtn.addEventListener('click', checkGuessNumber);
guessNumberInput.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
event.preventDefault();
checkGuessNumber();
}
});
setNewGuessNumber();
}
// ---------------------------------------------------------
// Game 4: Math Challenge
// ---------------------------------------------------------
const mathProblemEl = document.getElementById('mathProblem');
const mathAnswerInput = document.getElementById('mathAnswerInput');
const checkMathBtn = document.getElementById('checkMathBtn');
const newMathBtn = document.getElementById('newMathBtn');
const mathFeedback = document.getElementById('mathFeedback');
const mathComboEl = document.getElementById('mathCombo');
const mathHighScoreEl = document.getElementById('mathHighScore');
let currentMath = null;
let mathCombo = 0;
let mathHighScore = parseInt(localStorage.getItem('mathHighScore') || '0', 10);
if (mathHighScoreEl) mathHighScoreEl.textContent = mathHighScore;
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function setNewMathProblem() {
const operators = ['+', '-', '×'];
const op = operators[Math.floor(Math.random() * operators.length)];
let a = getRandomInt(2, 20);
let b = getRandomInt(2, 20);
if (op === '-' && b > a) {
[a, b] = [b, a]; // Avoid negative answers
}
if (op === '×') {
a = getRandomInt(2, 12);
b = getRandomInt(2, 12);
}
let answer = 0;
if (op === '+') answer = a + b;
if (op === '-') answer = a - b;
if (op === '×') answer = a * b;
currentMath = { text: `${a} ${op} ${b} = ?`, answer };
if (mathProblemEl) mathProblemEl.textContent = currentMath.text;
if (mathAnswerInput) {
mathAnswerInput.value = '';
mathAnswerInput.focus();
}
}
function checkMathAnswer() {
if (!mathAnswerInput || !mathFeedback || !currentMath) return;
const answer = parseInt(mathAnswerInput.value, 10);
if (isNaN(answer)) {
mathFeedback.textContent = 'Please type a valid number.';
mathFeedback.className = 'game-message error';
return;
}
if (answer === currentMath.answer) {
mathCombo++;
if (mathComboEl) mathComboEl.textContent = mathCombo;
if (mathCombo > mathHighScore) {
mathHighScore = mathCombo;
localStorage.setItem('mathHighScore', mathHighScore);
if (mathHighScoreEl) mathHighScoreEl.textContent = mathHighScore;
}
mathFeedback.textContent = `Nice! Correct answer. Combo: ${mathCombo} 🔥`;
mathFeedback.className = 'game-message success';
// Auto load next question after a brief delay
setTimeout(setNewMathProblem, 1000);