Skip to content

Instantly share code, notes, and snippets.

@pengcheng95
Last active January 16, 2018 05:24
Show Gist options
  • Select an option

  • Save pengcheng95/4f461d899d7dc3587b543c025389a5e7 to your computer and use it in GitHub Desktop.

Select an option

Save pengcheng95/4f461d899d7dc3587b543c025389a5e7 to your computer and use it in GitHub Desktop.
MCTS Tic Tac Toe
class Node {
constructor(state, node) {
if (arguments.length === 1) {
this.state = state;
this.parent;
this.childArray = [];
} else if (arguments.length === 2) {
this.state = new State(null, node.state);
if (node.parent !== (null || undefined)) {
this.parent = node.parent;
}
this.childArray = [];
node.childArray.forEach(child => {
this.childArray.push(new Node(null, child));
})
} else {
this.state = new State();
this.parent;
this.childArray = [];
}
}
/**
* Find a random Child Node
* @return {Node} child node
*/
getRandomChildNode() {
return this.childArray[Math.floor(Math.random() * this.childArray.length)];
}
getChildWithMaxScore() {
let arrScore = [];
for (var i = 0; i < this.childArray.length; i++) {
arrScore.push(this.childArray[i].state.visitCount);
}
var largest = Math.max(...arrScore);
var idx = arrScore.indexOf(largest);
return this.childArray[idx];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment