diff --git a/challenge.txt b/challenge.txt new file mode 100644 index 0000000..0d1bd95 --- /dev/null +++ b/challenge.txt @@ -0,0 +1,120 @@ +KotH: Bots to play the Grid Game + + +This is Grid Game (link) + +(picture here) + +It's a game based on celular automata, where the aim is to remove all of the pieces belonging to your oponent. + + +How it works: + +* Each player starts with one City (marked with C and a number), which will recularly send out walters (marked +with a number, initially the number 1) in a given direction. +* The player can change the direction of all of their walkers to be north, south, east or west. +* When walkers can't go any further, they will stay still and can be joined by other walkers, increasing their number. +* When 10 walkers join up, they become another city. +* When walkers meet walkers an cities belonging to the opponent, they deal damage, reducing the number of the pieces +* they've hit. This damage is dealt in both directions. +* There is no way to replenish the health of a city. +* The game ends when either player has no pieces remaining, or when a clear stale-mate is reached. + +## The Challenge + +To write a bot to play Grid Game, and defeat as many rival bots as possible. + +* A bot is some code which will be run on each turn of the game. +* It can only be JavaScript, sorry. +* The size of the code is immaterial. +* An object named `api` will be provided as an interface to the player. +* In this iteration, there is no way for your bot to know what it's opponent is doing. The challenge is to +write a proactive (not reactive) strategy. + +## The API + +* **api.turn** - Returns the numbered turn the game is on. +* **api.towardsX** - Aim your walkers towards the opponent on the East-West axis. +* **api.towardsY** - Aim your walkers towards the opponent on the North-South axis. +* **api.awayX** - Aim your walkers away from the opponent on the East-West axis. +* **api.awayY** - Aim your walkers away from the opponent on the North-South axis. +* **api.random_direction** - Aim you walkers in a randomly seelecttd direction. +* **api.north** - Aim your walkers North if you're in the top-left corner. Rotated for other player. Alias of awayY. +* **api.south** - Aim your walkers South if you're in the top-left corner. Rotated for other player. Alias of towardsY. +* **api.east** - Aim your walkers East if you're in the top-left corner. Rotated for other player. Alias of towardsX. +* **api.west** - Aim your walkers West if you're in the top-left corner. Rotated for other player. Alias of AwayX. + +_Note:_ The last direction method called will define the next direction the walkers move in. + +## How to write a bot + +_Note:_ I recommend playing a manual game or two first; to do this, grab a friend to play against and +select 'Manual (simple)' in the footer. + +* Go to https://ajfaraday.github.io/grid_game/ +* Select a default bot as your base (in the green or red box) +* Click Edit Code in the same coloured box. +* Click Use when you're happy with it. + +To test it: + +* Select an opponent bot as the other colour (or 'Manual' if you'd like to challenge it yourself. +* Open the developer console (f12) in case of errors in your code. +* Click Start. +* Watch the game unfold. +* Click Reset and repeat the process as desired. + +## How to deploy your bot + +Once you've written a boss you'd like to keep: + +* Go to https://gist.github.com +* Log in, if not already logged in. +* (The default page is the form to create a new gist, this is where we want to be.) +* Name the file 'bot.js' +* Copy the code into bot.js +* Save the gist +* Copy the gist ID (an alphanumeric string found after the last forward slash in the URL). + +This Gist ID can now be used to import your bot into Grid Game at any time: + +* Select Gist Id in a coloured box. +* Paste the ID into the field which appears. + +You will need to include this gist ID in your answer + +## Answer Format + +Your answer will consist of: + +* A Title, made up of a name for your bot and the gist ID +* A brief description of the bot's strategy. +* The code for your bot. +* Any notes you wish to include. + +Example answer: + +### The Faraday Cage - d9eb9a70ad0dc0fb1885be0fce032adc + +The Faraday Cage is an arrangement of four cities in the player's starting corner, which +this bot will attempt to build, which then allows it to repeatedly send two 9's towards the opponent. + + if (api.turn() < 30) { + api.awayY(); + } else if (api.turn() < 60) { + api.awayX(); + } else { + if (api.turn() % 54 < 27) { + api.towardsY(); + } else { + api.towardsX(); + } + } + +This is named after Alan Faraday, the elder brother of the game creator, who discovered it while playing the game manually. + +It has proven difficult to defeat. + +## The Tournament + + diff --git a/lib/classes/api.js b/lib/classes/api.js index dd3aef9..bcee0dc 100644 --- a/lib/classes/api.js +++ b/lib/classes/api.js @@ -1,11 +1,11 @@ GridGame.apis = []; -GridGame.classes.api = function (player, config) { +GridGame.classes.api = function(player, config) { GridGame.apis.push(this); this.player = player; - this.get_named_bot = function (name) { + this.get_named_bot = function(name) { var bot = GridGame.data.bots[name]; - if (bot === undefined) { + if(bot === undefined) { console.log("No known bot named " + name); } else { this.get_gist(bot); @@ -13,17 +13,17 @@ GridGame.classes.api = function (player, config) { }; - this.func = function () { + this.func = function() { }; - this.get_gist = function (id) { + this.get_gist = function(id) { var api = this; - if (GridGame.gist_cache[id]) { + if(GridGame.gist_cache[id]) { api.implement_code(GridGame.gist_cache[id]) } else { $.get( 'https://api.github.com/gists/' + id, - function (response) { + function(response) { var code = response.files['bot.js'].content; console.log(code); GridGame.gist_cache[id] = code; @@ -32,9 +32,9 @@ GridGame.classes.api = function (player, config) { ) } }; - this.implement_code = function (code) { + this.implement_code = function(code) { var api = this; - this.func = function () { + this.func = function() { eval(code) }; this.code_field.val(code); @@ -42,27 +42,31 @@ GridGame.classes.api = function (player, config) { // This point onwards is the public API - this.towardsX = function () { + this.towardsX = function() { this.player.change_direction(config.towardsX); }; + this.east = this.towardsX; - this.towardsY = function () { + this.towardsY = function() { this.player.change_direction(config.towardsY); }; + this.north = this.towardsY; - this.awayX = function () { + this.awayX = function() { this.player.change_direction(config.awayX); }; + this.west = this.awayX; - this.awayY = function () { + this.awayY = function() { this.player.change_direction(config.awayY); }; + this.west = this.awayY; - this.random_direction = function () { + this.random_direction = function() { this.player.go_random_direction(); }; - this.turn = function () { + this.turn = function() { return GridGame.turn_number; };