Created
March 17, 2015 10:34
-
-
Save hjr265/e86b091ae1c783e47196 to your computer and use it in GitHub Desktop.
RandomBot.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Bot() { | |
this.grid = [] | |
for(var y = 0; y < 10; ++y) { | |
var row = [] | |
for(var x = 0; x < 10; ++x) { | |
row.push({ | |
attacked: false | |
}) | |
} | |
this.grid.push(row) | |
} | |
} | |
Bot.prototype.play = function(turn) { | |
// turn#attack(x, y) - attacks at (x, y), returns true if it successfully hits the opponent's ship and | |
// returns an object {no, kind, size, x, y, direction} when it sinks a ship | |
var squares = [] | |
for(var y = 0; y < 10; ++y) { | |
for(var x = 0; x < 10; ++x) { | |
if(!this.grid[y][x].attacked) { | |
squares.push([x, y, this.grid[y][x]]) | |
} | |
} | |
} | |
var i = Math.floor(Math.random()*squares.length) | |
turn.attack(squares[i][0], squares[i][1]) | |
squares[i][2].attacked = true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment