Skip to content

Instantly share code, notes, and snippets.

@rahim
Created December 6, 2012 14:31
Show Gist options
  • Save rahim/4224829 to your computer and use it in GitHub Desktop.
Save rahim/4224829 to your computer and use it in GitHub Desktop.
bob
function Robot(robot) {
this.rng = new RandomNumberGenerator();
}
// well, we need to do something...
// whenever our robot is idle, this method gets called.
Robot.prototype.onIdle = function(ev) {
var robot;
robot = ev.robot;
robot.ahead(this.rng.nextRandomNumber()*700);
robot.rotateCannon(this.rng.nextRandomNumber()*720 - 360);
robot.back(this.rng.nextRandomNumber()*700);
robot.rotateCannon(this.rng.nextRandomNumber()*720 - 360);
robot.turn(this.rng.nextRandomNumber()*20);
};
// this method gets called whenever we hit another robot...
Robot.prototype.onRobotCollision = function(ev) {};
// this method gets called whenever we hit a wall...
Robot.prototype.onWallCollision = function(ev) {};
// yay we see another robot! time to wreak some havoc...
Robot.prototype.onScannedRobot = function(ev) {
var robot;
robot = ev.robot;
robot.fire(1);
};
// ohhh... we were hit by another robot...
Robot.prototype.onHitByBullet = function(ev) {
var robot;
robot = ev.robot;
robot.turn(90 - ev.bulletBearing);
};
function RandomNumberGenerator() {
this.seed = 2345678901;
this.A = 48271;
this.M = 2147483647;
this.Q = this.M / this.A;
this.R = this.M % this.A;
this.oneOverM = 1.0 / this.M;
this.next = this.nextRandomNumber;
return this;
}
RandomNumberGenerator.prototype.nextRandomNumber = function() {
var hi = this.seed / this.Q;
var lo = this.seed % this.Q;
var test = this.A * lo - this.R * hi;
if(test > 0) {
this.seed = test;
} else {
this.seed = test + this.M;
}
return (this.seed * this.oneOverM);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment