Ejemplo n.º 1
0
  public MiniMaxNode MaxValue(MiniMaxNode node, double alpha, double beta) {
    SetVisited(node);

    if (TerminalTest(node)) return node;

    MiniMaxNode bestNode = new MiniMaxNode("", Double.NEGATIVE_INFINITY);
    for (MiniMaxNode child : node.GetChildren()) {
      MiniMaxNode minNode = MinValue(child, alpha, beta);

      if (minNode.GetValue() > bestNode.GetValue()) bestNode = minNode;

      if (bestNode.GetValue() >= beta) return bestNode;

      alpha = Math.max(alpha, bestNode.GetValue());
    }

    MiniMaxNode returnNode = bestNode;

    // Since MiniMax always starts with max, only need to do this in MaxValue
    if (returnNode.GetParent() != head) returnNode = returnNode.GetParent();

    return returnNode;
  }
Ejemplo n.º 2
0
  public MiniMaxNode MinValue(MiniMaxNode node, double alpha, double beta) {
    SetVisited(node);

    if (TerminalTest(node)) return node;

    MiniMaxNode bestNode = new MiniMaxNode("", Double.POSITIVE_INFINITY);
    for (MiniMaxNode child : node.GetChildren()) {
      MiniMaxNode maxNode = MaxValue(child, alpha, beta);

      if (maxNode.GetValue() < bestNode.GetValue()) bestNode = maxNode;

      if (bestNode.GetValue() <= alpha) return bestNode;

      beta = Math.min(beta, bestNode.GetValue());
    }

    // Return parent because we need to know the next move to make, not what the ultimate best state
    // is
    return bestNode.GetParent();
  }