Skip to content

Instantly share code, notes, and snippets.

@sharikovvladislav
Created May 29, 2014 07:24
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 sharikovvladislav/50fcd3510192ca51b052 to your computer and use it in GitHub Desktop.
Save sharikovvladislav/50fcd3510192ca51b052 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;
}
}
g.nodes[node.name].shape.items['1'].attr('text', alpha);
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;
}
}
g.nodes[node.name].shape.items['1'].attr('text', beta);
return beta;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment