Created
June 11, 2014 21:19
-
-
Save macikokoro/c9da16ef64aadcf05de8 to your computer and use it in GitHub Desktop.
Fun JavaScript game based on the rabbit and the turtle story.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
var turtle, rabbit,monkey, km; | |
function Animal(name, speed, focus) { | |
this.name = name; | |
this.speed = speed; | |
this.position = 0; | |
this.focus = focus; | |
this.run = function() { | |
if (Math.random() * 10 < this.focus) { | |
this.position = this.position + this.speed; | |
} | |
} | |
} | |
turtle = new Animal("Beth", 1, 10); | |
rabbit = new Animal("Peter", 4, 2); | |
monkey = new Animal("George", 6, 1); | |
km = 20; | |
while (rabbit.position < km && turtle.position < km && monkey.position < km) { | |
rabbit.run(); | |
turtle.run(); | |
monkey.run(); | |
console.log(rabbit.name + ": " + rabbit.position + ", " + turtle.name + ": " + turtle.position + ", " + monkey.name + ": " + monkey.position); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment