|
- /**
- * @file MinMaxTree.java
- * @brief
- * File containing the Pacman Min-Max Tree class.
- *
- * @author Christos Choutouridis 8997 cchoutou@ece.auth.gr
- * @author Konstantina Tsechelidou 8445 konstsec@ece.auth.gr
- */
- package gr.auth.ee.dsproject.node;
-
-
- /**
- * @brief
- * A tree-like data structure containing move nodes to apply the min-max
- * algorithm.
- * @note
- * This is NOT a real tree. We do not insert node based on a key etc...
- */
- public class Tree
- {
- /*
- * ============ Constructor ==============
- */
- /**
- * @brief
- * The simple constructor. Do nothing (this is a static class)
- */
- public Tree () { }
-
- /*
- * ========== private helpers ==============
- */
- private static double min (double x, double y) {
- return (x < y) ? x : y;
- }
- private static double max (double x, double y) {
- return (x > y) ? x : y;
- }
-
- /*
- * ========== Tree-like public methods =============
- */
- /**
- * @brief
- * Insert a node to a parent directly.
- * This is NOT a real tree. We do not insert node based on a key
- * @param node Node to insert
- * @param parent The parent in witch to insert
- * @return Reference to inserted node
- * If insert fail, returns null
- */
- public static Node89978445 grow (Node89978445 node, Node89978445 parent) {
- if (parent.children.add (node)) {
- node.parent = parent;
- node.depth = parent.depth + 1;
- return node;
- }
- else
- return null;
- }
-
- public static Node89978445 minmaxAB (Node89978445 node, int depth, double a, double b, boolean maxingPlayer)
- {
- double v, ev;
- Node89978445 vNode = null;
-
- if ((depth == 0) || (node.children.isEmpty()))
- return node;
-
- if (maxingPlayer) {
- v = Globals.EVAL_MIN - 1;
- for (int i=0 ; i<node.children.size() ; ++i) {
- ev = minmaxAB (node.children.get (i), depth-1, a, b, false).getEvaluation();
- if (ev > v) {
- v = ev;
- vNode = node.children.get (i);
- }
- a = max (v, a);
- if (b <= a)
- break;
- }
- return vNode;
- }
- else {
- v = Globals.EVAL_MAX + 1;
- for (int i=0 ; i<node.children.size() ; ++i) {
- ev = minmaxAB(node.children.get (i), depth-1, a, b, true).getEvaluation();
- if (ev < v) {
- v = ev;
- vNode = node.children.get (i);
- }
- b = min (v, b);
- if (b <= a)
- break;
- }
- return vNode;
- }
- }
-
- }
|