private void solveTrie(Node node) { boolean isEvenNode = (node.depth % 2 == 0); if (node.children.isEmpty()) { if (node.depth >= 4) // only count 4+ length strings as goals { node.compGoal = !isEvenNode; } if (isEvenNode) { node.depthOfUserGoal = node.depth; } else { node.depthOfCompGoal = node.depth; } return; } // node.compGoal = isEvenNode; // non-leaf initialization (done in constructor instead) for (Node n : node.children.values()) { solveTrie(n); if (isEvenNode) { // it is the user's turn if (!n.compGoal) { // if there is a user goal to be chosen, this node is a user goal as well // if no user goals can be chosen, this node is a computer goal node.compGoal = false; node.depthOfUserGoal = (node.depthOfUserGoal > n.depthOfUserGoal) ? node.depthOfUserGoal : n.depthOfUserGoal; node.userGoals.add(n); } else { node.compGoals.add(n); // if the user cannot choose a user goal, he chooses the (computer-winning) path(s) that // forces the maximal game length // node.maxLosingPaths holds these path(s) and is updated here updateMaxLosingPaths(node, n, "compGoals"); } } else { // it is the computer's turn if (n.compGoal) { // if there is a computer goal to be chosen, this node is a computer goal as well // if no computer goals can be chosen, this node is a user goal node.compGoal = true; node.depthOfCompGoal = (node.depthOfCompGoal > n.depthOfCompGoal) ? node.depthOfCompGoal : n.depthOfCompGoal; node.compGoals.add(n); } else { node.userGoals.add(n); // if the computer cannot choose a computer goal, he chooses the (user-winning) path(s) // that forces the maximal game length // node.maxLosingPaths holds these path(s) and is updated here updateMaxLosingPaths(node, n, "userGoals"); } } } }
private void updateDepthOfGoal(Node node, Node n, String type) { if (type.equals("compGoals")) { node.depthOfCompGoal = n.depthOfCompGoal; } else { node.depthOfUserGoal = n.depthOfUserGoal; } }