I followed Filip’s advanced DNA mixing strategy, and added a condition before if for a bit more randomness.
Before entering the if/else
statement in the first for
loop, I added a modulo condition where the random number modulo over i should be less than 5 to enter into the prior if/else where it adopts exactly the DNA set from either the mother of father.
But if the modulo was 5 or greater, it would adopt the 2 digits which sit as the average between the mother and father DNA.
So, it goes ((_mumDna + dadDna) / 2) % 100
and the kid takes that spliced DNA.
So my full _mixDna
function is now:
function _mixDna(uint _dadDna, uint _mumDna) internal view returns (uint) {
uint[8] memory geneArray;
uint8 random = uint8(block.timestamp % 255); // Result is between 0 - 255, or 00000000 - 11111111 e.g. 11001011 => 0 we take genes from dad and 1 we take gene from mum
uint i = 1; // Lets Say that random = 11001011
uint index = 7;
for (i = 1; i <= 128; i*=2) {
if(random % i < 5) {
if(random & i != 0) {
geneArray[index] = uint8(_mumDna % 100); // Gets the last 2 digits
} else {
geneArray[index] = uint8(_dadDna % 100);
}
} else {
geneArray[index] = uint8(((_mumDna + _dadDna) / 2) % 100);
}
// And Then remove the last two digits from the dna
_mumDna /= 100;
_dadDna /= 100;
index -= 1;
}
uint newGene;
for (i = 0; i < 8; i++) {
// e.g. [11, 22, 33, 44, 55, 66, 77, 88]
newGene += geneArray[i];
// 11
if (i != 7) {
newGene *= 100;
// 1100 => such that when you enter the loop next time, you ADD the next 2 numbers, so => 1122
}
}
return newGene;
}
I removed most of the comments from the function explaining what’s going on because it looks supremely gross.
All about the aesthetiqs