A java PacMan game application for A.U.TH (data structures class)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.7 KiB

  1. /**
  2. * @file MinMaxTree.java
  3. * @brief
  4. * File containing the Pacman Min-Max Tree class.
  5. *
  6. * @author Christos Choutouridis 8997 cchoutou@ece.auth.gr
  7. * @author Konstantina Tsechelidou 8445 konstsec@ece.auth.gr
  8. */
  9. package gr.auth.ee.dsproject.node;
  10. /**
  11. * @brief
  12. * A tree-like data structure containing move nodes to apply the min-max
  13. * algorithm.
  14. * @note
  15. * This is NOT a real tree. We do not insert node based on a key etc...
  16. */
  17. public class Tree
  18. {
  19. /*
  20. * ============ Constructor ==============
  21. */
  22. /**
  23. * @brief
  24. * The simple constructor. Do nothing (this is a static class)
  25. */
  26. public Tree () { }
  27. /*
  28. * ========== private helpers ==============
  29. */
  30. private static double min (double x, double y) {
  31. return (x < y) ? x : y;
  32. }
  33. private static double max (double x, double y) {
  34. return (x > y) ? x : y;
  35. }
  36. /*
  37. * ========== Tree-like public methods =============
  38. */
  39. /**
  40. * @brief
  41. * Insert a node to a parent directly.
  42. * This is NOT a real tree. We do not insert node based on a key
  43. * @param node Node to insert
  44. * @param parent The parent in witch to insert
  45. * @return Reference to inserted node
  46. * If insert fail, returns null
  47. */
  48. public static Node89978445 grow (Node89978445 node, Node89978445 parent) {
  49. if (parent.children.add (node)) {
  50. node.parent = parent;
  51. node.depth = parent.depth + 1;
  52. return node;
  53. }
  54. else
  55. return null;
  56. }
  57. public static Node89978445 minmaxAB (Node89978445 node, int depth, double a, double b, boolean maxingPlayer)
  58. {
  59. double v, ev;
  60. Node89978445 vNode = null;
  61. if ((depth == 0) || (node.children.isEmpty()))
  62. return node;
  63. if (maxingPlayer) {
  64. v = Globals.EVAL_MIN - 1;
  65. for (int i=0 ; i<node.children.size() ; ++i) {
  66. ev = minmaxAB (node.children.get (i), depth-1, a, b, false).getEvaluation();
  67. if (ev > v) {
  68. v = ev;
  69. vNode = node.children.get (i);
  70. }
  71. a = max (v, a);
  72. if (b <= a)
  73. break;
  74. }
  75. return vNode;
  76. }
  77. else {
  78. v = Globals.EVAL_MAX + 1;
  79. for (int i=0 ; i<node.children.size() ; ++i) {
  80. ev = minmaxAB(node.children.get (i), depth-1, a, b, true).getEvaluation();
  81. if (ev < v) {
  82. v = ev;
  83. vNode = node.children.get (i);
  84. }
  85. b = min (v, b);
  86. if (b <= a)
  87. break;
  88. }
  89. return vNode;
  90. }
  91. }
  92. }