Programming competition

Well, this piece of crap can't consistently beat the demo. I want to apologize to my opponent for putting up such junk against his; and if this thing beats his I am going to look like an a$$hole but I really doubt that will be the case. I plead laziness. I wrote this in the last two hours and spend 1:30 of that trying to figure out why chrome stopped letting me debug a web worker... Place this in a js file in the ai folder, going to have to change the index to match. Voters can get the folders from my opponent because he has it hosted and really don't want to go through the trouble of creating an anonymous transfer method. /* Chess AI thing, ADreamOfLiberty http://www.debate.org... For: http://www.debate.org... */ importScripts('../js/chess.min.js'); var timeLeft = 5 * 60000; var numberOfMovesLeft = 120; var game; onmessage = function (evt) { var timeWhenCalled = new Date().getTime(); var timeToThink = calculateTimeToThink(); fen = evt.data; game = new Chess(fen); possibleMoves = game.moves({ verbose: true }); evaluatedMoves = new Array(possibleMoves.length); highestValueIndex = Math.floor(Math.random() * possibleMoves.length); highestValue = 0; //Initial evaluation for (var i = 0; i < possibleMoves.length; i++) { evaluatedMoves[i] = evaluateMove(possibleMoves[i]); if (evaluatedMoves[i].value > highestValue) { highestValue = evaluatedMoves[i].value; highestValueIndex = i; } } //Refine with predictions var index = 0; while ((new Date().getTime() - timeWhenCalled) < timeToThink) { if (evaluatedMoves[index] == null) { var oops; } makePredictions(evaluatedMoves[index]); if (evaluatedMoves[index].value > highestValue) { highestValue = evaluatedMoves[index].value; highestValueIndex = index; } index++; if (index >= evaluatedMoves.length) { break; } } numberOfMovesLeft -= 1; var timeUsedUp = new Date().getTime() - timeWhenCalled; timeLeft -= timeUsedUp; postMessage(evaluatedMoves[highestValueIndex].move); }; //Values /*PawnValue = { FENCharacter: "P", Value: 10 }; KnightValue = { FENCharacter: "N", Value: 30 }; BishopValue = { FENCharacter: "B", Value: 50 }; RookValue = { FENCharacter: "R", Value: 50 }; QueenValue = { FENCharacter: "Q", Value: 100 }; KingValue = { FENCharacter: "K", Value: 100000 };*/ var Values = {"p" : 10, "n" : 30, "b" : 50, "r" : 50, "q" : 100, "k" : 100000}; //Example evaluated move: // {value : 10, move: "d5", prediction : {state : Chess instance, moves: [recursive evaluated move entries]}} function evaluateMove(move) { var value = 0; if (move.flags.indexOf("c") != -1) { value = Values[move.captured]; } return { value: value, move: move.san }; } function makePredictions(evaluatedMove) { var currentChessContext = new Chess(game.fen()); var san = evaluatedMove.move currentChessContext.move(san); //At this point it is the enemies turn, so here the value is negative because we don't want them to capture us. var predictedPossibleMoves = currentChessContext.moves({ verbose: true }); var predictedEvaluatedMoves = new Array(predictedPossibleMoves.length); var highestIndex = 0; var highestValue = 0; for (var i = 0; i < predictedPossibleMoves.length; i++) { predictedEvaluatedMoves[i] = evaluateMove(predictedPossibleMoves[i]); evaluatedMove.value -= predictedEvaluatedMoves[i].value * 1.2; if (predictedEvaluatedMoves[i].value > highestValue) { highestIndex = i; } } if (predictedEvaluatedMoves[highestIndex] != null) { //Look one more currentChessContext.move(predictedEvaluatedMoves[highestIndex].move.san); //At this point it is the our turn again var predictedPossibleMoves = currentChessContext.moves({ verbose: true }); var predictedEvaluatedMoves = new Array(predictedPossibleMoves.length); for (var i = 0; i < predictedPossibleMoves.length; i++) { predictedEvaluatedMoves[i] = evaluateMove(predictedPossibleMoves[i]); evaluatedMove.value += predictedEvaluatedMoves[i].value * 0.8; } } } function calculateTimeToThink() { if (numberOfMovesLeft <= 5) { return timeLeft / 25; } else { return timeLeft / numberOfMovesLeft; } }