Skip to content

Instantly share code, notes, and snippets.

@kdefliese
Last active August 29, 2015 14:15
Show Gist options
  • Save kdefliese/cfe6b3063f5a9a0d367e to your computer and use it in GitHub Desktop.
Save kdefliese/cfe6b3063f5a9a0d367e to your computer and use it in GitHub Desktop.
Turtle and rabbit race
<script>
var Animal = function(s,f,n) {
this.speed = s;
this.focus = f;
this.name = n;
this.position = 0;
this.report = function() {
return this.name + " is at " + this.position;
};
this.run = function() {
if(this.focus > (Math.random() * 10)) {
this.position += this.speed;
}
};
this.powerup1 = function() {
if((Math.random() * 10) < 5) {
this.speed ++;
}
else if (((Math.random() * 10) >=5) && ((Math.random() * 10) < 9)) {
this.speed * 2;
}
else {
this.speed --;
}
};
this.powerup2 = function() {
if((Math.random() * 10) < 5) {
this.speed --;
}
else if (((Math.random() * 10) >=5) && ((Math.random() * 10) < 9)) {
this.speed ++;
}
else {
this.speed * 2;
}
};
}
var turtle = new Animal(2,8,"Beth");
var rabbit = new Animal(5,2,"David");
var distance = 50;
while(turtle.position < distance && rabbit.position < distance) {
if (turtle.position >= 15 && turtle.position <17) {
turtle.powerup1();
turtle.run();
//console.log(turtle.report);
}
else if (turtle.position >= 35 && turtle.position <37) {
turtle.powerup2();
turtle.run();
//console.log(turtle.report);
}
else {
turtle.run();
//console.log(turtle.report);
}
if (rabbit.position >= 15 && rabbit.position <=20) {
rabbit.powerup1();
rabbit.run();
//console.log(rabbit.report);
}
else if (rabbit.position >= 35 && rabbit.position <=40) {
rabbit.powerup2();
rabbit.run();
//console.log(rabbit.report);
}
else {
rabbit.run();
//console.log(rabbit.report);
}
}
console.log(turtle.report());
console.log(rabbit.report());
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment