Skip to content

Instantly share code, notes, and snippets.

@spiterman
Created September 17, 2018 09:13
Show Gist options
  • Select an option

  • Save spiterman/9efac1db392f43c260b77ce287a2b0dc to your computer and use it in GitHub Desktop.

Select an option

Save spiterman/9efac1db392f43c260b77ce287a2b0dc to your computer and use it in GitHub Desktop.
class Organism {
constructor(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
this.mutationSize = 10; //Must be greater than 2
}
reproduce() {
// Prevents negative numbers from appearing
let newR = Math.max(this.r + this.mutateTrait(), 0);
let newG = Math.max(this.g + this.mutateTrait(), 0);
let newB = Math.max(this.b + this.mutateTrait(), 0);
return new Organism(newR, newG, newB);
}
mutateTrait() {
let multiplier = Math.random() - 0.5;
if(multiplier < 0) {
return randomValue(this.mutationSize) * -1;
} else {
return randomValue(this.mutationSize);
}
}
}
function randomValue(range) {
return Math.floor(Math.random() * range);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment