-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
165 lines (140 loc) · 4.95 KB
/
Copy pathapp.js
File metadata and controls
165 lines (140 loc) · 4.95 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
// Available pool of genes (Characters allowed)
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
// Game State Values
let targetPhrase = "";
let currentString = "";
let stepCounter = 0;
let autoRunInterval = null;
// DOM Element Selectors
const targetInput = document.getElementById('target-input');
const stringOutput = document.getElementById('string-output');
const stepsDisplay = document.getElementById('stat-steps');
const fitnessDisplay = document.getElementById('stat-fitness');
const modeDisplay = document.getElementById('stat-mode');
const btnRandom = document.getElementById('btn-random');
const btnWeasel = document.getElementById('btn-weasel');
const autoRunCheck = document.getElementById('auto-run');
// Helper: Generate a single random character
function getRandomChar() {
return ALPHABET[Math.floor(Math.random() * ALPHABET.length)];
}
// Helper: Build a completely random string of target length
function makeRandomString(length) {
let result = "";
for (let i = 0; i < length; i++) {
result += getRandomChar();
}
return result;
}
// Compute fitness score (number of exactly matching character positions)
function getFitness(str, target) {
let score = 0;
for (let i = 0; i < target.length; i++) {
if (str[i] === target[i]) score++;
}
return score;
}
// Render the current string to the UI and apply color matching highlights
function renderDisplay() {
let htmlOutput = "";
let currentFitness = 0;
for (let i = 0; i < targetPhrase.length; i++) {
const currentChar = currentString[i] || " ";
if (currentChar === targetPhrase[i]) {
htmlOutput += `<span class="match">${currentChar === " " ? " " : currentChar}</span>`;
currentFitness++;
} else {
htmlOutput += `<span class="mismatch">${currentChar === " " ? " " : currentChar}</span>`;
}
}
stringOutput.innerHTML = htmlOutput;
stepsDisplay.innerText = stepCounter;
fitnessDisplay.innerText = `${currentFitness} / ${targetPhrase.length}`;
}
// Reset data array layouts when changing the configuration target word
function resetSimulation() {
targetPhrase = targetInput.value.toUpperCase().replace(/[^A-Z ]/g, "");
targetInput.value = targetPhrase; // Sanitize input UI element
stepCounter = 0;
currentString = makeRandomString(targetPhrase.length);
modeDisplay.innerText = "Initialized";
renderDisplay();
}
// If the input is empty or just spaces when they try to run it, set some none-empty value
function ensureValidTarget() {
if (targetPhrase.trim() === "") {
targetInput.value = "WEASEL";
resetSimulation();
}
}
// BUTTON LOGIC 1: Complete independent random regeneration
function executeRandomStep() {
stopAutoRun(); // Kill autorun loop if active
stepCounter++;
currentString = makeRandomString(targetPhrase.length);
modeDisplay.innerText = "Pure Random";
renderDisplay();
}
// BUTTON LOGIC 2: Dawkins' Cumulative Breeding Loop
function executeWeaselStep() {
stepCounter++;
modeDisplay.innerText = "Cumulative (Weasel)";
const copiesCount = 100;
const mutationRate = 0.05; // 5% chance per character slot
let bestCopy = currentString;
let bestFitness = getFitness(currentString, targetPhrase);
// Spawn 100 target organism variations based on current string
for (let i = 0; i < copiesCount; i++) {
let mutatedString = "";
for (let j = 0; j < currentString.length; j++) {
if (Math.random() < mutationRate) {
mutatedString += getRandomChar(); // Mutate slot
} else {
mutatedString += currentString[j]; // Inherit slot unchanged
}
}
let mutantFitness = getFitness(mutatedString, targetPhrase);
if (mutantFitness > bestFitness) {
bestFitness = mutantFitness;
bestCopy = mutatedString;
}
}
currentString = bestCopy;
renderDisplay();
// End run criteria check
if (currentString === targetPhrase) {
stopAutoRun();
modeDisplay.innerText = "Target Reached! 🎉";
}
}
// Auto-run clock toggles
function startAutoRun() {
if (currentString === targetPhrase) resetSimulation();
autoRunInterval = setInterval(executeWeaselStep, 40);
}
function stopAutoRun() {
clearInterval(autoRunInterval);
autoRunInterval = null;
autoRunCheck.checked = false;
}
// Event Binding Listeners
targetInput.addEventListener('input', resetSimulation);
btnRandom.addEventListener('click', () => {
ensureValidTarget();
executeRandomStep();
});
btnWeasel.addEventListener('click', () => {
stopAutoRun();
ensureValidTarget();
executeWeaselStep();
});
autoRunCheck.addEventListener('change', (e) => {
if (e.target.checked) {
ensureValidTarget();
startAutoRun();
} else {
stopAutoRun();
}
});
// Fire script initialization routine on screen draw load
window.onload = resetSimulation;