Skip to content

Instantly share code, notes, and snippets.

@chrislloyd
Created September 4, 2015 23:45
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 chrislloyd/2af96d671126714304ac to your computer and use it in GitHub Desktop.
Save chrislloyd/2af96d671126714304ac to your computer and use it in GitHub Desktop.
var WebSocket = require('ws')
var ws = new WebSocket('ws://localhost:5000')
function rand(lim) {
return Math.floor(Math.random(lim) * lim)
}
function sample(col) {
return col[rand(col.length)]
}
ws.on('message', function(message) {
// parse the state from the server
var state = JSON.parse(message)
// print out debugging information
console.log('tick', state.tick)
console.log('me', state.me)
// Just move randomly
var dirs = ['←', '↓', '↑', '→']
var cmd = ['MOVE', sample(dirs)]
// send the next command
ws.send(JSON.stringify(cmd))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment