-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoji_maker.js
249 lines (211 loc) · 6.9 KB
/
moji_maker.js
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
'use strict'
const INPUT = process.env.INPUT || 'results.json';
const COUNT = process.env.COUNT || 1;
const source = require('./' + INPUT);
const DNA_BITS = 2 * 8;
const DNA_SIZE = 2 ** DNA_BITS;
const MAX_WHITESPACE = 4;
const AVERAGE_CHANCE = 0.25;
const DOMINANT_CHANCE = 0.75;
const MUTATION_CHANCE = 0.5;
const MUTATION_SIZE = 8;
const GENE_TYPES = [
'mouths',
'WHITESPACE',
'eyes',
'insides',
'WHITESPACE',
'sides',
'arms',
'outsides',
'WHITESPACE'
];
// Build arrays of tuples for each part category with the format:
// [[<part>, [<tags>]], [<part>, [<tags>]]]
const parts = Object.keys(source).reduce((parts, category) => {
parts[category] = Object.keys(source[category])
.reduce((catArray, part) => {
const count = source[category][part].count;
const tags = source[category][part].tags;
return catArray.concat(Array
.apply(null, Array(count))
.map(() => [part, tags]));
}, []);
return parts;
}, {});
const randomDna = length => {
return Array.apply(null, Array(length)).map(() => {
return Math.floor(Math.random() * DNA_SIZE);
})
};
const intToString = (integer, radix, size = null) => {
const bits = integer.toString(radix);
if (size === null) size = bits.length;
const padding = Array.apply(null, Array(size)).map(() => '0').join('');
return (padding + bits).slice(-size);
}
const dnaToString = dnaArray => {
return dnaArray
.map(int => intToString(int, 16, DNA_BITS / 4))
.join('');
};
const dnaToArray = dnaString => {
return dnaString
.match(/[0-9a-f]{4,4}/g)
.map(hex => parseInt(hex, 16));
}
const getPart = (category, indexDna, whitespaceDna) => {
const conversion = parts[category].length / DNA_SIZE;
const partIndex = Math.floor(indexDna * conversion);
const part = parts[category][partIndex];
if (!whitespaceDna) return part;
const spaces = intToString(whitespaceDna, 2, DNA_BITS)
.split('')
.map(b => Number(b));
const chars = part[0].split('').reverse();
let spacedPart = '';
const maxSpaces = Array
.apply(null, Array(MAX_WHITESPACE))
.map(() => ' ')
.join('');
for (let i = 0; true; i++) {
if (i >= spaces.length) i = 0;
if (spaces[i] === 0) spacedPart += ' ';
if (spaces[i] === 1 || spacedPart.slice(-MAX_WHITESPACE) === maxSpaces) {
let char = chars.pop();
if (!char) return [spacedPart, part[1]];
spacedPart += char;
}
}
};
const getMojiParts = dnaArray => {
// DNA should be in the order:
// mouth, eyes, insides, sides, arms, outsides
return {
mouth: getPart('mouths', dnaArray[0], dnaArray[1]),
eyes: getPart('eyes', dnaArray[2]),
insides: getPart('insides', dnaArray[3], dnaArray[4]),
sides: getPart('sides', dnaArray[5]),
arms: getPart('arms', dnaArray[6]),
outsides: getPart('outsides', dnaArray[7], dnaArray[8])
};
};
const getTags = mojiParts => {
const tagSet = Object.keys(mojiParts)
.map(partKey => mojiParts[partKey][1])
.reduce((flat, tags) => flat.concat(tags), [])
.reduce((tagSet, tag) => ({ [tag]: true, ...tagSet }), {});
return Object.keys(tagSet);
}
const displayCryptomoji = mojiParts => {
const parts = Object.keys(mojiParts)
.map(partKey => [partKey, mojiParts[partKey][0]])
.reduce((parts, [k, v]) => ({ [k]: v, ...parts }), {});
let moji = parts.mouth;
moji = parts.eyes.replace('%', moji);
moji = parts.insides.replace('%', moji);
moji = parts.sides.replace('%', moji);
// Add off center arms if applicable
const isOffRight = parts.arms.length === 3 &&
parts.arms[0] === '%' &&
parts.arms[1] === parts.arms[2];
const isOffLeft = parts.arms.length === 3 &&
parts.arms[2] === '%' &&
parts.arms[0] === parts.arms[1];
if (isOffRight) {
moji = moji[0] + parts.arms[1] + moji.slice(1) + parts.arms[2];
} else if (isOffLeft) {
moji = parts.arms[0] + moji.slice(0, -1) + parts.arms[1] + moji.slice(-1);
} else {
moji = parts.arms.replace('%', moji);
}
moji = parts.outsides.replace('%', moji);
return moji.trim();
}
const spawnCryptomoji = () => {
const categories = Object.keys(parts);
const dnaArray = randomDna(GENE_TYPES.length);
return dnaToString(dnaArray);
};
const breedCryptomoji = (sireDna, breederDna) => {
const sireArray = dnaToArray(sireDna);
const breederArray = dnaToArray(breederDna);
const childArray = sireArray
.map((gene, i) => [gene, breederArray[i]])
.map(([sGene, bGene], i) => {
if (Math.random() < AVERAGE_CHANCE) {
return Math.floor((sGene + bGene) / 2);
}
if (GENE_TYPES[i] === 'WHITESPACE') {
return Math.random() < 0.5 ? sGene : bGene;
}
const counts = source[GENE_TYPES[i]];
const sPart = getPart(GENE_TYPES[i], sGene)[0];
const bPart = getPart(GENE_TYPES[i], bGene)[0];
const sCount = counts[sPart].count;
const bCount = counts[bPart].count;
if (Math.random() < DOMINANT_CHANCE) {
return sCount > bCount ? sGene : bGene;
}
return sCount < bCount ? sGene : bGene;
})
.map((gene, i) => {
if (Math.random() > MUTATION_CHANCE) return gene;
const shift = Math.floor(Math.random() * 2 * MUTATION_SIZE) - MUTATION_SIZE;
if (GENE_TYPES[i] !== 'WHITESPACE') {
const conversion = DNA_SIZE / parts[GENE_TYPES[i]].length;
return gene + Math.floor(shift * conversion);
};
// For whitespace, up to eight wrapping contiguous bits might be mutated
const size = Math.abs(shift);
const index = Math.floor(Math.random * DNA_BITS);
const mutation = Math.floor(Math.random() * 2 ** size);
const mutationBits = intToString(mutation, 2, size);
const geneBits = intToString(gene, 2, DNA_BITS);
let finalBits = null;
if (index + size < DNA_BITS) {
finalBits = geneBits.slice(0, index) +
mutationBits +
geneBits.slice(index + size);
} else {
const mutationIndex = DNA_BITS - index;
finalBits = mutationBits.slice(mutationIndex) +
geneBits.slice(size - mutationIndex, index) +
mutationBits.slice(0, mutationIndex);
}
return parseInt(finalBits, 2);
});
return dnaToString(childArray);
};
const parseDna = dna => {
const dnaArray = dnaToArray(dna);
const mojiParts = getMojiParts(dnaArray);
return {
moji: displayCryptomoji(mojiParts),
dna,
tags: getTags(mojiParts)
};
}
const printMoji = dna => {
const { moji, tags } = parseDna(dna);
console.log(moji);
console.log(tags);
console.log(dna);
console.log();
}
for (let i = 0; i < COUNT; i++) {
const sire = spawnCryptomoji();
const breeder = spawnCryptomoji();
console.log('---------');
console.log();
console.log('Parents:');
printMoji(sire);
printMoji(breeder);
console.log();
console.log('Children:');
printMoji(breedCryptomoji(sire, breeder));
printMoji(breedCryptomoji(sire, breeder));
printMoji(breedCryptomoji(sire, breeder));
console.log('---------');
console.log();
}