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.

116 lines
3.1 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 MinMaxTree
  18. {
  19. private Node89978445 root;
  20. /*
  21. * ============ Constructors ==============
  22. */
  23. /**
  24. * @brief
  25. * The simple constructor. Just initialize the root to null
  26. */
  27. public MinMaxTree () {
  28. root = null; // No root yet
  29. }
  30. /**
  31. * @brief
  32. * A constructor with root
  33. */
  34. public MinMaxTree (Node89978445 root) {
  35. setRoot (root);
  36. }
  37. /*
  38. * ========= setter ============
  39. */
  40. /** Set root */
  41. public Node89978445 setRoot (Node89978445 root) {
  42. return this.root = root;
  43. }
  44. /*
  45. * ========== Tree-like methods =============
  46. */
  47. /**
  48. * @brief
  49. * Insert a node to a parent directly.
  50. * This is NOT a real tree. We do not insert node based on a key
  51. * @param node Node to insert
  52. * @param parent The parent in witch to insert
  53. * @return Reference to inserted node
  54. * If insert fail, returns null
  55. */
  56. public static Node89978445 insert (Node89978445 node, Node89978445 parent) {
  57. if (parent.children.add (node)) {
  58. node.parent = parent;
  59. return node;
  60. }
  61. else
  62. return null;
  63. }
  64. /**
  65. * @brief
  66. * Find the child with the minimum evaluation value of a
  67. * specific parent and return a reference to it
  68. * @param parent The parent of children we scan
  69. * @return Reference to the child with the minimum evaluation value
  70. * If parent has no children null.
  71. */
  72. public static Node89978445 minChild (Node89978445 parent)
  73. {
  74. Node89978445 node = null;
  75. double ev, min = Globals.EVAL_MAX+1;
  76. for (int i=0 ; i<parent.children.size() ; ++i) {
  77. if ((ev = parent.children.get(i).getEvaluation()) < min) {
  78. min = ev;
  79. node = parent.children.get (i);
  80. }
  81. }
  82. return node;
  83. }
  84. /**
  85. * @brief
  86. * Find the child with the maximum evaluation value of a
  87. * specific parent and return a reference to it
  88. * @param parent The parent of children we scan
  89. * @return Reference to the child with the maximum evaluation value
  90. * If parent has no children null.
  91. */
  92. public static Node89978445 maxChild (Node89978445 parent)
  93. {
  94. Node89978445 node = null;
  95. double ev, max = Globals.EVAL_MIN-1;
  96. for (int i=0 ; i<parent.children.size() ; ++i) {
  97. if ((ev = parent.children.get(i).getEvaluation()) > max) {
  98. max = ev;
  99. node = parent.children.get (i);
  100. }
  101. }
  102. return node;
  103. }
  104. }