-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetris.html
More file actions
849 lines (790 loc) · 51.2 KB
/
tetris.html
File metadata and controls
849 lines (790 loc) · 51.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tetris AI Training Environment - Neuroevolution</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;700&display=swap" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
font-family: 'Roboto Mono', monospace;
background-color: #1a202c;
color: #e2e8f0;
}
.game-container canvas {
background-color: #000;
border: 2px solid #4a5568;
border-radius: 0.5rem;
}
.info-panel {
background-color: #2d3748;
border-radius: 0.5rem;
padding: 1rem;
font-size: 0.8rem;
}
.info-panel canvas {
background-color: #1a202c;
border: 1px solid #4a5568;
border-radius: 0.25rem;
}
#dna-display, #parent-dna-input {
background-color: #1a202c;
border: 1px solid #4a5568;
font-size: 0.75rem;
white-space: pre-wrap;
word-wrap: break-word;
width: 100%;
min-height: 120px;
}
input[type=range] {
-webkit-appearance: none;
width: 100%;
height: 8px;
background: #4a5568;
border-radius: 5px;
outline: none;
opacity: 0.7;
transition: opacity .2s;
}
input[type=range]:hover { opacity: 1; }
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 18px;
height: 18px;
background: #a0aec0;
cursor: pointer;
border-radius: 50%;
}
input[type=range]::-moz-range-thumb {
width: 18px;
height: 18px;
background: #a0aec0;
cursor: pointer;
border-radius: 50%;
}
#game-instances-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 2rem;
}
.instance-wrapper {
flex-grow: 1;
min-width: 550px;
max-width: 600px;
}
</style>
</head>
<body class="p-4 sm:p-8">
<div class="max-w-screen-2xl mx-auto">
<header class="text-center mb-8">
<h1 class="text-3xl sm:text-4xl font-bold text-white">Tetris AI Training Environment</h1>
<p class="text-gray-400 mt-2">Neuroevolution Playground</p>
</header>
<div id="main-controls" class="text-center mb-8 bg-gray-800 p-4 rounded-lg shadow-lg">
<h2 class="text-xl font-bold mb-4">Master Controls</h2>
<div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
<button id="start-all-btn" class="bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded-lg">Start All</button>
<button id="stop-all-btn" class="bg-red-600 hover:bg-red-700 text-white font-bold py-2 px-4 rounded-lg">Stop All</button>
<div class="flex items-center justify-center">
<input type="checkbox" id="human-player-checkbox" class="h-5 w-5 rounded">
<label for="human-player-checkbox" class="ml-2">Human Player</label>
</div>
<div class="flex items-center justify-center">
<input type="checkbox" id="ai-enabled-checkbox" class="h-5 w-5 rounded" checked>
<label for="ai-enabled-checkbox" class="ml-2">Enable AI</label>
</div>
</div>
<p id="seed-display" class="text-xs text-gray-500 mb-4"></p>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<h3 class="text-lg font-bold mb-2">Population Controls</h3>
<div class="flex justify-between items-center mb-4">
<label for="num-instances" class="text-right pr-2 py-1">Instances:</label>
<input type="number" id="num-instances" value="3" min="1" max="12" class="bg-gray-700 text-white rounded px-2 py-1 w-24 text-center">
<label class="text-right pr-2 py-1">Generation:</label>
<span id="generation-display" class="font-bold text-lg text-teal-300">0</span>
</div>
<button id="generate-instances-btn" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-lg w-full mt-4">Generate New Population</button>
<button id="permutate-btn" class="bg-teal-600 hover:bg-teal-700 text-white font-bold py-2 px-4 rounded-lg w-full mt-2">Generate from Parent DNA</button>
<textarea id="parent-dna-input" class="mt-4 p-2 rounded-md w-full" placeholder="Paste parent agent's DNA here..."></textarea>
<button id="extract-dna-btn" class="bg-purple-600 hover:bg-purple-700 text-white font-bold py-2 px-4 rounded-lg w-full mt-2">Extract Best Agent's DNA</button>
<textarea id="dna-display" class="mt-2 p-2 rounded-md text-left hidden" readonly></textarea>
</div>
<div>
<h3 class="text-lg font-bold mb-2">AI Parameters</h3>
<div class="space-y-3 text-sm text-left">
<h4 class="font-bold mt-4 border-b border-gray-600">Live Parameters</h4>
<div>
<label for="lookahead-depth">Lookahead Depth: <span id="lookahead-depth-value">3</span></label>
<input type="range" id="lookahead-depth" min="1" max="5" step="1" value="3">
</div>
<div>
<label for="beam-width">Beam Width: <span id="beam-width-value">5</span></label>
<input type="range" id="beam-width" min="1" max="10" step="1" value="5">
</div>
<div>
<label for="game-speed">AI Speed: <span id="game-speed-value">Normal</span></label>
<input type="range" id="game-speed" min="0" max="1000" step="10" value="500">
</div>
<h4 class="font-bold mt-4 border-b border-gray-600">Evolution Parameters</h4>
<div>
<label for="min-nn-influence">Min. NN Influence: <span id="min-nn-influence-value">0.05</span></label>
<input type="range" id="min-nn-influence" min="0" max="1.0" step="0.01" value="0.05">
</div>
<div>
<label for="action-exploration-rate">Action Exploration Rate: <span id="action-exploration-rate-value">0.10</span></label>
<input type="range" id="action-exploration-rate" min="0" max="1.0" step="0.01" value="0.1">
</div>
<div>
<label for="discount-factor">Discount Factor (γ): <span id="discount-factor-value">0.95</span></label>
<input type="range" id="discount-factor" min="0.5" max="1.0" step="0.01" value="0.95">
</div>
<div>
<label for="mutation-rate">Weight Mutation Rate: <span id="mutation-rate-value">0.20</span></label>
<input type="range" id="mutation-rate" min="0.01" max="1.0" step="0.01" value="0.2">
</div>
<div>
<label for="mutation-amount">Weight Mutation Amount: <span id="mutation-amount-value">0.50</span></label>
<input type="range" id="mutation-amount" min="0.01" max="1.0" step="0.01" value="0.5">
</div>
<div>
<label for="topology-mutation-rate">Topology Mutation Rate: <span id="topology-mutation-rate-value">0.20</span></label>
<input type="range" id="topology-mutation-rate" min="0" max="0.5" step="0.01" value="0.2">
</div>
</div>
</div>
</div>
</div>
<div id="performance-container" class="my-8 bg-gray-800 p-4 rounded-lg shadow-lg">
<button id="toggle-chart-btn" class="w-full text-left font-bold text-lg mb-2">Performance History (Click to Toggle)</button>
<div id="chart-wrapper" class="hidden">
<canvas id="performance-chart"></canvas>
</div>
</div>
<div id="game-instances-container">
<!-- Game instances will be dynamically inserted here -->
</div>
</div>
<script>
// --- GAME CONSTANTS AND CORE LOGIC ---
const COLS=10;const ROWS=20;const HIDDEN_ROWS=20;const BLOCK_SIZE=30;const COLORS={T:"#a000f0",I:"#00f0f0",O:"#f0f000",L:"#f0a000",J:"#0000f0",S:"#00f000",Z:"#f00000",GHOST:"rgba(255,255,255,0.2)"};const SHAPES={I:[[[0,0,0,0],[2,2,2,2],[0,0,0,0],[0,0,0,0]],[[0,0,2,0],[0,0,2,0],[0,0,2,0],[0,0,2,0]],[[0,0,0,0],[0,0,0,0],[2,2,2,2],[0,0,0,0]],[[0,2,0,0],[0,2,0,0],[0,2,0,0],[0,2,0,0]]],J:[[[5,0,0],[5,5,5],[0,0,0]],[[0,5,5],[0,5,0],[0,5,0]],[[0,0,0],[5,5,5],[0,0,5]],[[0,5,0],[0,5,0],[5,5,0]]],L:[[[0,0,4],[4,4,4],[0,0,0]],[[0,4,0],[0,4,0],[0,4,4]],[[0,0,0],[4,4,4],[4,0,0]],[[4,4,0],[0,4,0],[0,4,0]]],O:[[[0,3,3,0],[0,3,3,0],[0,0,0,0]]],S:[[[0,6,6],[6,6,0],[0,0,0]],[[0,6,0],[0,6,6],[0,0,6]],[[0,0,0],[0,6,6],[6,6,0]],[[6,0,0],[6,6,0],[0,6,0]]],T:[[[0,1,0],[1,1,1],[0,0,0]],[[0,1,0],[0,1,1],[0,1,0]],[[0,0,0],[1,1,1],[0,1,0]],[[0,1,0],[1,1,0],[0,1,0]]],Z:[[[7,7,0],[0,7,7],[0,0,0]],[[0,0,7],[0,7,7],[0,7,0]],[[0,0,0],[7,7,0],[0,7,7]],[[0,7,0],[7,7,0],[7,0,0]]]};const KICK_DATA={JLSTZ:[[[0,0],[-1,0],[-1,1],[0,-2],[-1,-2]],[[0,0],[1,0],[1,-1],[0,2],[1,2]],[[0,0],[1,0],[1,-1],[0,2],[1,2]],[[0,0],[-1,0],[-1,1],[0,-2],[-1,-2]],[[0,0],[1,0],[1,1],[0,-2],[1,-2]],[[0,0],[-1,0],[-1,-1],[0,2],[-1,2]],[[0,0],[-1,0],[-1,-1],[0,2],[-1,2]],[[0,0],[1,0],[1,1],[0,-2],[1,-2]]],I:[[[0,0],[-2,0],[1,0],[-2,-1],[1,2]],[[0,0],[2,0],[-1,0],[2,1],[-1,-2]],[[0,0],[-1,0],[2,0],[-1,2],[2,-1]],[[0,0],[1,0],[-2,0],[1,-2],[-2,1]],[[0,0],[2,0],[-1,0],[2,1],[-1,-2]],[[0,0],[-2,0],[1,0],[-2,-1],[1,2]],[[0,0],[1,0],[-2,0],[1,-2],[-2,1]],[[0,0],[-1,0],[2,0],[-1,2],[2,-1]]]};
const PIECE_MAP={1:"T",2:"I",3:"O",4:"L",5:"J",6:"S",7:"Z"};const PIECE_KEYS=Object.keys(SHAPES);
const DAS=10,ARR=0,LOCK_DELAY_DURATION=500,MAX_LOCK_DELAY_RESETS=15;
class SeededRandom{constructor(e){this.seed=e}next(){return this.seed=(9301*this.seed+49297)%233280,this.seed/233280}}
class TetrisGame{
constructor(e,t,s){this.id=t;this.container=e;this.rng=new SeededRandom(s);this.lastTime=0;this.agent=null;this.initUI();this.reset();}
initUI() {
this.container.innerHTML = `
<h2 class="text-xl font-bold mb-4 text-center w-full">Agent ${this.id}</h2>
<div class="flex flex-col sm:flex-row gap-4 w-full items-start justify-center">
<div class="info-panel flex-shrink-0 w-full sm:w-40 text-center">
<div class="mb-4">
<h3 class="text-lg font-bold">HOLD</h3>
<div class="h-24 flex items-center justify-center mt-2">
<canvas id="hold-canvas-${this.id}" width="80" height="80"></canvas>
</div>
</div>
<div>
<h3 class="text-lg font-bold mb-2">STATS</h3>
<p>Score: <span id="score-${this.id}">0</span></p>
<p>Lines: <span id="lines-${this.id}">0</span></p>
<p>Level: <span id="level-${this.id}">1</span></p>
<p>Combo: <span id="combo-${this.id}">0</span></p>
<p>Max Combo: <span id="max-combo-${this.id}">0</span></p>
<p>T-Spins: <span id="t-spins-${this.id}">0</span></p>
<p>B2B: <span id="b2b-${this.id}">0</span></p>
<div id="agent-dna-display-${this.id}" class="mt-2 text-left"></div>
</div>
<button id="reset-btn-${this.id}" class="reset-btn mt-4 bg-gray-600 hover:bg-gray-700 text-white font-bold py-1 px-2 rounded">Reset</button>
</div>
<div class="game-container relative flex-grow">
<canvas id="game-canvas-${this.id}" width="${10 * BLOCK_SIZE}" height="${20 * BLOCK_SIZE}"></canvas>
<div id="game-over-${this.id}" class="absolute inset-0 bg-black/75 flex items-center justify-center text-2xl font-bold text-red-500" style="display: none;">GAME OVER</div>
</div>
<div class="info-panel flex-shrink-0 w-full sm:w-40 text-center">
<h3 class="text-lg font-bold">NEXT</h3>
<div id="next-queue-${this.id}" class="flex flex-col items-center gap-2 mt-2"></div>
</div>
</div>`;
this.canvas=document.getElementById(`game-canvas-${this.id}`);this.ctx=this.canvas.getContext("2d");this.ctx.scale(BLOCK_SIZE,BLOCK_SIZE);this.holdCtx=document.getElementById(`hold-canvas-${this.id}`).getContext("2d");this.nextQueueContainer=document.getElementById(`next-queue-${this.id}`);this.nextCanvases=[];for(let e=0;e<5;e++){const t=document.createElement("canvas");t.width=80,t.height=80,this.nextQueueContainer.appendChild(t),this.nextCanvases.push(t.getContext("2d"))}this.scoreEl=document.getElementById(`score-${this.id}`);this.linesEl=document.getElementById(`lines-${this.id}`);this.levelEl=document.getElementById(`level-${this.id}`);this.comboEl=document.getElementById(`combo-${this.id}`);this.b2bEl=document.getElementById(`b2b-${this.id}`);this.maxComboEl=document.getElementById(`max-combo-${this.id}`);this.tSpinsEl=document.getElementById(`t-spins-${this.id}`);this.dnaDisplayEl=document.getElementById(`agent-dna-display-${this.id}`);this.gameOverEl=document.getElementById(`game-over-${this.id}`);document.getElementById(`reset-btn-${this.id}`).addEventListener("click",()=>this.reset());document.querySelector(`#instance-wrapper-${this.id} h2`).textContent=`Agent ${this.id}`
}
reset(){this.isRunning=!1,this.isGameOver=!1,this.score=0,this.linesCleared=0,this.level=1,this.combo=0,this.maxCombo=0,this.tSpins=0,this.b2b=0,this.finesseMoves=0,this.startTime=Date.now(),this.dropCounter=0,this.dasCounter=0,this.arrCounter=0,this.leftHeld=!1,this.rightHeld=!1,this.softDropActive=!1,this.lockDelayTimer=0,this.isTouchingFloor=!1,this.lockDelayResets=0,this.lastMoveWasRotation=!1,this.grid=this.createGrid(COLS,ROWS+HIDDEN_ROWS),this.pieceQueue=[],this.fillQueue(),this.spawnNewPiece(),this.holdPiece=null,this.canHold=!0,this.updateStats(),this.gameOverEl.style.display="none"; const wrapper = document.getElementById(`instance-wrapper-${this.id}`); if (wrapper) { wrapper.classList.remove('opacity-50'); } }
createGrid(e,t){return Array.from({length:t},()=>Array(e).fill(0))}fillQueue(){const e=[...PIECE_KEYS];while(e.length){const t=e.splice(Math.floor(this.rng.next()*e.length),1)[0];this.pieceQueue.push(t)}}
spawnNewPiece(e=null){if(this.isGameOver)return;e||(this.pieceQueue.length<7&&this.fillQueue(),e=this.pieceQueue.shift()),this.activePiece={type:e,rotation:0,x:3,y:18,finesse:0},"O"===e&&(this.activePiece.x=4),"I"===e&&(this.activePiece.y=17),this.activePiece.shape=SHAPES[this.activePiece.type][this.activePiece.rotation],this.isTouchingFloor=!1,this.lockDelayResets=0,this.lastMoveWasRotation=!1,this.checkCollision(this.activePiece.shape,this.activePiece.x,this.activePiece.y)&&this.gameOver()}
checkCollision(e,t,s,i=this.grid){for(let r=0;r<e.length;r++)for(let o=0;o<e[r].length;o++)if(0!==e[r][o]){const n=t+o,l=s+r;if(n<0||n>=COLS||l<0||l>=ROWS+HIDDEN_ROWS||i[l]&&0!==i[l][n])return!0}return!1}
lockPiece() {
const piece = this.activePiece;
if (!piece || piece.y < 0) return;
piece.shape.forEach((row, rowIndex) => { row.forEach((value, colIndex) => { if (value !== 0) { const boardY = piece.y + rowIndex; const boardX = piece.x + colIndex; if (boardY >= 0 && boardY < this.grid.length && this.grid[boardY]) { this.grid[boardY][boardX] = value; } } }); });
const spinInfo = this.lastMoveWasRotation ? this.checkSpin(piece) : { isSpin: false, isMini: false };
let clearedRows = this.clearLines();
if(spinInfo.isSpin && piece.type === 'T' && clearedRows.length > 0) this.tSpins++;
if (clearedRows.length > 0) {
let score = 0; const isDifficult = clearedRows.length >= 4 || spinInfo.isSpin;
if (spinInfo.isSpin) { score = spinInfo.isMini ? { 1: 200, 2: 400 }[clearedRows.length] || 100 : { 1: 800, 2: 1200, 3: 1600 }[clearedRows.length] || 400; } else { score = { 1: 100, 2: 300, 3: 500, 4: 800 }[clearedRows.length] || 0; }
if(this.b2b > 0 && isDifficult) score = Math.floor(1.5 * score);
this.score += score * this.level; this.combo++; this.maxCombo = Math.max(this.maxCombo, this.combo); this.score += 50 * this.combo * this.level;
isDifficult ? this.b2b++ : this.b2b = 0;
this.linesCleared += clearedRows.length; this.level = Math.floor(this.linesCleared / 10) + 1;
} else { this.combo = 0; }
if (this.grid.slice(HIDDEN_ROWS).every(r => r.every(c => c === 0))) this.score += 3500 * this.level;
this.spawnNewPiece(); this.canHold = !0; this.updateStats();
}
checkSpin(e, grid = this.grid) {
if (!e?.type || "O" === e.type || e.y < 0) return { isSpin: false, isMini: false };
const t = e.x + 1, s = e.y + 1; let i = 0, r = 0;
const o = [[s - 1, t - 1], [s - 1, t + 1], [s + 1, t + 1], [s + 1, t - 1]];
o.forEach(([y, x]) => { if (y >= 0 && grid[y]?.[x] > 0) i++; });
if (i < 3) return { isSpin: false, isMini: false };
const n = [[s - 1, t], [s, t + 1], [s + 1, t], [s, t - 1]][e.rotation];
o.forEach(([y, x], idx) => { if (!(y === n[0] && x === n[1])) { if (y >= 0 && grid[y]?.[x] > 0) r++; } });
return { isSpin: true, isMini: r < 2 };
}
clearLines(){let t=[];for(let s=this.grid.length-1;s>=0;s--)this.grid[s].every(e=>0!==e)&&t.push(s);if(t.length>0){for(const i of t)this.grid.splice(i,1);for(let i=0;i<t.length;i++)this.grid.unshift(new Array(COLS).fill(0));}return t}
move(e){if(!this.activePiece||this.checkCollision(this.activePiece.shape,this.activePiece.x+e,this.activePiece.y))return!1;return this.activePiece.x+=e,this.activePiece.finesse++,this.resetLockDelay(),this.lastMoveWasRotation=!1,!0}
rotate(e=1){const t=this.activePiece;if(!t||"O"===t.type)return!1;const s=t.rotation,i=(s+e+4)%4,r="I"===t.type?KICK_DATA.I:KICK_DATA.JLSTZ,o=1===e?2*s:2*i+1;for(const[n,l]of r[o])if(!this.checkCollision(SHAPES[t.type][i],t.x+n,t.y-l))return t.x+=n,t.y-=l,t.rotation=i,t.shape=SHAPES[t.type][i],t.finesse++,this.resetLockDelay(),this.lastMoveWasRotation=!0,!0;return!1}
resetLockDelay(){this.isTouchingFloor&&this.lockDelayResets<MAX_LOCK_DELAY_RESETS&&(this.lockDelayTimer=LOCK_DELAY_DURATION,this.lockDelayResets++)}
drop(){if(this.activePiece&&!this.checkCollision(this.activePiece.shape,this.activePiece.x,this.activePiece.y+1)){this.activePiece.y++,this.dropCounter=0,this.isTouchingFloor=!1}else{this.isTouchingFloor=!0;if(this.lockDelayTimer<=0)this.lockDelayTimer=LOCK_DELAY_DURATION}}
hardDrop(){if(!this.activePiece)return;let cellsDropped=0;while(!this.checkCollision(this.activePiece.shape,this.activePiece.x,this.activePiece.y+1)){this.activePiece.y++,cellsDropped++};this.score+=2*cellsDropped;this.lockPiece()}
hold(){if(!this.canHold||!this.activePiece)return;const heldType=this.holdPiece?this.holdPiece.type:null;this.holdPiece={type:this.activePiece.type};this.spawnNewPiece(heldType),this.canHold=!1,this.draw()}
update(e){if(!this.isRunning||this.isGameOver)return;this.handleHorizontalMovement();if(this.isTouchingFloor){this.lockDelayTimer-=e;if(this.lockDelayTimer<=0)this.lockPiece()}else{const t=1e3/this.level;const s=this.softDropActive?t/20:t;this.dropCounter+=e;if(this.dropCounter>s)this.drop()}this.draw(); if(this.agent) {this.agent.updateAgentDisplay();}}
handleHorizontalMovement(){if(this.leftHeld===this.rightHeld){this.dasCounter=0}else{this.dasCounter++;if(this.dasCounter>DAS){this.arrCounter++;if(this.arrCounter>=ARR){this.arrCounter=0;this.move(this.rightHeld?1:-1)}}}}
handleKeyDown(e){if(this.isGameOver)return;const t=["ArrowLeft","ArrowRight","ArrowDown","ArrowUp","x","X","z","Z"," ","Shift","c","C"];t.includes(e.key)&&e.preventDefault();switch(e.key){case"ArrowLeft":this.leftHeld||(this.move(-1),this.dasCounter=0),this.leftHeld=!0;break;case"ArrowRight":this.rightHeld||(this.move(1),this.dasCounter=0),this.rightHeld=!0;break;case"ArrowDown":this.softDropActive=!0,this.resetLockDelay();break;case"ArrowUp":case"x":case"X":this.rotate(1);break;case"z":case"Z":this.rotate(-1);break;case" ":this.hardDrop();break;case"Shift":case"c":case"C":this.hold()}}
handleKeyUp(e){switch(e.key){case"ArrowLeft":this.leftHeld=!1;break;case"ArrowRight":this.rightHeld=!1;break;case"ArrowDown":this.softDropActive=!1}}
draw(){this.ctx.fillStyle="#000",this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height);this.drawMatrix(this.grid,0,-HIDDEN_ROWS,this.ctx);this.drawGhostPiece();this.activePiece&&this.drawMatrix(this.activePiece.shape,this.activePiece.x,this.activePiece.y-HIDDEN_ROWS,this.ctx,this.activePiece.type);this.drawHoldPiece();this.drawNextQueue()}
drawMatrix(e,t,s,i,r){e.forEach((e,o)=>e.forEach((n,l)=>{if(n!==0){i.fillStyle=r?COLORS[r]:COLORS[PIECE_MAP[n]];i.fillRect(l+t,o+s,1,1)}}))}
drawGhostPiece(){if(!this.activePiece)return;const e={...this.activePiece};for(;!this.checkCollision(e.shape,e.x,e.y+1);)e.y++;this.ctx.globalAlpha=.3;this.drawMatrix(e.shape,e.x,e.y-HIDDEN_ROWS,this.ctx,this.activePiece.type),this.ctx.globalAlpha=1}
drawPieceInPreview(e,t){e.clearRect(0,0,80,80);if(t){const s=SHAPES[t.type][0],i="I"===t.type||"O"===t.type?18:20,r=s[0].length*i,o=s.length*i,n=(80-r)/2,l=(80-o)/2;e.fillStyle=COLORS[t.type],s.forEach((t,s)=>t.forEach((r,o)=>{r&&e.fillRect(n+o*i,l+s*i,i-1,i-1)}))}}
drawHoldPiece(){this.drawPieceInPreview(this.holdCtx,this.holdPiece)}drawNextQueue(){this.nextCanvases.forEach((e,t)=>{const s=this.pieceQueue[t];s?this.drawPieceInPreview(e,{type:s}):e.clearRect(0,0,80,80)})}
updateStats(){this.scoreEl.textContent=this.score;this.linesEl.textContent=this.linesCleared;this.levelEl.textContent=this.level;this.comboEl.textContent=this.combo;this.b2bEl.textContent=this.b2b;this.maxComboEl.textContent=this.maxCombo;this.tSpinsEl.textContent=this.tSpins;}
gameOver(){
this.isRunning=!1,this.isGameOver=!0,this.activePiece=null,this.gameOverEl.style.display="flex";
if (this.agent) { this.agent.finalFitness = this.agent.calculateFitness(this); }
console.log(`Game ${this.id} Over! Final Score: ${this.score}`);
setTimeout(checkAllGameOver, 100);
}
start(){if(this.isGameOver)this.reset();if(!this.isRunning){this.isRunning=!0,this.lastTime=0,requestAnimationFrame(this.gameLoop.bind(this))}}stop(){this.isRunning=!1}
gameLoop(e){if(this.isRunning){const t=e-(this.lastTime||e);this.lastTime=e,this.update(t),requestAnimationFrame(this.gameLoop.bind(this))}}
}
// --- NEURAL NETWORK & AGENT CLASSES ---
class Level {
constructor(inputCount, outputCount) {
this.inputs = new Array(inputCount); this.outputs = new Array(outputCount);
this.biases = new Array(outputCount); this.weights = [];
for (let i = 0; i < inputCount; i++) { this.weights[i] = new Array(outputCount); }
Level.#randomize(this, inputCount);
}
static #randomize(level, inputCount) {
for (let i = 0; i < level.inputs.length; i++) {
for (let j = 0; j < level.outputs.length; j++) {
level.weights[i][j] = (Math.random() * 2 - 1) * Math.sqrt(1 / inputCount);
}
}
for (let i = 0; i < level.biases.length; i++) { level.biases[i] = (Math.random() * 2 - 1); }
}
static feedForward(givenInputs, level) {
level.inputs = givenInputs;
for (let i = 0; i < level.outputs.length; i++) {
let sum = 0;
for (let j = 0; j < level.inputs.length; j++) { sum += level.inputs[j] * level.weights[j][i]; }
level.outputs[i] = Math.tanh(sum + level.biases[i]);
}
return level.outputs;
}
}
class NeuralNetwork {
constructor(layerConfigs) {
this.levels = [];
for (let i = 0; i < layerConfigs.length - 1; i++) {
this.levels.push(new Level(layerConfigs[i], layerConfigs[i+1]));
}
}
static feedForward(givenInputs, network) {
let outputs = givenInputs;
for (const level of network.levels) { outputs = Level.feedForward(outputs, level); }
return outputs;
}
static getDNA(network) {
const dna = { weights: [], biases: [] };
network.levels.forEach(level => {
dna.weights.push(...level.weights.flat());
dna.biases.push(...level.biases);
});
return dna;
}
static loadDNA(network, networkDNA) {
if(!networkDNA || !networkDNA.weights || !networkDNA.biases) return;
let weightIndex = 0; let biasIndex = 0;
for(const level of network.levels){
for(let i=0; i < level.weights.length; i++){
for(let j=0; j < level.weights[i].length; j++){
if(weightIndex < networkDNA.weights.length) level.weights[i][j] = networkDNA.weights[weightIndex++];
}
}
for(let i=0; i < level.biases.length; i++){
if(biasIndex < networkDNA.biases.length) level.biases[i] = networkDNA.biases[biasIndex++];
}
}
}
static mutate(network, mutationRate, mutationAmount) {
if (!network || !network.levels) return;
network.levels.forEach(level => {
for (let i = 0; i < level.biases.length; i++) {
if (Math.random() < mutationRate) {
level.biases[i] += (Math.random() * 2 - 1) * mutationAmount;
}
}
for (let i = 0; i < level.weights.length; i++) {
for (let j = 0; j < level.weights[i].length; j++) {
if (Math.random() < mutationRate) {
level.weights[i][j] += (Math.random() * 2 - 1) * mutationAmount;
}
}
}
});
}
}
class Agent {
constructor(game, dna = null, shouldMutate = true) {
this.game = game; this.thinkTimeout = null; this.isThinking = false;
this.finalFitness = 0;
this.baseWeights = {
aggregateHeight: -0.51, maxHeight: -0.2, holes: -0.36, coveredHoles: -0.8,
bumpiness: -0.18, wells: -0.72, rowTransitions: -0.1, columnTransitions: -0.1,
linesClearedPower: 1.5, combo: 3, holdBonus: 0.5, finessePenalty: -0.2,
t_spin_single_bonus: 800, t_spin_double_bonus: 1200, t_spin_triple_bonus: 1600,
mini_t_spin_single_bonus: 100, mini_t_spin_double_bonus: 200, l_spin_bonus: 400,
j_spin_bonus: 400, s_spin_bonus: 400, z_spin_bonus: 400
};
this.weightNames = Object.keys(this.baseWeights);
if (dna) { this.dna = JSON.parse(JSON.stringify(dna)); }
else {
this.dna = {
hiddenLayers: [10],
nn_influence: 0.05,
networkDNA: null,
};
}
// FIX: Mutate DNA *before* building the brain from it
if (shouldMutate) { this.mutate(); }
const featureCount = 8;
const layerSizes = [featureCount, ...this.dna.hiddenLayers, this.weightNames.length];
this.brain = new NeuralNetwork(layerSizes);
if(this.dna.networkDNA) {
NeuralNetwork.loadDNA(this.brain, this.dna.networkDNA);
}
}
mutate() {
const mutationRate = parseFloat(document.getElementById("mutation-rate").value);
const mutationAmount = parseFloat(document.getElementById("mutation-amount").value);
const topologyMutationRate = parseFloat(document.getElementById("topology-mutation-rate").value);
// FIX: Use additive mutation for influence so it can grow effectively
if (Math.random() < mutationRate) {
this.dna.nn_influence += (Math.random() * 2 - 1) * (mutationAmount * 0.1);
this.dna.nn_influence = Math.max(0, Math.min(1, this.dna.nn_influence));
}
let topologyChanged = false;
if (Math.random() < topologyMutationRate) {
if (Math.random() < 0.5 && this.dna.hiddenLayers.length > 0) { // 50% chance to add neuron
const layerIndex = Math.floor(Math.random() * this.dna.hiddenLayers.length);
this.dna.hiddenLayers[layerIndex]++;
} else { // 50% chance to add layer
this.dna.hiddenLayers.push(Math.floor(Math.random() * 5) + 3);
}
topologyChanged = true;
console.log(`Agent ${this.game.id} topology → [${this.dna.hiddenLayers}]`);
}
// FIX: Rebuild brain if topology changed, otherwise mutate existing weights
if (topologyChanged) {
const featureCount = 8;
const outputCount = this.weightNames.length;
const layerSizes = [ featureCount, ...this.dna.hiddenLayers, outputCount ];
this.brain = new NeuralNetwork(layerSizes);
this.dna.networkDNA = null; // Invalidate old DNA
} else if (this.brain) { // Only mutate if brain exists
NeuralNetwork.mutate(this.brain, mutationRate, mutationAmount);
}
}
getDNA() {
this.dna.networkDNA = NeuralNetwork.getDNA(this.brain);
return this.dna;
}
calculateFitness(game) {
let fitness = game.score;
fitness += Math.pow(game.maxCombo, 2) * 100;
fitness += game.tSpins * 5000;
return fitness;
}
start() { this.stop(); if (this.game.isRunning) this.think(); }
stop() { if(this.thinkTimeout) clearTimeout(this.thinkTimeout); this.isThinking = false; }
async think() {
if (this.isThinking || !this.game.isRunning || this.game.isGameOver || !this.game.activePiece) {
if(this.game.isGameOver) this.stop(); return;
}
this.isThinking = true;
try {
let chosenSequence = null; let useHold = false;
const beamCurrent = this.beamSearch(this.game.grid, [this.game.activePiece.type, ...this.game.pieceQueue], this.game.combo, this.game.b2b);
let bestOverallScore = beamCurrent[0]?.score ?? -Infinity;
let beamHold = [];
if (this.game.canHold) {
const piecesAfterHold = this.game.holdPiece ? [this.game.holdPiece.type, ...this.game.pieceQueue] : this.game.pieceQueue.slice(1);
if (piecesAfterHold.length > 0) {
beamHold = this.beamSearch(this.game.grid, piecesAfterHold, this.game.combo, this.game.b2b);
const holdActionScore = (beamHold[0]?.score ?? -Infinity) + (this.baseWeights.holdBonus || 0);
if (holdActionScore > bestOverallScore) {
useHold = true;
}
}
}
let targetBeam = useHold ? beamHold : beamCurrent;
if (targetBeam.length > 0) {
const explorationRate = parseFloat(document.getElementById('action-exploration-rate').value);
if (Math.random() < explorationRate && targetBeam.length > 1) {
chosenSequence = targetBeam[Math.floor(Math.random() * Math.min(targetBeam.length, 3))];
} else {
chosenSequence = targetBeam[0];
}
}
if (this.game.isGameOver) { this.stop(); return; }
if (useHold) { this.game.hold(); }
else if (chosenSequence?.moveSequence.length > 0 && this.game.activePiece) { await this.executeMove(chosenSequence.moveSequence[0]); }
else if (this.game.activePiece) { this.game.hardDrop(); }
} finally {
this.isThinking = false;
if(!this.game.isGameOver && this.game.isRunning) {
const speed = parseFloat(document.getElementById('game-speed')?.value || 500);
const thinkDelay = 1000 - speed;
if (thinkDelay > 0) this.thinkTimeout = setTimeout(() => this.think(), thinkDelay);
else requestAnimationFrame(() => this.think());
}
}
}
beamSearch(initialGrid, pieceQueue, initialCombo, initialB2B) {
const lookaheadDepth = parseInt(document.getElementById('lookahead-depth').value, 10);
const beamWidth = parseInt(document.getElementById('beam-width').value, 10);
const discountFactor = parseFloat(document.getElementById('discount-factor').value);
let beam = [{
grid: initialGrid, combo: initialCombo, b2b: initialB2B,
score: this.evaluateBoard(initialGrid).totalScore, moveSequence: []
}];
for (let depth = 0; depth < lookaheadDepth; depth++) {
if (!pieceQueue[depth]) break;
const pieceType = pieceQueue[depth];
const allNextStates = [];
for (const node of beam) {
const possiblePlacements = this.calculatePossiblePlacements(node.grid, pieceType);
for (const placement of possiblePlacements) {
const reward = this.getImmediateReward(placement, node.combo, node.b2b);
const { totalScore: stateValue } = this.evaluateBoard(placement.grid);
const totalScore = reward + (stateValue * discountFactor);
allNextStates.push({
grid: placement.grid, combo: placement.nextCombo, b2b: placement.nextB2B,
score: totalScore,
moveSequence: [...node.moveSequence, placement]
});
}
}
if (allNextStates.length === 0) break;
allNextStates.sort((a, b) => b.score - a.score);
beam = allNextStates.slice(0, beamWidth);
}
return beam;
}
getImmediateReward(placement, currentCombo, currentB2B) {
const { linesCleared, isSpin, isMini, finesse, pieceType } = placement;
let score = 0;
if(linesCleared > 0) {
if (isSpin) {
if (pieceType === 'T') {
if (isMini) score += [0, this.baseWeights.mini_t_spin_single_bonus, this.baseWeights.mini_t_spin_double_bonus][linesCleared] || 0;
else score += [0, this.baseWeights.t_spin_single_bonus, this.baseWeights.t_spin_double_bonus, this.baseWeights.t_spin_triple_bonus][linesCleared] || 0;
} else { score += (this.baseWeights[`${pieceType.toLowerCase()}_spin_bonus`] || 0); }
} else { score += Math.pow(linesCleared, this.baseWeights.linesClearedPower || 1.5) * 100; }
if(linesCleared > 0 && currentB2B > 0) score *= 1.5;
score += currentCombo * (this.baseWeights.combo || 2);
}
score += finesse * (this.baseWeights.finessePenalty || -0.2);
return score;
}
getBoardFeatures(grid) {
const columnHeights = new Array(COLS).fill(0); let aggregateHeight = 0; let holes = 0;
let coveredHoles = 0; let bumpiness = 0; let wells = 0;
let rowTransitions = 0; let columnTransitions = 0;
for (let c = 0; c < COLS; c++) {
let foundTop = false;
for (let r = 0; r < grid.length; r++) {
if (grid[r][c] !== 0) {
if (!foundTop) { columnHeights[c] = (ROWS + HIDDEN_ROWS) - r; foundTop = true; }
} else if (foundTop) {
holes++;
if (columnHeights[c] > 0) coveredHoles++;
}
}
}
const maxHeight = Math.max(...columnHeights);
for (let i = 0; i < COLS; i++) {
aggregateHeight += columnHeights[i];
if (i > 0) { bumpiness += Math.abs(columnHeights[i] - columnHeights[i - 1]); }
const leftHeight = (i === 0) ? (ROWS + HIDDEN_ROWS) : columnHeights[i-1];
const rightHeight = (i === COLS - 1) ? (ROWS + HIDDEN_ROWS) : columnHeights[i+1];
if (leftHeight > columnHeights[i] && rightHeight > columnHeights[i]) {
wells += Math.min(leftHeight, rightHeight) - columnHeights[i];
}
}
for (let r = 0; r < ROWS + HIDDEN_ROWS; r++) {
for (let c = 0; c < COLS; c++) {
const current = grid[r]?.[c] > 0;
const right = grid[r]?.[c+1] > 0;
const down = grid[r+1]?.[c] > 0;
if(c < COLS - 1 && current !== right) rowTransitions++;
if(r < ROWS + HIDDEN_ROWS - 1 && current !== down) columnTransitions++;
}
}
return { aggregateHeight, maxHeight, holes, coveredHoles, bumpiness, wells, rowTransitions, columnTransitions };
}
evaluateBoard(grid) {
const features = this.getBoardFeatures(grid);
const featureVector = Object.values(features);
const adjustments = NeuralNetwork.feedForward(featureVector, this.brain);
let totalScore = 0;
const weightKeys = Object.keys(this.baseWeights);
for(let i=0; i < weightKeys.length; i++) {
const key = weightKeys[i];
if(features[key] !== undefined) {
const base = this.baseWeights[key] || 0;
const adjustment = (adjustments[i] || 0) * this.dna.nn_influence;
const dynamicWeight = base + adjustment;
totalScore += dynamicWeight * (features[key] || 0);
}
}
return { totalScore, heuristicValues: features };
}
updateAgentDisplay() {
const displayEl = this.game.dnaDisplayEl;
if(displayEl) {
displayEl.innerHTML = `
<h4 class="font-bold text-sm mb-1">Agent DNA:</h4>
<p>Arch: [${this.dna.hiddenLayers.join(',')}]</p>
<p>Influence: ${this.dna.nn_influence.toFixed(3)}</p>
`;
}
}
calculatePossiblePlacements(grid, pieceType) {
const placements = []; const spawnX = (pieceType === "O" ? 4 : 3);
for (let r = 0; r < SHAPES[pieceType].length; r++) {
const shape = SHAPES[pieceType][r];
for (let c = -2; c < COLS + 2; c++) {
let tempY = 0;
if (this.game.checkCollision(shape, c, tempY, grid)) continue;
while (!this.game.checkCollision(shape, c, tempY + 1, grid)) tempY++;
const tempGrid = grid.map((row) => [...row]);
shape.forEach((row, y) => row.forEach((val, x) => { if (val !== 0 && tempGrid[tempY + y]) tempGrid[tempY + y][c + x] = val; }));
let linesCleared = 0;
for (let y = 0; y < tempGrid.length; y++) { if (tempGrid[y].every((cell) => cell !== 0)) linesCleared++; }
const finesseCost = Math.abs(c - spawnX) + (r > 1 ? Math.min(r, 4 - r) : r);
const { isSpin, isMini } = this.game.checkSpin({type: pieceType, x: c, y: tempY, rotation: r}, tempGrid);
const nextCombo = linesCleared > 0 ? (this.game.combo || 0) + 1 : 0;
const isDifficult = linesCleared >= 4 || isSpin;
const nextB2B = isDifficult ? (this.game.b2b || 0) + 1 : 0;
placements.push({ rotation: r, x: c, grid: tempGrid, linesCleared, isSpin, isMini, nextCombo, finesse: finesseCost, isDifficult, nextB2B, pieceType });
}
}
return placements;
}
async executeMove(move) {
if (this.game.isGameOver || !this.game.activePiece) return;
const speed = parseFloat(document.getElementById('game-speed')?.value || 500);
const moveDelay = speed < 900 ? 50 : 0;
const currentRot = this.game.activePiece.rotation;
const targetRot = move.rotation;
const diff = (targetRot - currentRot + 4) % 4;
if (diff === 3) { this.game.rotate(-1); }
else if (diff > 0) { for(let i=0; i<diff; i++) this.game.rotate(1); }
if (moveDelay > 0) await new Promise(res => setTimeout(res, moveDelay));
if (!this.game.activePiece) return;
while (this.game.activePiece.x > move.x) {
this.game.move(-1);
if (moveDelay > 0) await new Promise(res => setTimeout(res, moveDelay));
if (!this.game.activePiece) return;
}
while (this.game.activePiece.x < move.x) {
this.game.move(1);
if (moveDelay > 0) await new Promise(res => setTimeout(res, moveDelay));
if (!this.game.activePiece) return;
}
this.game.hardDrop();
}
}
// --- MAIN SCRIPT ---
let gameInstances = []; let agents = []; let humanPlayerKeyListeners = null;
let generationCounter = 0;
let performanceHistory = { labels: [], topScores: [], avgScores: [] };
let performanceChart;
function createInstanceElement(id) { const w = document.createElement('div'); w.className = 'instance-wrapper bg-gray-800/50 p-4 rounded-lg shadow-md'; w.id = `instance-wrapper-${id}`; return w; }
function updateGeneration(isNew) {
if(isNew) {
generationCounter = 0;
performanceHistory = { labels: [], topScores: [], avgScores: [] };
if(performanceChart) performanceChart.destroy();
initializeChart();
} else {
const fitnessScores = agents.map(a => a.finalFitness || 0);
const topScore = Math.max(...fitnessScores);
const avgScore = fitnessScores.reduce((a, b) => a + b, 0) / fitnessScores.length;
performanceHistory.labels.push(generationCounter);
performanceHistory.topScores.push(topScore);
performanceHistory.avgScores.push(avgScore);
performanceChart.update();
}
generationCounter++;
document.getElementById('generation-display').textContent = generationCounter;
}
function generateInstances(parentDNA = null, isNewPopulation = true) {
if(!isNewPopulation) updateGeneration(false);
else updateGeneration(true);
const instancesContainer = document.getElementById('game-instances-container'); const numInstancesInput = document.getElementById('num-instances'); const seedDisplay = document.getElementById('seed-display');
stopAll(); instancesContainer.innerHTML = ''; gameInstances = []; agents = [];
if (humanPlayerKeyListeners) { document.removeEventListener('keydown', humanPlayerKeyListeners.down); document.removeEventListener('keyup', humanPlayerKeyListeners.up); }
const seed = Date.now(); seedDisplay.textContent = `Current Seed: ${seed}`;
const count = parseInt(numInstancesInput.value, 10);
const includeHuman = document.getElementById('human-player-checkbox').checked;
let parentPreserved = false;
for (let i = 1; i <= count; i++) {
const instanceEl = createInstanceElement(i);
instancesContainer.appendChild(instanceEl);
const game = new TetrisGame(instanceEl, i, seed);
gameInstances.push(game);
if (!includeHuman || i > 1) {
let agent = new Agent(game, parentDNA, !parentPreserved);
if(parentDNA && !parentPreserved) parentPreserved = true;
agents.push(agent);
game.agent = agent;
}
}
if (includeHuman && gameInstances.length > 0) {
const humanPlayer = gameInstances[0];
humanPlayerKeyListeners = { down: (e) => humanPlayer.handleKeyDown(e), up: (e) => humanPlayer.handleKeyUp(e) };
document.addEventListener('keydown', humanPlayerKeyListeners.down);
document.addEventListener('keyup', humanPlayerKeyListeners.up);
document.querySelector('#instance-wrapper-1 h2').textContent += " (Player)";
}
}
function startAll() { gameInstances.forEach(game => game.start()); if (document.getElementById('ai-enabled-checkbox').checked) { agents.forEach(agent => agent.start()); } }
function stopAll() { gameInstances.forEach(game => game.stop()); agents.forEach(agent => agent.stop()); }
function checkAllGameOver() {
if (gameInstances.every(g => g.isGameOver)) {
let bestAgent = agents[0];
let bestFitness = -Infinity;
agents.forEach(a => {
const fitness = a.calculateFitness(a.game);
if (fitness > bestFitness) {
bestFitness = fitness;
bestAgent = a;
}
});
const parentDNA = bestAgent.getDNA();
generateInstances(parentDNA, false);
}
}
function extractBestAgentDNA() {
const dnaDisplay = document.getElementById('dna-display');
const parentInput = document.getElementById('parent-dna-input');
if (agents.length === 0) {
dnaDisplay.textContent = "No agents available.";
dnaDisplay.classList.remove('hidden'); return;
}
let bestAgent = agents[0]; let bestFitness = -Infinity;
const runningAgents = agents.filter(a => a && !a.game.isGameOver);
const targetAgents = runningAgents.length > 0 ? runningAgents : agents;
targetAgents.forEach(agent => {
const fitness = agent.calculateFitness(agent.game);
if (fitness > bestFitness) {
bestFitness = fitness;
bestAgent = agent;
}
});
if (bestAgent) {
const dnaString = JSON.stringify(bestAgent.getDNA(), null, 2);
dnaDisplay.textContent = dnaString;
parentInput.value = dnaString;
dnaDisplay.classList.remove('hidden');
}
}
function generateFromParent() {
const parentInput = document.getElementById('parent-dna-input');
try {
if (!parentInput.value) { alert("Parent DNA input is empty."); return; }
const parentDNA = JSON.parse(parentInput.value);
generateInstances(parentDNA, false);
} catch (e) {
alert("Invalid JSON in parent DNA input."); console.error("Error parsing parent DNA:", e);
}
}
function initializeChart() {
const ctx = document.getElementById('performance-chart').getContext('2d');
performanceChart = new Chart(ctx, {
type: 'line',
data: {
labels: performanceHistory.labels,
datasets: [{
label: 'Top Fitness',
data: performanceHistory.topScores,
borderColor: 'rgba(75, 192, 192, 1)',
tension: 0.1
},
{
label: 'Average Fitness',
data: performanceHistory.avgScores,
borderColor: 'rgba(255, 99, 132, 1)',
tension: 0.1
}]
},
options: {
scales: {
y: { beginAtZero: true }
}
}
});
}
// --- EVENT LISTENERS ---
function setupSlider(sliderId, valueId, isFloat = false) {
const slider = document.getElementById(sliderId);
const valueEl = document.getElementById(valueId);
slider.oninput = () => { valueEl.textContent = isFloat ? parseFloat(slider.value).toFixed(2) : slider.value; };
}
setupSlider('action-exploration-rate', 'action-exploration-rate-value', true);
setupSlider('lookahead-depth', 'lookahead-depth-value');
setupSlider('beam-width', 'beam-width-value');
setupSlider('discount-factor', 'discount-factor-value', true);
setupSlider('mutation-rate', 'mutation-rate-value', true);
setupSlider('mutation-amount', 'mutation-amount-value', true);
setupSlider('topology-mutation-rate', 'topology-mutation-rate-value', true);
document.getElementById('game-speed').oninput = function() {
const speed = parseInt(this.value, 10);
const speedValue = document.querySelector('#game-speed-value') || {};
if (speed < 10) speedValue.textContent = 'Paused';
else if (speed < 300) speedValue.textContent = 'Slow';
else if (speed < 700) speedValue.textContent = 'Normal';
else if (speed < 990) speedValue.textContent = 'Fast';
else speedValue.textContent = 'Instant';
};
document.getElementById('toggle-chart-btn').addEventListener('click', () => {
document.getElementById('chart-wrapper').classList.toggle('hidden');
});
document.getElementById('start-all-btn').addEventListener('click', startAll);
document.getElementById('stop-all-btn').addEventListener('click', stopAll);
document.getElementById('generate-instances-btn').addEventListener('click', () => generateInstances(null, true));
document.getElementById('extract-dna-btn').addEventListener('click', extractBestAgentDNA);
document.getElementById('permutate-btn').addEventListener('click', generateFromParent);
window.onload = () => {
generateInstances(null, true);
initializeChart();
};
</script>
</body>
</html>