Skip to content

Instantly share code, notes, and snippets.

@sharikovvladislav
Created May 28, 2014 19:08
Show Gist options
  • Save sharikovvladislav/28d96af3b80b317274c7 to your computer and use it in GitHub Desktop.
Save sharikovvladislav/28d96af3b80b317274c7 to your computer and use it in GitHub Desktop.
function alphabeta(node, depth, alpha, beta, isMax, g) {
g.nodes[node.name].shape.items['0'].attr('fill', 'green');
if((depth == 0) || (node.isTerminal == true)) {
return node.value;
}
if(isMax) {
console.log('maximizing');
for (var i in node.children) {
var child = node.children[i];
console.log(g.nodes[child.name]);
alpha = Math.max(alpha, alphabeta(child, depth-1, alpha, beta, false, g));
if(beta <= alpha) {
console.log('beta '+beta+' alpha '+alpha);
break;
}
}
return alpha;
} else {
console.log('minimizing');
for (var i in node.children) {
var child = node.children[i];
console.log(g.nodes[child.name]);
beta = Math.min(beta, alphabeta(child, depth-1, alpha, beta, true, g));
if (beta <= alpha) {
console.log('beta '+beta+' alpha '+alpha);
break;
}
}
return beta;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment