-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzles.js
More file actions
152 lines (134 loc) · 4.91 KB
/
Copy pathpuzzles.js
File metadata and controls
152 lines (134 loc) · 4.91 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
/**
* Puzzle Generator for Circuitle
* Handles infinite puzzle generation and daily seeding
*/
const PuzzleGenerator = (function() {
// Seedable RNG
function mulberry32(a) {
return function() {
var t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}
const types = [
'equivalent_r',
'equivalent_c',
'equivalent_l',
'rc_time_constant',
'rl_time_constant',
'lc_oscillation',
'capacitor_energy',
'inductor_energy'
];
function generate(seed) {
const random = mulberry32(seed);
const type = types[Math.floor(random() * types.length)];
let puzzle = {
type: type,
id: seed,
values: {},
title: "",
question: "",
answer: 0,
unit: ""
};
switch(type) {
case 'equivalent_r':
const r1 = Math.floor(random() * 90) + 10;
const r2 = Math.floor(random() * 90) + 10;
const iso = random() > 0.5;
puzzle.values = { r1, r2, config: iso ? 'series' : 'parallel' };
puzzle.title = iso ? "Series Resistors" : "Parallel Resistors";
puzzle.question = "Find the equivalent resistance (R<sub>eq</sub>)";
puzzle.answer = iso ? (r1 + r2) : (r1 * r2) / (r1 + r2);
puzzle.unit = "Ω";
break;
case 'equivalent_c':
const c1 = (Math.floor(random() * 15) + 1) * 10;
const c2 = (Math.floor(random() * 15) + 1) * 10;
const isoC = random() > 0.5;
puzzle.values = { c1, c2, config: isoC ? 'parallel' : 'series' };
puzzle.title = isoC ? "Parallel Capacitors" : "Series Capacitors";
puzzle.question = "Find the equivalent capacitance (C<sub>eq</sub>)";
puzzle.answer = isoC ? (c1 + c2) : (c1 * c2) / (c1 + c2);
puzzle.unit = "μF";
break;
case 'equivalent_l':
const l1 = (Math.floor(random() * 9) + 1) * 10;
const l2 = (Math.floor(random() * 9) + 1) * 10;
const isoL = random() > 0.5;
puzzle.values = { l: l1, l2: l2, config: isoL ? 'series' : 'parallel' };
puzzle.title = isoL ? "Series Inductors" : "Parallel Inductors";
puzzle.question = "Find the equivalent inductance (L<sub>eq</sub>)";
puzzle.answer = isoL ? (l1 + l2) : (l1 * l2) / (l1 + l2);
puzzle.unit = "mH";
break;
case 'rc_time_constant':
const rRC = Math.floor(random() * 9) + 1;
const cRC = (Math.floor(random() * 9) + 1) * 10;
puzzle.values = { r: rRC, c: cRC };
puzzle.title = "RC Time Constant";
puzzle.question = "Calculate the time constant (τ = RC)";
puzzle.answer = (rRC * 1000) * (cRC / 1000000);
puzzle.unit = "s";
break;
case 'rl_time_constant':
const rRL = (Math.floor(random() * 5) + 1) * 10;
const lRL = (Math.floor(random() * 5) + 1) * 100;
puzzle.values = { r: rRL, l: lRL };
puzzle.title = "RL Time Constant";
puzzle.question = "Calculate the time constant (τ = L/R)";
puzzle.answer = (lRL / 1000) / rRL;
puzzle.unit = "s";
break;
case 'lc_oscillation':
const lLC = (Math.floor(random() * 10) + 1);
const cLC = (Math.floor(random() * 10) + 1);
puzzle.values = { l: lLC, c: cLC };
puzzle.title = "LC Oscillation";
puzzle.question = "Find the resonant frequency f = 1 / (2π√LC)";
const L = lLC / 1000;
const C = cLC / 1000000;
puzzle.answer = 1 / (2 * Math.PI * Math.sqrt(L * C));
puzzle.unit = "Hz";
break;
case 'capacitor_energy':
const cE = (Math.floor(random() * 20) + 1) * 5;
const vE = (Math.floor(random() * 12) + 1) * 2;
puzzle.values = { c: cE, v: vE };
puzzle.title = "Capacitor Energy";
puzzle.question = "Energy stored in the capacitor (U = ½CV²)";
puzzle.answer = 0.5 * (cE / 1000000) * (vE * vE) * 1000;
puzzle.unit = "mJ";
break;
case 'inductor_energy':
const lE = (Math.floor(random() * 10) + 1) * 10;
const iE = (Math.floor(random() * 5) + 1);
puzzle.values = { l: lE, i: iE };
puzzle.title = "Inductor Energy";
puzzle.question = "Energy stored in the inductor (U = ½LI²)";
puzzle.answer = 0.5 * (lE / 1000) * (iE * iE) * 1000;
puzzle.unit = "mJ";
break;
default:
const rFallback = 100;
puzzle.type = 'equivalent_r';
puzzle.values = { r1: rFallback, r2: rFallback, config: 'series' };
puzzle.title = "Simple Resistance";
puzzle.answer = 200;
puzzle.unit = "Ω";
}
return puzzle;
}
function getDailySeed() {
const d = new Date();
return d.getFullYear() * 10000 + (d.getMonth() + 1) * 100 + d.getDate();
}
return {
generate,
getDailySeed
};
})();
window.PuzzleGenerator = PuzzleGenerator;