Skip to content

Instantly share code, notes, and snippets.

@hjr265
Created March 17, 2015 10:34
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 hjr265/e86b091ae1c783e47196 to your computer and use it in GitHub Desktop.
Save hjr265/e86b091ae1c783e47196 to your computer and use it in GitHub Desktop.
RandomBot.js
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