Skip to content

Instantly share code, notes, and snippets.

@moduscreate
Created March 8, 2012 15:39
Show Gist options
  • Save moduscreate/2001549 to your computer and use it in GitHub Desktop.
Save moduscreate/2001549 to your computer and use it in GitHub Desktop.
Rvrsit simple AI
nextMove : function() {
var me = this,
app = Rvrsit.app,
currentTurn = me.turn,
computerColor = me.computerColor,
nextMoves = me.findNextMoves(currentTurn),
nextMoveIndex = -1,
numVisibleChips = Object.keys(me.visChips).length,
nextMove;
if (nextMoves.length < 1) {
// are all chips visible?
if (numVisibleChips == me.boardSize) {
// if so, calc score & end game!
me.halt = true;
Rvrsit.app.fireEvent('endgame', this, this.getScore());
return;
}
else /*if(currentTurn != computerColor)*/ {
// Count the chips. Are all black or white?
var score = me.getScore();
// if so, end game
if (score.white == 0 || score.black == 0) {
app.fireEvent('winner', this, currentTurn, score[currentTurn]);
me.halt = true;
return;
}
app.fireEvent('nomoves', this, currentTurn);
}
}
if (me.halt) {
return;
}
if (currentTurn != computerColor || this.mode != 'single') {
return;
}
// hard
var moveIdx = 0,
flattenedStack,
potentialMove,
moveCount = 0;
for (; moveIdx < nextMoves.length; moveIdx++) {
potentialMove = nextMoves[moveIdx];
flattenedStack = me.flattenChipStacks(potentialMove.stacks);
if (flattenedStack.length > moveCount) {
nextMove = potentialMove;
}
}
if (!nextMove) {
return;
}
me.swapTurn();
var chipsToFlip = me.flattenChipStacks(nextMove.stacks);
nextMove.chip.startFlip(currentTurn);
nextMove.chip.processChipStacks(currentTurn, chipsToFlip);
// debug stuff
var chipItemIds = [];
Ext.each(chipsToFlip, function(chip, index) {
chipItemIds[index] = chip.itemId;
});
Rvrsit.app.fireEvent('chipFlips', {
turnColor : currentTurn,
chipItemIds : chipItemIds
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment