function play(state){ // Your code here // // 'state' contains the objects that you currently see: // state.bots - array of your bots // state.wrenches - array of wrenches on the ground // state.castles - castles that you can occupy (there are 3 total) // state.others - bots from other players // // You can do: // var bot = state.bots[0]; // get your first bot // bot.moveTo(state.wrenches[0]); // move 1 towards something (diagonal OK) // bot.collect(); // collect a wrench on the ground (maximum 5 wrenches per bot) // bot.build(); // spend 2 wrenches to build another bot (maximum 256 bots) // bot.attack(other); // remove 1 wrench from another bot (so wrenches are really your health points) // only one action at a time; // // Your goal is to occupy as many castles as possible, // you occupy a castle when you have at least 1 bot in it // you cannot enter a castle where there are other players bot in // the computer is playing against you in solo mode // // There is a more complete documentation at the top of the page // Happy coding! }
Line break

In machinegame, your goal is to find the castles, invade them and occupy them as long as possible

You control bots not with a traditional UI but by writing code. Each turn, your code gets executed to decide what actions your bots are going to take

Compiling and running

You add your code in the function play(state){} and compile it, then run the simulation with the play button, or execute only one turn with the step button. At any point you can reset the state of the game with the reset button.

The state object

Your play function gets passed a state object that contains everything you need to play:

  • state.bots is an array of all your bots
  • state.wrenches is an array of wrenches that are visible on the ground. Wrenches are the main resource of the game
  • state.castles is an array of the castles that are visible
  • state.others is an array of the other bots that are visible
Now let's see what you can do with your bots:

Selecting your bots

The bots are in the state.bots array, in no particular order. You can select the first available bot by doing: state.bots[0]. What is very common though is to loop through the available bots one by one:

function play(state){
  for (var i = 0; i < state.bots.length; i++){
    var bot = state.bots[i];
    // do action with bot
  }
}

Moving around

Objects in the game have a x and a y coordinate. You can move your bot 1 pixel, including diagonal, by doing: bot.moveTo({x:1,y:1}). Your bot will move (including in diagonal) to the 1,1 object.

Several bots can move in the same spot, even with other player's bots.

For example, to move your first bot to the first wrench:

function play(state){
  var bot = state.bots[0];
  var wrench = state.wrenches[0];
  if(wrench){
    bot.moveTo(wrench);
  }
  else{
    console.log("No visible wrench");
  }
}

Collecting wrenches

To have a bot collect a wrench, you must first move to the wrench you want to collect and then call the bot.collect() method. The bot will collect a wrench from the ground if any.Here is a function that will lead your first bot to a wrench and collect it:

function play(state){
  var bot = state.bots[0];
  var wrench = state.wrenches[0];
  if(wrench){
    // we check if we are on the wrench
    if(bot.x === wrench.x && bot.y === wrench.y){
      bot.collect();
    }
    else{
      bot.moveTo(wrench);
    }
  }
  else{
    console.log("No visible wrench");
  }
}

Building other bots

Once you have collected 2 wrenches, you can build another bot by doing bot.build(). This costs 2 wrenches and create another bot where your bot is standing

Let's reformat our function to have each bot try to go to a wrench and then build other bots:

function play(state){
  for (var i = 0; i < state.bots.length; i++){
    var bot = state.bots[i];
    if(bot.wrenches >= 3){
      bot.build();
    }
    else if(i < state.wrenches.length){
      var wrench = state.wrenches[i];
      // we check if we are on the wrench
      if(bot.x === wrench.x && bot.y === wrench.y){
        bot.collect();
      }
      else{
        bot.moveTo(wrench);
      }
    }
    else{
      console.log("No visible wrench");
    }
  }
}
This function will have bots reach for a random wrench, collect enough to build another bot and then build one.

If you play this code, you'll notice that it's not very efficient. Bots often get stuck, don't know what to do, and end up being killed by the computer. There are several problems here:

  • bot i is reaching for wrench i but this is not necessarily the closest wrench
  • Once there are no more wrenches visible, bots do not know what to do
but it's a start

Attacking

If you played a little with the code already, you will have noticed that other bots are killing yours. You can attack another bot by doing: bot.attack(target). An attack will remove 1 wrench to the target, and if the target reaches 0 wrenches it will be removed from the game. This means that wrenches are your health points in the game

To successfully attack another bot, you must be within reaching distance (so next to you, even diagonally)

Castles

In the solo game, there are 3 castles, spawned withing 64 units from where you start. You occupy a castle by moving one of your bots in it, and you can't move in a castle where a bot from another player is (you'd have to kill those bots)

Once you are in a castle, your number of occupied castles grows.

Time matters

At each turn, all players get called at the same time. The player to answer the fastest will get his actions executed first, which his a massive advantage: if 2 bots compete for the same wrench, the firt one to execute the action will take it, and the second one will just miss his action and do nothing.

A few notes

Your code is saved in your browser's localStorage every time you compile. so you can safely close your window and come back, your code will still be there.

Your code is sent to a WebWorker and executed in a different thread, so you have only access to barebone javascript features. Browser functionality such as localStorage or AJAX calls are not possible.