A java PacMan game application for A.U.TH (data structures class)
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

126 lignes
4.2 KiB

  1. /**
  2. * @file MinMaxTree.java
  3. * @brief
  4. * File containing the Pacman Min-Max Tree class. A collection
  5. * of static routines to build a search tree and execute
  6. * a alpha-beta pruning algorithm to it
  7. *
  8. * @author Christos Choutouridis 8997 cchoutou@ece.auth.gr
  9. * @author Konstantina Tsechelidou 8445 konstsec@ece.auth.gr
  10. */
  11. package gr.auth.ee.dsproject.node;
  12. /**
  13. * @brief
  14. * A tree-like data structure containing move nodes to apply the min-max
  15. * algorithm.
  16. * @note
  17. * This is NOT a real tree. We do not insert node based on a key etc...
  18. */
  19. public class Tree
  20. {
  21. /*
  22. * ============ Constructor ==============
  23. */
  24. /**
  25. * @brief
  26. * The simple constructor. Do nothing (this is a static class)
  27. */
  28. public Tree () { }
  29. /*
  30. * ========== private helpers ==============
  31. */
  32. /** @return the minimum of x and y */
  33. private static double min (double x, double y) {
  34. return (x < y) ? x : y;
  35. }
  36. /** return the maximum of x and y */
  37. private static double max (double x, double y) {
  38. return (x > y) ? x : y;
  39. }
  40. /*
  41. * ========== Tree-like public methods =============
  42. */
  43. /**
  44. * @brief
  45. * Insert a node to a parent directly.
  46. * This is NOT a real tree. We do not insert node based on a key
  47. * @param node Node to insert
  48. * @param parent The parent in witch to insert
  49. * @return Reference to inserted node
  50. * If insert fail, returns null
  51. */
  52. public static Node89978445 grow (Node89978445 node, Node89978445 parent) {
  53. if (parent.children.add (node)) {
  54. node.parent = parent;
  55. node.depth = parent.depth + 1;
  56. return node;
  57. }
  58. else
  59. return null;
  60. }
  61. /**
  62. * @brief
  63. * A recursive implementation of an alpha-beta pruning algorithm.
  64. * Alpha–beta pruning is a search algorithm that seeks to decrease
  65. * the number of nodes that are evaluated by the minimax algorithm in its search tree.
  66. * It stops completely evaluating a move when at least one possibility has
  67. * been found that proves the move to be worse than a previously examined move.
  68. * Such moves need not be evaluated further.
  69. * @param node current node of the tree. In the initial call is the root node
  70. * @param depth the maximum depth to investigate
  71. * @param a alpha (start -infinity)
  72. * @param b beta (start +infinity)
  73. * @param maxingPlayer helper variable to switch between min-max behavior
  74. * @return
  75. * reference to node with the maximum evaluation resulting path.
  76. */
  77. public static Node89978445 minmaxAB (Node89978445 node, int depth, double a, double b, boolean maxingPlayer)
  78. {
  79. double v, ev;
  80. Node89978445 vNode = null;
  81. if ((depth == 0) || (node.children.isEmpty())) // Escape
  82. return node;
  83. if (maxingPlayer) {
  84. // maximizing player part
  85. v = Integer.MIN_VALUE; // start small for max algorithm to work
  86. // recursively call alpha-beta for every child, reducing depth and toggling between min and max
  87. for (int i=0 ; i<node.children.size() ; ++i) {
  88. ev = minmaxAB (node.children.get (i), depth-1, a, b, false).getEvaluation();
  89. // select the maximum output node
  90. if (ev > v) {
  91. v = ev;
  92. vNode = node.children.get (i);
  93. }
  94. a = max (v, a); // mark alpha
  95. if (b <= a) // cut-off alpha
  96. break;
  97. }
  98. return vNode;
  99. }
  100. else {
  101. // minimizing player part
  102. v = Integer.MAX_VALUE; // start big for min algorithm to work
  103. // recursively call alpha-beta for every child, reducing depth and toggling between min and max
  104. for (int i=0 ; i<node.children.size() ; ++i) {
  105. ev = minmaxAB(node.children.get (i), depth-1, a, b, true).getEvaluation();
  106. if (ev < v) {
  107. v = ev;
  108. vNode = node.children.get (i);
  109. }
  110. b = min (v, b); // mark beta
  111. if (b <= a) // cut-off beta
  112. break;
  113. }
  114. return vNode;
  115. }
  116. }
  117. }