FacebookTwitterGoogle+Share

Rock, paper, scissors, bots!

This Google AI challenge thing seems so neat, and like it’d be fun! Because who doesn’t like bots??

A couple of nights ago I made a rock/paper/scissors bot arena. You can make bots using Javascript and have them compete.

It seemed like it’d be fun to make, and who knows, maybe it’ll trick some people into trying out some easy programming since it’s all web-based.

It’s a little weirder than normal Javascript, but that’s because the code gets wrapped into an object before use:

function bot1() {
	this.name = "Random (from array)";
	// Actual bot code starts here
	this.ROCK = 0;
	this.PAPER = 1;
	this.SCISSORS = 2;
	/**
	 * This is the deciding function for your bot.
	 * It should return ROCK, PAPER, or SCISSORS.
	 **/
	this.evaluate = function() {
		var values = [0, 1, 2];
		// Pick a random element in the array
		return values[ Math.floor( Math.random()*values.length ) ];
	}
	 // And ends here
}

There are a couple of special functions:

  • this.evaluate = function() gets called when it’s time for the bot to throw down.
  • this.opponentPicked = function( v ) (if it exists) gets called with your opponents choice for the game that’s just finished.

A good example of the opponentPicked function is the Watcher bot.

this.ROCK = 0;
this.PAPER = 1;
this.SCISSORS = 2;

this.memory = [0,0,0];

/**
 * This is the deciding function for your bot.
 * It should return ROCK, PAPER, or SCISSORS.
 **/
this.evaluate = function() {
	var total = 0;
	for( var i=0; i < this.memory.length; i++ )
		total += this.memory[i];
	var po = Math.floor( Math.random()*total );
	
	for( var i=0; i < this.memory.length; i++ )
		if ( po > this.memory[i] ) {
			po - this.memory[i];	
		} else 
			return (i+1)%3;
	return 0;	
}

/**
 * Let's you know what you're opponent has picked.  
 * This gets called after evaluate.
 * @param int v
 */
this.opponentPicked = function( v ) {
	if ( v >= 0 && v < this.memory.length ) 
		this.memory[ Math.floor( v ) ]++;
}

There are a bunch of example bots anyway. Just don't try this is going to be bad!

 

Comments

  1. k says:

    I worked on probability tree, hax0r, and defender. Also tweaked random array and watcher.

  2. Brad says:

    hax0r is awesome!

  3. k says:

    ty!

    Idea for second hax0r when you fix the use of global values: replace Math.random() with a wrapper which remembers values, extract the opponent bot type from the url, download his code and evaluate it.
    If Math.random can’t be assigned to, I guess one would have to emulate all major browser PRNGs in javascript =S

You must be logged in to post a comment.