diff --git a/bin/gr/auth/ee/dsproject/pacman/Creature.class b/bin/gr/auth/ee/dsproject/pacman/Creature.class index 11597e0..e53c04b 100644 Binary files a/bin/gr/auth/ee/dsproject/pacman/Creature.class and b/bin/gr/auth/ee/dsproject/pacman/Creature.class differ diff --git a/src/gr/auth/ee/dsproject/node/Globals.java b/src/gr/auth/ee/dsproject/node/Globals.java index a236c93..5cfa326 100755 --- a/src/gr/auth/ee/dsproject/node/Globals.java +++ b/src/gr/auth/ee/dsproject/node/Globals.java @@ -53,7 +53,7 @@ public class Globals { /* * Tree settings */ - public static final int MINMAXTREE_MAX_DEPTH = 2; + public static final int MINMAXTREE_MAX_DEPTH = 2; // MUST be multiple of 2 /* * In order to find out when a ghost is inside a cavity we manualy diff --git a/src/gr/auth/ee/dsproject/node/MinMaxTree.java b/src/gr/auth/ee/dsproject/node/MinMaxTree.java index bc5d52b..8956feb 100755 --- a/src/gr/auth/ee/dsproject/node/MinMaxTree.java +++ b/src/gr/auth/ee/dsproject/node/MinMaxTree.java @@ -58,7 +58,7 @@ public class MinMaxTree * @return Reference to inserted node * If insert fail, returns null */ - public Node89978445 insert (Node89978445 node, Node89978445 parent) { + public static Node89978445 insert (Node89978445 node, Node89978445 parent) { if (parent.children.add (node)) { node.parent = parent; return node; @@ -75,7 +75,7 @@ public class MinMaxTree * @return Reference to the child with the minimum evaluation value * If parent has no children null. */ - public Node89978445 minChild (Node89978445 parent) + public static Node89978445 minChild (Node89978445 parent) { Node89978445 node = null; double ev, min = Globals.EVAL_MAX+1; @@ -97,7 +97,7 @@ public class MinMaxTree * @return Reference to the child with the maximum evaluation value * If parent has no children null. */ - public Node89978445 maxChild (Node89978445 parent) + public static Node89978445 maxChild (Node89978445 parent) { Node89978445 node = null; double ev, max = Globals.EVAL_MIN-1; diff --git a/src/gr/auth/ee/dsproject/node/Node89978445.java b/src/gr/auth/ee/dsproject/node/Node89978445.java index 4288458..3579b83 100755 --- a/src/gr/auth/ee/dsproject/node/Node89978445.java +++ b/src/gr/auth/ee/dsproject/node/Node89978445.java @@ -29,7 +29,7 @@ public class Node89978445 * Pacman's current move evaluation * This is used also as the "return status" of the object */ - int[] nodeXY; // Pacman's current x,y coordinate + int[] nodeXY; // Pacman's current x,y coordinate int[][] currentGhostPos; // Array holding the Ghost (x,y) pairs @@ -68,7 +68,7 @@ public class Node89978445 currentGhostPos = new int [PacmanUtilities.numberOfGhosts][2]; flagPos = new int [PacmanUtilities.numberOfFlags][2]; currentFlagStatus = new boolean[PacmanUtilities.numberOfFlags]; - ArrayList children = new ArrayList (); + children = new ArrayList (); } /** @@ -88,7 +88,7 @@ public class Node89978445 currentGhostPos = new int [PacmanUtilities.numberOfGhosts][2]; flagPos = new int [PacmanUtilities.numberOfFlags][2]; currentFlagStatus = new boolean[PacmanUtilities.numberOfFlags]; - ArrayList children = new ArrayList (); + children = new ArrayList (); } /* @@ -134,6 +134,13 @@ public class Node89978445 return nodeEvaluation; } + public int[] getCurentPacmanPos () { + return nodeXY; + } + public int[][] getCurrentGhostPos () { + return currentGhostPos; + } + /* * ============= Helper API ============ */ diff --git a/src/gr/auth/ee/dsproject/pacman/Creature.java b/src/gr/auth/ee/dsproject/pacman/Creature.java index 670d9fc..780da22 100755 --- a/src/gr/auth/ee/dsproject/pacman/Creature.java +++ b/src/gr/auth/ee/dsproject/pacman/Creature.java @@ -47,58 +47,78 @@ public class Creature implements gr.auth.ee.dsproject.pacman.AbstractCreature /** * @brief * Create node for each one of the possible moves (0, 1, 2, 3) and - * evaluate these moves. After that feeds the valid ones to an ArrayList - * and select the best of them to return + * evaluate these moves. */ -// public int calculateNextPacmanPosition (Room[][] Maze, int[] currPosition) -// { -// Node89978445 node = new Node89978445 (); // make a dummy node -// MinMaxTree rt = new MinMaxTree (node); /*< -// * Plant a tree with dummy root -// * Null parent and no evaluation -// */ -// -// createSubTreePacman (0,node, Maze, currPosition); -// return 0; -// } - public int calculateNextPacmanPosition (Room[][] Maze, int[] currPosition) - { - Node89978445 cur = new Node89978445 (Maze, currPosition); - Node89978445 moveNode; - //ArrayList moveNodes = new ArrayList (); - double ev, max = Globals.NO_EVAL; - int decision = -1; - - int[] nextPosition = {-1, -1}; - - /* - * loop the possible moves and fill them to list - * mark the move with the maximum evaluation value - */ - for (int i=0 ; i<4 ; ++i) { - if ((nextPosition = Node89978445.pacmanValidMove(cur, i)) != Globals.FALSE_POS) { - moveNode = new Node89978445 (Maze, nextPosition); - ev = moveNode.getEvaluation(); - if (ev > max) { - max = ev; - decision = i; - } - } - } - return decision; - } - - void createSubTreePacman (int depth, Node89978445 parent, Room[][] Maze, int[] currPacmanPosition) - { - - // TODO Fill This - - } + public int calculateNextPacmanPosition (Room[][] Maze, int[] currPosition) + { + Node89978445 cur = new Node89978445 (Maze, currPosition); + MinMaxTree mmTree = new MinMaxTree (cur); /*< + * Plant a tree with dummy root + * Null parent and no evaluation + */ + + createSubTreePacman (0, cur, Maze, currPosition); //make the tree + // min-max + return 0; + } + + void createSubTreePacman (int depth, Node89978445 parent, Room[][] Maze, int[] curPacmanPosition) + { + if (++depth > Globals.MINMAXTREE_MAX_DEPTH) + return; + else { + Node89978445 newNode; + Room[][] newMaze; + int[] nextPosition = {-1, -1}; + + // scan possible valid moves + for (int move=0 ; move<4 ; ++move) { + if ((nextPosition = Node89978445.pacmanValidMove (parent, move)) != Globals.FALSE_POS) { + // Make a copy of the maze in order to simulate next move and move Pacman + newMaze = PacmanUtilities.copy (Maze); + PacmanUtilities.movePacman (newMaze, parent.getCurentPacmanPos(), nextPosition); + + // Create a node for the move in the new stated game maze + newNode = new Node89978445 (newMaze, nextPosition); + newNode.getEvaluation(); + MinMaxTree.insert (newNode, parent); // add node to the min-max tree + + // call the recursive ranch creator + createSubTreeGhosts (depth, newNode, newMaze, newNode.getCurrentGhostPos()); + } + } + } + } - void createSubTreeGhosts (int depth, Node89978445 parent, Room[][] Maze, int[][] currGhostsPosition) - { - // TODO Fill This - } + void createSubTreeGhosts (int depth, Node89978445 parent, Room[][] Maze, int[][] currGhostsPosition) + { + if (++depth > Globals.MINMAXTREE_MAX_DEPTH) + return; + else { + Node89978445 newNode; + Room[][] newMaze; + ArrayList ghostMoves; + + // Get all possible ghost moves + ghostMoves = PacmanUtilities.allGhostMoves(Maze, currGhostsPosition); + + // loop all ghost moves + int s = ghostMoves.size(); + for (int i=0 ; i