Skip to content

Instantly share code, notes, and snippets.

@roblabla
Created September 24, 2012 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roblabla/3778994 to your computer and use it in GitHub Desktop.
Save roblabla/3778994 to your computer and use it in GitHub Desktop.
Scalatron bot javax.script
// Tutorial Bot #9: The hacker's RPG !
import javax.script._
class ControlFunctionFactory {
val manager = new ScriptEngineManager
val engine = manager.getEngineByExtension("js")
engine.eval("""
importPackage(java.util); // This is UGLYYYYYYY
// Rhino imports ALL CLASSES IN A PACKAGE !
// this is equivalent to import java.util.*/java.util._
function XY(dirx,diry) {
this.x = dirx;
this.y = diry;
}
XY.prototype.isNonZero = function() {
return (this.x != 0 || this.y !=0);
}
XY.prototype.isZero = function() {
return (this.x == 0 && this.y == 0);
}
XY.prototype.isNonNegative = function() {
return (this.x >= 0 && this.y >= 0);
}
XY.prototype.updateX = function(newX) {
return new XY(newX, this.y);
}
XY.prototype.updateY = function(newY) {
return new XY(this.x, newy);
}
XY.prototype.addToX = function(dx) {
return new XY(this.x + dx, this.y);
}
XY.prototype.addToY = function(dy) {
return new XY(this.x, this.y + dy);
}
XY.prototype.add = function(other) {
return new XY(this.x + other.x, this.y + other.y);
}
XY.prototype.substract = function(other) {
return new XY(this.x - other.x, this.y - other.y);
}
XY.prototype.multiply = function(factor) {
return new XY(this.x * factor, this.y * factor);
}
XY.prototype.distanceTo = function(other) {
this.substract(other).length;
}
XY.prototype.length = function() {
sqrt(this.x*this.x + this.y*this.y);
}
XY.prototype.negate = function() {
new XY(-this.x, -this.y);
}
XY.prototype.negateX = function() {
new XY(-this.x, this.y);
}
XY.prototype.negateY = function() {
new XY(this.x, -this.y);
}
XY.prototype.toString = function() {
return "" + this.x + ":" + this.y
}
var XYFactory = new Object();
XY.Zero = new XY(0,0);
XY.One = new XY(1,1);
XY.Right = new XY(1,0);
XY.RightUp = new XY(1,-1);
XY.Up = new XY(0,-1);
XY.UpLeft = new XY(-1,-1);
XY.Left = new XY(-1,0);
XY.LeftDown = new XY(-1,1);
XY.Down = new XY(0,1);
XY.DownRight = new XY(1,1);
XY.random = function(random) {
return new XY(random.nextInt(3)-1, random.nextInt(3)-1);
}
XY.fromString = function(s) {
var xy = s.split(":");
return new XY(parseInt(xy[0]), parseInt(xy[1]));
}
function parse(command) {
var splitParam = function(param) {
var segments = param.split('=');
var temp = {};
temp[segments[0]] = segments[1];
return temp;
}
var segments = command.split('(');
var params = segments[1].slice(0,-1).split(',');
var keyValuePairs = {};
keyValuePairs["opcode"] = segments[0];
for (var i = 0; i < params.length; i++) {
var temp = splitParam(params[i]);
for (var key in temp) {
keyValuePairs[key] = temp[key];
}
}
return keyValuePairs;
}
var ControlFunction = new Object();
ControlFunction.rnd = new Random();
ControlFunction.respond = function(input) {
var parseResult = parse(input);
if(parseResult["opcode"] == "React") {
if(parseInt(parseResult["generation"]) == 0) {
var temp = this.rnd.nextDouble();
if (parseInt(parseResult["energy"]) >= 100 && this.rnd.nextDouble() < 0.50) {
var heading = XY.random(this.rnd);
return "Spawn(direction=" + heading + ",heading=" + heading + ")";
} else {
return "Log(text=I am here energy is " + parseResult["energy"] + " and rnd is " + temp + ")";
}
} else {
var headingStr = parseResult["heading"];
var heading = XY.fromString(headingStr);
return "Move(direction=" + heading + ")";
}
} else {
return "Log(text=Imthere)";
}
}
""")
val inv = engine.asInstanceOf[Invocable]
val ControlFunction = engine.get("ControlFunction")
def create = respond _
def respond(input: String) = inv.invokeMethod(ControlFunction, "respond", input)
}
@roblabla
Copy link
Author

note . this might not work, will copy paste the current working code in an hour or so.

@roblabla
Copy link
Author

works now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment