Last active
January 16, 2018 05:24
-
-
Save pengcheng95/4f461d899d7dc3587b543c025389a5e7 to your computer and use it in GitHub Desktop.
MCTS Tic Tac Toe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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