Created
August 31, 2012 08:14
-
-
Save AndyMoreland/3550151 to your computer and use it in GitHub Desktop.
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
var html = require('fs').readFileSync(__dirname+'/index.html'); | |
var fs = require('fs'); | |
var server = require('http').createServer(function(req, res){ | |
res.end(html); | |
}); | |
server.listen(8080); | |
var nowjs = require("now"); | |
var everyone = nowjs.initialize(server); | |
var board = new Array(); | |
var chars = "abcdefghijklmnopqrstuvwxyz"; | |
var word_data = {}; | |
var already_checked_words = []; | |
var func = function(data) { | |
word_data[data] = true; | |
} | |
var arr = fs.readFileSync('words').toString().split("\n"); | |
for(i in arr) | |
func(arr[i]); | |
for(var i = 0; i < 4; i++) | |
for(var j = 0; j < 4; j++) { | |
if(!board[i]) | |
board[i] = new Array(); | |
board[i][j] = chars[Math.floor(Math.random() * chars.length)]; | |
} | |
var check_bounds = function (row, col) { | |
if(row < 0 || col < 0 || row > 3 || col > 3) | |
return false; | |
else | |
return true; | |
} | |
var build_empty_checked_moves = function() { | |
var b = new Array(); | |
for(var i = 0; i < 4; i++) | |
for(var j = 0; j < 4; j++) { | |
if(!b[i]) | |
b[i] = new Array(); | |
b[i][j] = 0; | |
} | |
return b; | |
} | |
var check_move = function (move) { | |
if(!word_data[move]) | |
return false; | |
for(i in already_checked_words) | |
if(already_checked_words[i] == move) | |
return false; | |
var recursive = function (move, checked, row, col) { | |
console.log("Being called with move: " + move); | |
if(!check_bounds(row, col)) { | |
console.log("Failing for bounds at row: " + row + ", col: " + col); | |
return false; | |
} | |
if(move == "") | |
return true; | |
if(checked[row][col] == 1) | |
return false; | |
else if(board[row][col] == move[0]){ | |
checked[row][col] = 1; | |
return recursive(move.substring(1), checked, row + 1, col) || | |
recursive(move.substring(1), checked, row + 1, col + 1) || | |
recursive(move.substring(1), checked, row + 1, col - 1) || | |
recursive(move.substring(1), checked, row, col + 1) || | |
recursive(move.substring(1), checked, row, col - 1) || | |
recursive(move.substring(1), checked, row - 1, col) || | |
recursive(move.substring(1), checked, row - 1, col + 1) || | |
recursive(move.substring(1), checked, row - 1, col - 1); | |
} else | |
return false; | |
} | |
for(var i = 0; i < board.length; i++) | |
for(var j = 0; j < board.length; j++) | |
if(recursive(move, build_empty_checked_moves(), i, j)) { | |
already_checked_words.push(move); | |
return true; | |
} | |
return false; | |
} | |
everyone.now.distributeGameMove = function(move){ | |
if(check_move(move)) { | |
everyone.now.receiveMove(this.now.name, move); | |
return true; | |
} | |
else | |
return false; | |
} | |
nowjs.on('connect', function () { | |
this.now.board = board; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment