-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.js
More file actions
446 lines (381 loc) · 15.7 KB
/
solver.js
File metadata and controls
446 lines (381 loc) · 15.7 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
// ==========================================
// CONFIGURAZIONE GLOBALE
// ==========================================
const suspects = ["Scarlett", "Mustard", "White", "Green", "Peacock", "Plum"];
const weapons = ["Candeliere", "Pugnale", "Tubo", "Rivoltella", "Corda", "Chiave"];
const rooms = ["Ingresso", "Veranda", "Pranzo", "Cucina", "Ballo", "Serra", "Biliardo", "Biblioteca", "Studio"];
const allCards = [...suspects, ...weapons, ...rooms];
const CARDS_IN_DECK = suspects.length + weapons.length + rooms.length - 3;
// ==========================================
// STATO DEL GIOCO
// ==========================================
let players = [];
let grid = {}; // 0=Ignoto, 1=No, 2=Sì
let constraints = [];
let limits = {};
let history = [];
let isSimulating = false;
// Cache delle probabilità
let probabilityCache = null;
// ==========================================
// 1. GESTIONE FATTI
// ==========================================
function resetGameVars() {
grid = {};
constraints = [];
history = [];
probabilityCache = null;
allCards.forEach(c => {
grid[c] = { SOL: 0 };
players.forEach(p => grid[c][p] = 0);
});
}
function setFact(card, player, status) {
const currentStatus = grid[card][player];
if (currentStatus !== 0 && currentStatus !== status) {
if (isSimulating) throw "SIM_CONTRADICTION";
log(`⚠️ <span class ="log-error">CONTRADDIZIONE: ${player} ${currentStatus === 1 ? 'non può' : 'deve'} avere ${card}.</span>`);
return;
}
if (currentStatus === status) return;
grid[card][player] = status;
if (status === 2) {
players.forEach(p => { if (p !== player) setFact(card, p, 1); });
grid[card].SOL = 1;
}
probabilityCache = null;
}
function addConstraint(player, cards) {
const possible = cards.filter(c => grid[c][player] !== 1);
if(possible.length === 0) {
if (isSimulating) throw "SIM_EMPTY_CONSTRAINT";
log(`⚠️ <span class="log-error">CONTRADDIZIONE: ${player} non può avere nessuna di queste carte.</span>`);
return;
}
if (possible.length === 1) {
setFact(possible[0], player, 2);
if (!isSimulating) log(`⚡️ Deduzione Immediata: ${player} ha ${possible[0]}`);
return;
}
const sortedPossible = [...possible].sort();
const exists = constraints.some(con =>
con.player === player &&
JSON.stringify([...con.cards].sort()) === JSON.stringify(sortedPossible)
);
if (!exists) {
constraints.push({ player: player, cards: possible });
probabilityCache = null;
}
}
function undoLastTurn() {
if (history.length === 0) return alert("Nulla da annullare");
const last = history.pop();
grid = last.grid;
constraints = last.constraints;
currentTurnIndex = last.turnIndex;
probabilityCache = null;
log(`⏪ UNDO eseguito.`);
updateTurnUI();
renderGrid();
}
// ==========================================
// 2. MOTORE LOGICO (DETERMINISTICO)
// ==========================================
function runSolver(fromSimulation = false) {
let changed = true;
let loops = 0;
const MAX_LOOPS = 50;
try {
while(changed && loops < MAX_LOOPS) {
changed = false;
const snap = JSON.stringify(grid);
// A. Pulizia vincoli
const initialLen = constraints.length;
constraints = constraints.filter(con => !con.cards.some(c => grid[c][con.player] === 2));
if (constraints.length !== initialLen) changed = true;
// B. Logica Limiti
players.forEach(p => {
let found = 0, unknown = [];
allCards.forEach(c => {
if(grid[c][p] === 2) found++;
if(grid[c][p] === 0) unknown.push(c);
});
if (found > limits[p]) {
if (isSimulating) throw "SIM_LIMIT_EXCEEDED";
log(`⚠️ Errore: ${p} ha troppe carte!`);
}
// Check cruciale: Hand Underflow
if (found + unknown.length < limits[p]) {
if (isSimulating) throw "SIM_HAND_UNDERFLOW";
log(`⚠️ Errore: Impossibile che ${p} abbia ${limits[p]} carte.`);
}
if (found === limits[p] && unknown.length > 0) {
unknown.forEach(c => setFact(c, p, 1)); changed = true;
}
if (found < limits[p] && (found + unknown.length === limits[p]) && unknown.length > 0) {
unknown.forEach(c => setFact(c, p, 2)); changed = true;
}
});
// C. Risoluzione Vincoli
constraints.forEach(con => {
const stillPossible = con.cards.filter(c => grid[c][con.player] !== 1);
if (stillPossible.length === 0) { if (isSimulating) throw "SIM_EMPTY_CONSTRAINT"; }
if (stillPossible.length === 1 && grid[stillPossible[0]][con.player] !== 2) {
setFact(stillPossible[0], con.player, 2);
changed = true;
}
});
// D. Esclusione Totale (Se nessuno ce l'ha -> SOLUZIONE)
allCards.forEach(c => {
if (grid[c].SOL !== 2) {
let allNo = true;
players.forEach(p => { if (grid[c][p] !== 1) allNo = false; });
if (allNo) {
grid[c].SOL = 2;
if (!isSimulating) log(`🏆 SOLUZIONE (Tutti scartati): ${c}`);
changed = true;
}
}
});
// E. Categorie
[suspects, weapons, rooms].forEach(list => {
let owned = 0, unk = [];
list.forEach(c => { if(grid[c].SOL === 1) owned++; else if(grid[c].SOL === 0) unk.push(c); });
if (owned === list.length - 1 && unk.length === 1 && grid[unk[0]].SOL !== 2) {
grid[unk[0]].SOL = 2;
if (!isSimulating) log(`🏆 SOLUZIONE LOGICA: ${unk[0]}`);
changed = true;
}
if (list.some(c => grid[c].SOL === 2)) {
list.forEach(c => {
if (grid[c].SOL !== 2 && grid[c].SOL !== 1) {
grid[c].SOL = 1;
changed = true;
}
});
}
});
// F. Esistenza
allCards.forEach(c => {
if (grid[c].SOL === 1) {
let possibleOwners = [];
players.forEach(p => { if (grid[c][p] !== 1) possibleOwners.push(p); });
if (possibleOwners.length === 0) { if (isSimulating) throw "SIM_NO_OWNER"; }
if (possibleOwners.length === 1) {
const owner = possibleOwners[0];
if (grid[c][owner] !== 2) {
setFact(c, owner, 2);
changed = true;
}
}
}
});
if (JSON.stringify(grid) !== snap) changed = true;
loops++;
}
} catch (e) {
if (isSimulating) throw e;
console.error("Errore Solver:", e);
}
if (!fromSimulation && !isSimulating) {
runDeepScan();
// FORCE RENDER: Assicura che la UI si aggiorni sempre
if (typeof renderGrid === 'function') renderGrid();
}
}
// ==========================================
// 3. DEEP SCAN
// ==========================================
function runDeepScan() {
isSimulating = true;
let deduzioniFatte = false;
const candidates = [];
allCards.forEach(c => {
players.forEach(p => { if (grid[c][p] === 0) candidates.push({card: c, player: p}); });
});
const backupGrid = JSON.stringify(grid);
const backupConstraints = JSON.stringify(constraints);
for (const cand of candidates) {
let possibleYes = true;
try { grid[cand.card][cand.player] = 2; runSolver(true); } catch (e) { possibleYes = false; }
grid = JSON.parse(backupGrid); constraints = JSON.parse(backupConstraints);
let possibleNo = true;
try { grid[cand.card][cand.player] = 1; runSolver(true); } catch (e) { possibleNo = false; }
grid = JSON.parse(backupGrid); constraints = JSON.parse(backupConstraints);
if (!possibleYes && possibleNo) {
setFact(cand.card, cand.player, 1);
if (!isSimulating) log(`🧠 Deep Scan: ${cand.player} NON ha ${cand.card}`);
deduzioniFatte = true;
break;
}
if (possibleYes && !possibleNo) {
setFact(cand.card, cand.player, 2);
if (!isSimulating) log(`🧠 Deep Scan: ${cand.player} HA ${cand.card}`);
deduzioniFatte = true;
break;
}
}
isSimulating = false;
if (deduzioniFatte) runSolver();
}
// ==========================================
// 4. MOTORE MONTE CARLO (PROBABILISTICO)
// ==========================================
function getProbabilities(minValidSamples = 500) {
if (probabilityCache !== null) return probabilityCache;
const solCounts = {};
const playerCounts = {};
allCards.forEach(c => {
solCounts[c] = 0;
playerCounts[c] = {};
players.forEach(p => playerCounts[c][p] = 0);
});
let validWorlds = 0;
const fixedGrid = grid; const fixedLimits = limits; const fixedConstraints = constraints;
const potentialS = suspects.filter(c => fixedGrid[c].SOL !== 1);
const potentialW = weapons.filter(c => fixedGrid[c].SOL !== 1);
const potentialR = rooms.filter(c => fixedGrid[c].SOL !== 1);
// ANTI-CRASH: Se contraddizione totale, ritorna fallback
if (potentialS.length === 0 || potentialW.length === 0 || potentialR.length === 0) {
return createFallbackProbabilities(true);
}
// === MODIFICA CORE: Target su Mondi Validi, non Iterazioni ===
let attempts = 0;
const MAX_ATTEMPTS = 100000; // Limite sicurezza per evitare freeze infiniti
const MAX_TIME_MS = 150; // Non bloccare la UI per più di 150ms
const startTime = performance.now();
// Continua finché non abbiamo abbastanza dati statisticamente significativi
while (validWorlds < minValidSamples && attempts < MAX_ATTEMPTS) {
attempts++;
// Safety Break temporale (ogni 1000 tentativi controlla l'orologio)
if (attempts % 1000 === 0 && (performance.now() - startTime > MAX_TIME_MS)) {
break;
}
const s = potentialS[Math.floor(Math.random() * potentialS.length)];
const w = potentialW[Math.floor(Math.random() * potentialW.length)];
const r = potentialR[Math.floor(Math.random() * potentialR.length)];
const hypothesis = [s, w, r];
let possible = true;
// Quick fail check
players.forEach(p => {
if (fixedGrid[s][p] === 2 || fixedGrid[w][p] === 2 || fixedGrid[r][p] === 2) possible = false;
});
if (!possible) continue;
// Build deck
let deck = [];
allCards.forEach(c => {
if (!hypothesis.includes(c)) {
let owned = false;
players.forEach(p => { if (fixedGrid[c][p] === 2) owned = true; });
if (!owned) {
let rejectedByAll = true;
players.forEach(p => { if (fixedGrid[c][p] !== 1) rejectedByAll = false; });
if (rejectedByAll) { possible = false; }
deck.push(c);
}
}
});
if (!possible) continue;
// Shuffle (Fisher-Yates)
for (let k = deck.length - 1; k > 0; k--) {
const j = Math.floor(Math.random() * (k + 1));
[deck[k], deck[j]] = [deck[j], deck[k]];
}
// Deal logic
const handSlots = {};
players.forEach(p => {
let held = 0;
allCards.forEach(c => { if (fixedGrid[c][p] === 2) held++; });
handSlots[p] = fixedLimits[p] - held;
});
const tempAssignments = {};
let dealValid = true;
for (const card of deck) {
const startIdx = Math.floor(Math.random() * players.length);
let assigned = false;
for (let offset = 0; offset < players.length; offset++) {
const pIdx = (startIdx + offset) % players.length;
const p = players[pIdx];
if (handSlots[p] > 0 && fixedGrid[card][p] !== 1) {
tempAssignments[card] = p;
handSlots[p]--;
assigned = true;
break;
}
}
if (!assigned) { dealValid = false; break; }
}
if (!dealValid) continue;
// Constraints Check
let constraintsMet = true;
for (const con of fixedConstraints) {
let satisfies = false;
for (const c of con.cards) {
if (fixedGrid[c][con.player] === 2 || tempAssignments[c] === con.player) {
satisfies = true;
break;
}
}
if (!satisfies) { constraintsMet = false; break; }
}
if (!constraintsMet) continue;
// Success: Questo è un mondo valido!
validWorlds++;
solCounts[s]++; solCounts[w]++; solCounts[r]++;
allCards.forEach(c => {
if (fixedGrid[c].SOL !== 2 && !hypothesis.includes(c)) {
if (tempAssignments[c]) playerCounts[c][tempAssignments[c]]++;
else players.forEach(p => { if (fixedGrid[c][p] === 2) playerCounts[c][p]++; });
}
});
}
// FALLBACK
if (validWorlds === 0) {
// Se non ne troviamo nemmeno uno in MAX_ATTEMPTS, allora sì, usiamo fallback
return createFallbackProbabilities(false);
}
// Normalizzazione
const solResults = {};
const distResults = {};
const safeDiv = validWorlds; // Ora dividiamo per i mondi VALIDI trovati
allCards.forEach(c => {
if (grid[c].SOL === 2) solResults[c] = 1.0;
else if (grid[c].SOL === 1) solResults[c] = 0.0;
else solResults[c] = solCounts[c] / safeDiv;
distResults[c] = {};
players.forEach(p => {
if (grid[c][p] === 2) distResults[c][p] = 1.0;
else if (grid[c][p] === 1) distResults[c][p] = 0.0;
else distResults[c][p] = playerCounts[c][p] / safeDiv;
});
});
probabilityCache = { solution: solResults, distribution: distResults };
return probabilityCache;
}
// Helper per evitare celle vuote quando MC fallisce
function createFallbackProbabilities(isZero) {
const solRes = {};
const distRes = {};
allCards.forEach(c => {
// Solution Fallback
if (grid[c].SOL === 2) solRes[c] = 1.0;
else if (grid[c].SOL === 1) solRes[c] = 0.0;
else solRes[c] = isZero ? 0 : 0.01; // Marker value
// Distribution Fallback
distRes[c] = {};
let possiblePlayers = [];
players.forEach(p => {
if (grid[c][p] === 2) distRes[c][p] = 1.0;
else if (grid[c][p] === 1) distRes[c][p] = 0.0;
else possiblePlayers.push(p);
});
if (possiblePlayers.length > 0 && !isZero) {
const uniform = 1.0 / possiblePlayers.length;
possiblePlayers.forEach(p => distRes[c][p] = uniform);
} else {
possiblePlayers.forEach(p => distRes[c][p] = 0);
}
});
probabilityCache = { solution: solRes, distribution: distRes };
return probabilityCache;
}