From f38254d1ee45454a2335c3f2ab61b7d85e2807d7 Mon Sep 17 00:00:00 2001 From: Christos Houtouridis Date: Mon, 15 Jan 2018 19:23:44 +0200 Subject: [PATCH] DEV: Not working, work in progres --- GameLog.txt | 49 ++++++ bin/.gitignore | 0 .../auth/ee/dsproject/pacman/Creature.class | Bin 5520 -> 5270 bytes src/gr/auth/ee/dsproject/node/Globals.java | 4 + src/gr/auth/ee/dsproject/node/MinMaxTree.java | 115 ++++++++++++++ .../auth/ee/dsproject/node/Node89978445.java | 141 ++++++++++-------- src/gr/auth/ee/dsproject/node/Queue2D.java | 89 ++++++++--- src/gr/auth/ee/dsproject/pacman/Creature.java | 53 ++++--- 8 files changed, 344 insertions(+), 107 deletions(-) mode change 100755 => 100644 bin/.gitignore mode change 100755 => 100644 bin/gr/auth/ee/dsproject/pacman/Creature.class create mode 100755 src/gr/auth/ee/dsproject/node/MinMaxTree.java diff --git a/GameLog.txt b/GameLog.txt index 9eb9e46..1ce915b 100755 --- a/GameLog.txt +++ b/GameLog.txt @@ -31,3 +31,52 @@ Team 0.00 Mine 1 Team 0.00 Mine 0 75 Team 0.00 Mine 1 Team 0.00 Mine 0 75 Team 0.00 Mine 1 Team 0.00 Mine 0 75 Team 0.00 Mine 1 Team 0.00 Mine 0 75 +Team 0.00 Mine 1 Team 0.00 Mine 0 78 +Team 0.00 Mine 1 Team 0.00 Mine 0 78 +Team 2 Mine 1 Team 1 Mine 0 78 +Team 0.00 Mine 1 Team 0.00 Mine 0 76 +Team 0.00 Mine 1 Team 0.00 Mine 0 76 +Team 0.00 Mine 1 Team 0.00 Mine 0 98 +Team 0.00 Mine 1 Team 0.00 Mine 0 98 +Team 0.00 Mine 1 Team 0.00 Mine 0 97 +Team 0.00 Mine 1 Team 0.00 Mine 0 54 +Team 0.00 Mine 1 Team 0.00 Mine 0 67 +Team 0.00 Mine 1 Team 0.00 Mine 0 67 +Team 0.00 Mine 1 Team 0.00 Mine 0 85 +Team 0.00 Mine 1 Team 0.00 Mine 0 85 +Team 0.00 Mine 1 Team 0.00 Mine 0 80 +Team 0.00 Mine 1 Team 0.00 Mine 0 80 +Team 0.00 Mine 1 Team 0.00 Mine 0 70 +Team 0.00 Mine 1 Team 0.00 Mine 0 70 +Team 0.00 Mine 1 Team 0.00 Mine 0 70 +Team 0.00 Mine 1 Team 0.00 Mine 0 94 +Team 0.00 Mine 1 Team 0.00 Mine 0 94 +Team 0.00 Mine 1 Team 0.00 Mine 0 85 +Team 0.00 Mine 1 Team 0.00 Mine 0 85 +Team 0.00 Mine 1 Team 0.00 Mine 0 82 +Team 0.00 Mine 1 Team 0.00 Mine 0 82 +Team 0.00 Mine 1 Team 0.00 Mine 0 64 +Team 0.00 Mine 1 Team 0.00 Mine 0 64 +Team 0.00 Mine 1 Team 0.00 Mine 0 129 +Team 0.00 Mine 1 Team 0.00 Mine 0 129 +Team 0.00 Mine 1 Team 0.00 Mine 0 127 +Team 0.00 Mine 1 Team 0.00 Mine 0 127 +Team 0.00 Mine 0 Team 0.00 Mine 1 79 +Team 0.00 Mine 0 Team 0.00 Mine 1 79 +Team 0.00 Mine 0 Team 0.00 Mine 1 27 +Team 0.00 Mine 0 Team 0.00 Mine 1 27 +Team 0.00 Mine 1 Team 0.00 Mine 0 106 +Team 0.00 Mine 1 Team 0.00 Mine 0 106 +Team 0.00 Mine 1 Team 0.00 Mine 0 74 +Team 0.00 Mine 1 Team 0.00 Mine 0 74 +Team 0.00 Mine 1 Team 0.00 Mine 0 62 +Team 0.00 Mine 1 Team 0.00 Mine 0 62 +Team 0.00 Mine 1 Team 0.00 Mine 0 159 +Team 0.00 Mine 1 Team 0.00 Mine 0 159 +Team 0.00 Mine 1 Team 0.00 Mine 0 65 +Team 0.00 Mine 1 Team 0.00 Mine 0 59 +Team 0.00 Mine 1 Team 0.00 Mine 0 59 +Team 0.00 Mine 1 Team 0.00 Mine 0 77 +Team 0.00 Mine 1 Team 0.00 Mine 0 77 +Team 0.00 Mine 1 Team 0.00 Mine 0 94 +Team 0.00 Mine 1 Team 0.00 Mine 0 94 diff --git a/bin/.gitignore b/bin/.gitignore old mode 100755 new mode 100644 diff --git a/bin/gr/auth/ee/dsproject/pacman/Creature.class b/bin/gr/auth/ee/dsproject/pacman/Creature.class old mode 100755 new mode 100644 index a461bdbd4b679febd9e83aa90f4ce20ddc4f917e..11597e03b51ed15bf786ad7001f383e0f20a6642 GIT binary patch delta 2095 zcmah~X;4*V7=GTf+{0avB`%8sBKzfXO;L%(1w=p+S%R>uCS4E>xd;iEmUgqzCTEx(@y z@$#zx<|1N15oDWhXsOQ@ZV}bM4D&@;M)fjWL*Lpivs_Kh^K9(eGT78N&^`IObkjx4V5qZad8ofN)DiA7P;6k<*l%jH zIGS*ofo8K(&rw^@YIf`G>Ge)DVU-7~t-SPWL*ej%3u|zdfp&r4;wPcWuJrQ3?l6ON zv4#*j%tXgBUAHnZ*Er_MdUM3_+SEZ0mZQN1!DbIu;0g1aR>8mW&aRG=BPxQtIN{Is&323&p;!9qllkm*>0MKpFHlPeWm zbEA?jGoYShR&E1gV6lNJgG(%fPgM*#Tj9eJRGZJZN_;i6;u?Ro3~B9%otkQ+rSz^5 z)!|aUogrQ3F;~+6*epXmZNxfQGtQkSjb^F4D5sli8<=wsO9^8ddYS!3WTTHq+ruau z7=6GTb|+O%5~JY~Ty7KNpvR33RY+K4o}%6RB!;j~IR16>l8Q}iyDh|H62UusY( zbEw6}s7&|^y;_kvv&gz>GmD@VE>V;R(43Ps%PlB_nuR_G3&A<9Qj!L3tl9v9y;J z4-P5uxKEje?MgNdo8M1Kikw%El1C-gTgOJUmUQvhh`MZ&ytG<}^;A!q;=m1LXDD4Wb&BupU7K(~RQWO$7k>FMdy&LB!`P;eU6tX zl-zqr;XM-{Ade&jaFpHfG4=W>N5p5O?hEGdCGEb#CVY({d`q%_z?~%OE}S5hAK@|l zjHmGn={$)S@hj>44deKo9#7(Pl4*5NKO+Ih|LdUR5xY1`yT_?0tA#AoX_KHccrTGg zBBt5}>rCzd6*r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$fagSwU+L^|3M_AYt`PMQRQ=$L;ev|?HxNpRUcL3u5Qhf%s@jT*BWL9 z%wtyb;p0~l&P5vPkd1njp#hcruESz9;zF!K6RyBTtadSav6Q;NS{PinAsH#$9m&xb zSE{GILQg>~#CcHBiljKsrz?%&25|u;FN5}D5mz7GMd|oL!WAH3O*}DZST-SDM3*KV zk83TVRx?j_z&pl#4IQFmsg4$&sT8xA!5A{mxX{@Ms4Gt0DQDcBVU*xl`A@r3PkZ|f z3mRXHOY9MU=E8OtlDVu7%ybeG!7|q3KqB}9flyq@fk}0xO9_)ksTG$wUhIB%& zobYXD!8^#m>#-U;8SW;oyKpyd!Na%}kK%Uh!yPz?JMlVCKf-SO4K}{Qz4#XQ;UxBm z7Y|AS{wS4rNE-1cS&Y519FIsR9+kCtO#0$@T&}~g+<+(K9{gGM;wgCp&&a!YR^G>R ztoM1v!~rE4dz2X%RC4f&i)9B9fRaCnC8dc6K&gjmv)hyw6IFJbQm>0;kbXAc8ZsqA zF|d((d0u2HnhUxvs-(N1`&`iT5Ewy3o1K;) X)J?_w89vGT35^qD max) { + max = ev; + node = parent.children.get (i); + } + } + return node; + } + + +} diff --git a/src/gr/auth/ee/dsproject/node/Node89978445.java b/src/gr/auth/ee/dsproject/node/Node89978445.java index 9821ed7..4288458 100755 --- a/src/gr/auth/ee/dsproject/node/Node89978445.java +++ b/src/gr/auth/ee/dsproject/node/Node89978445.java @@ -10,6 +10,8 @@ package gr.auth.ee.dsproject.node; +import java.util.ArrayList; + //import java.awt.image.PackedColorModel; import gr.auth.ee.dsproject.pacman.PacmanUtilities; import gr.auth.ee.dsproject.pacman.Room; @@ -27,9 +29,7 @@ public class Node89978445 * Pacman's current move evaluation * This is used also as the "return status" of the object */ - int nodeMove; // Pacman's current move int[] nodeXY; // Pacman's current x,y coordinate - int[] newXY; // Pacman's new x,y coordinate int[][] currentGhostPos; // Array holding the Ghost (x,y) pairs @@ -38,6 +38,13 @@ public class Node89978445 boolean[] currentFlagStatus; // Array holding the flag capture status Room[][] Maze; // copy of the current Maze + /* + * Tree navigation references. These variables are handled by + * PacTree + */ + Node89978445 parent; + ArrayList children; + int depth; /* * ============ Constructors ============== @@ -52,15 +59,16 @@ public class Node89978445 public Node89978445 () { // Fill members - nodeXY = newXY = Globals.FALSE_POS; - nodeMove = Globals.INVALID_MOVE; + this.Maze = null; + nodeXY = Globals.FALSE_POS; nodeEvaluation = Globals.NO_EVAL; + parent = null; // allocate objects currentGhostPos = new int [PacmanUtilities.numberOfGhosts][2]; flagPos = new int [PacmanUtilities.numberOfFlags][2]; currentFlagStatus = new boolean[PacmanUtilities.numberOfFlags]; - + ArrayList children = new ArrayList (); } /** @@ -68,23 +76,19 @@ public class Node89978445 * Constructor for the Node89978445 * @param Maze The current maze object * @param curXY The current pacman's (x, y) position - * @param move The move under inspection */ - public Node89978445 (Room[][] Maze, int[] curXY, int move) + public Node89978445 (Room[][] Maze, int[] curXY) { this.Maze = Maze; // Fill members - nodeXY = newXY = curXY; - nodeMove = move; + nodeXY = curXY; nodeEvaluation = Globals.NO_EVAL; - - //calculate members - newXY = pacmanValidMove (nodeXY, move); + parent = null; // allocate objects currentGhostPos = new int [PacmanUtilities.numberOfGhosts][2]; flagPos = new int [PacmanUtilities.numberOfFlags][2]; currentFlagStatus = new boolean[PacmanUtilities.numberOfFlags]; - + ArrayList children = new ArrayList (); } /* @@ -104,18 +108,6 @@ public class Node89978445 */ public void setPosition (int[] curXY) { nodeXY = curXY; - //update members - newXY = pacmanValidMove (curXY, nodeMove); - } - - /** - * @brief Set the move to Node object - * @param move The move under inspection - */ - public void setMove (int move) { - nodeMove = move; - //update members - newXY = pacmanValidMove (nodeXY, move); } /* @@ -124,34 +116,72 @@ public class Node89978445 /** * @brief If not done runs the evaluation algorithm and returns the result + * @note We assume the current position was a valid position * @return The evaluation result */ public double getEvaluation () { - // calculate helper arrays - currentGhostPos = findGhosts (); - flagPos = findFlags (); - currentFlagStatus = checkFlags (); - - // validation and evaluate move - if (newXY != Globals.FALSE_POS) { - // If already evaluated do not re-evaluate the move - if (nodeEvaluation == Globals.NO_EVAL) - nodeEvaluation = evaluate (); - } - else { - nodeEvaluation = Globals.NO_EVAL; - } - return nodeEvaluation; - } + // If already evaluated do not re-evaluate the move + if (nodeEvaluation == Globals.NO_EVAL) { + // calculate helper arrays + currentGhostPos = findGhosts (); + flagPos = findFlags (); + currentFlagStatus = checkFlags (); + + return nodeEvaluation = evaluate (); + } + else + return nodeEvaluation; + } + /* + * ============= Helper API ============ + */ /** + * @param creature creature's (x,y) * @return - * The current move of the Node89978445 + * Return true if the creature is inside the maze boxes */ - public int getMove () { - return nodeMove; + public static boolean isInsideBox (int[] creature) + { + boolean ret = false; //have faith + for (int i=0 ; i<4 ; ++i) { + if ((creature[0]>=Globals.BOXES[i][0][0] && creature[0]<=Globals.BOXES[i][1][0]) && + (creature[1]>=Globals.BOXES[i][0][1] && creature[1]<=Globals.BOXES[i][1][1])) + ret = true; + } + return ret; } + /** + * @brief + * Static version of move validation + * Check if the requested move for a node is valid + */ + public static int[] pacmanValidMove (Node89978445 node, int move) + { + int[] newPos = new int[2]; + + // find hypothetical new position + newPos[0] = node.nodeXY[0]; + newPos[1] = node.nodeXY[1]; + newPos[0] += (move == Room.SOUTH) ? 1:0; + newPos[0] -= (move == Room.NORTH) ? 1:0; + newPos[1] += (move == Room.EAST) ? 1:0; + newPos[1] -= (move == Room.WEST) ? 1:0; + + // Pacman curves Maze plane to a Torus + if (newPos[0] < 0) newPos[0] = PacmanUtilities.numberOfRows; + if (newPos[0] >= PacmanUtilities.numberOfRows ) newPos[0] = 0; + if (newPos[1] < 0) newPos[1] = PacmanUtilities.numberOfColumns; + if (newPos[1] >= PacmanUtilities.numberOfColumns ) newPos[1] = 0; + + // Valid filters + if (!isInsideBox(node.nodeXY) && + (node.Maze[node.nodeXY[0]][node.nodeXY[1]].walls[move] == 0)) + return Globals.FALSE_POS; + return newPos; + } + /* * ============= Node's private methods ============= @@ -239,23 +269,6 @@ public class Node89978445 * ============ evaluation helper methods ============== */ - - /** - * @param creature creature's (x,y) - * @return - * Return true if the creature is inside the maze boxes - */ - private boolean isInsideBox (int[] creature) - { - boolean ret = false; //have faith - for (int i=0 ; i<4 ; ++i) { - if ((creature[0]>=Globals.BOXES[i][0][0] && creature[0]<=Globals.BOXES[i][1][0]) && - (creature[1]>=Globals.BOXES[i][0][1] && creature[1]<=Globals.BOXES[i][1][1])) - ret = true; - } - return ret; - } - private int[] ghostValidMove (int[] ghost, int move) { int[] newPos = new int[2]; @@ -324,7 +337,7 @@ public class Node89978445 int[] xyNext = new int [2]; // Coordinates of the next valid position of the algo Queue2D q = new Queue2D (r+c - 1); // Queue to feed with possible position int [][] dist = new int [r][c]; - /* + /*< * 2D array holding all the distances from the ghost to each square of the maze */ @@ -495,7 +508,7 @@ public class Node89978445 // Find ghost distances, min and average for (i=0, averGhostDist=0 ; i ghostDist[i]) minGhostDist = ghostDist[i]; @@ -509,7 +522,7 @@ public class Node89978445 // Find flag distances and min for (i=0 ; i flagDist[i]) minFlagDist = flagDist[i]; } diff --git a/src/gr/auth/ee/dsproject/node/Queue2D.java b/src/gr/auth/ee/dsproject/node/Queue2D.java index f7c2866..e26f381 100755 --- a/src/gr/auth/ee/dsproject/node/Queue2D.java +++ b/src/gr/auth/ee/dsproject/node/Queue2D.java @@ -1,3 +1,12 @@ +/** + * @file Queue2D.java + * @brief + * File containing the Queue2D class. A queue for x,y coordinate + * pairs + * + * @author Christos Choutouridis 8997 cchoutou@ece.auth.gr + * @author Konstantina Tsechelidou 8445 konstsec@ece.auth.gr + */ package gr.auth.ee.dsproject.node; /** @@ -6,11 +15,21 @@ package gr.auth.ee.dsproject.node; */ public class Queue2D { - int [][] q; + int [][] q; //!< The queue array //int f; - int r; - int size, nItem; + int r; // Rear pointer + int size, nItem; // Queue helper size and queued items + /* + * ============ Constructors ============== + */ + /** + * @brief + * Simple constructor. Initialize all to zero + * @note + * As long as there is no Setter for buffer and read pointer + * this constructor is useless. + */ public Queue2D () { //f = 0; r = 0; @@ -18,18 +37,37 @@ public class Queue2D q = null; } + /** + * @brief + * Constructor with buffer size + * @param size The size of buffer to allocate + */ public Queue2D (int size) { //f = 0; - r = -1; + r = -1; // Init the rear value nItem = 0; this.size = size; q = new int[size][2]; } + /** + * @return The "waiting" items in queue + */ public int size () { return nItem; } + /** + * @return True if queue is empty + */ public boolean isEmpty () { return (nItem == 0) ? true : false; } + /** + * @return True if queue is full + */ public boolean isFull () { return (nItem >= size) ? true : false; } + /** + * @return + * The first item waiting in queue, without removing it + * If the queue is empty return {-1, -1} + */ public int [] peek () { int [] none = {-1, -1}; if (!isEmpty ()) { @@ -38,26 +76,18 @@ public class Queue2D else return none; } - - public int [] insert (int [] it) { - int [] none = {-1, -1}; - - if (!isFull ()) { - ++r; - q[r][0] = it[0]; - q[r][1] = it[1]; - ++nItem; - return it; - } - else - return none; - } - + + /** + * @return + * The first item waiting in queue and removes it + * If the queue is empty return {-1, -1} + */ public int [] remove () { int [] ret = {-1, -1}; if (!isEmpty ()) { ret[0] = q[0][0]; //ret[0] = q[f][0]; ret[1] = q[0][1]; //ret[1] = q[f][1]; + // shift the buffered items for (int i=0 ; i moveNodes = new ArrayList (); double ev, max = Globals.NO_EVAL; int decision = -1; - ArrayList moves = new ArrayList (); - - // loop the possible moves and fill them to list + 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) { - mv = new Node89978445 (Maze, currPosition, i); - if (mv.getEvaluation() > Globals.NO_EVAL) - moves.add (mv); - /* - * how can i rant for not to have a "stay still" move? - */ - } - - // Find the best of the moves in list - for (int i=0 ; i= max) { - max = ev; - decision = 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 moves.get (decision).getMove(); - /* - * The stock version of function: - * - * int moveToReturn = (int) (4 * Math.random()); - * return moveToReturn; - */ + return decision; } void createSubTreePacman (int depth, Node89978445 parent, Room[][] Maze, int[] currPacmanPosition)