make pacmen be able to resurrect
authorjzhou <jzhou>
Thu, 4 Sep 2008 01:01:13 +0000 (01:01 +0000)
committerjzhou <jzhou>
Thu, 4 Sep 2008 01:01:13 +0000 (01:01 +0000)
12 files changed:
Robust/src/Benchmarks/MMG/Java/Ghost.java
Robust/src/Benchmarks/MMG/Java/MMG.java
Robust/src/Benchmarks/MMG/Java/Map.java
Robust/src/Benchmarks/MMG/Java/Pacman.java
Robust/src/Benchmarks/MMG/Nor/Ghost.java
Robust/src/Benchmarks/MMG/Nor/MMG.java
Robust/src/Benchmarks/MMG/Nor/Map.java
Robust/src/Benchmarks/MMG/Nor/Pacman.java
Robust/src/Benchmarks/MMG/Tag/Ghost.java
Robust/src/Benchmarks/MMG/Tag/MMG.java
Robust/src/Benchmarks/MMG/Tag/Map.java
Robust/src/Benchmarks/MMG/Tag/Pacman.java

index 9b3c46f2947d3a15fd8f59717d73ff5ad9a5b24d..b842d306dd2544169fc148cf30b17769c6531442 100755 (executable)
@@ -52,46 +52,56 @@ public class Ghost {
                //System.printString("Use first choice: (" + this.m_dx + ", " + this.m_dy + ")\n");
            } else {
                // Reversely go over the parents array to find the next node to reach
-               boolean found = false;
                int index = this.m_map.m_pacMenY[this.m_target] * this.m_map.m_nrofblocks + this.m_map.m_pacMenX[this.m_target];
-               //System.printString("Target: " + this.m_target + "\n");
-               while(!found) {
-                   int parent = parents[index];
-                   if(parent == start) {
-                       found = true;
-                   } else {
-                       index = parent;
+               int steps = parents[parents.length - 1];
+               if(steps == 0) {
+                   // already caught one pacman, stay still
+                   this.m_dx = this.m_dy = 0;
+                   this.m_map.m_ghostdirections[this.m_index] = this.m_direction = 0;
+                   //System.printString("Stay still\n");
+                   set = true;
+               } else {
+                   boolean found = false;
+                   while(!found) {
+                       int parent = parents[index];
+                       if(parent == start) {
+                           found = true;
+                       } else {
+                           index = parent;
+                       }
+                       // System.printString("parent: " + parent + "\n");
                    }
-               }
+                   //System.printString("Index: " + index + "\n");
 
-               // set the chase direction
-               int nx = index % this.m_map.m_nrofblocks;
-               int ny = index / this.m_map.m_nrofblocks;
-               this.m_dx = nx - this.m_locX;
-               this.m_dy = ny - this.m_locY;
-               if(this.m_dx > 0) {
-                   // right
-                   this.m_direction = 4;
-               } else if(this.m_dx < 0) {
-                   // left
-                   this.m_direction = 3;
-               } else if(this.m_dy > 0) {
-                   // down
-                   this.m_direction = 2;
-               } else if(this.m_dy < 0) {
-                   // up
-                   this.m_direction = 1;
-               } else {
-                   // still
-                   this.m_direction = 0;
-               }
-               if(first) {
-                   tmptarget = this.m_target;
-                   tmpdx = this.m_dx;
-                   tmpdy = this.m_dy;
-                   tmpdirection = this.m_direction;
-                   first = false;
-                   //System.printString("First choice: (" + tmpdx + ", " + tmpdy + ")\n");
+                   // set the chase direction
+                   int nx = index % this.m_map.m_nrofblocks;
+                   int ny = index / this.m_map.m_nrofblocks;
+                   this.m_dx = nx - this.m_locX;
+                   this.m_dy = ny - this.m_locY;
+                   if(this.m_dx > 0) {
+                       // right
+                       this.m_direction = 4;
+                   } else if(this.m_dx < 0) {
+                       // left
+                       this.m_direction = 3;
+                   } else if(this.m_dy > 0) {
+                       // down
+                       this.m_direction = 2;
+                   } else if(this.m_dy < 0) {
+                       // up
+                       this.m_direction = 1;
+                   } else {
+                       // still
+                       this.m_direction = 0;
+                   }
+                   if(first) {
+                       tmptarget = this.m_target;
+                       tmpdx = this.m_dx;
+                       tmpdy = this.m_dy;
+                       tmpdirection = this.m_direction;
+                       first = false;
+                       //System.printString("First choice: (" + tmpdx + ", " + tmpdy + ")\n");
+                   }
                }
 
                // check if this choice follows some other ghosts' path
index 8ca790736806260c389cbc463c05abcf73a88194..781d9ff128a7effec6859808f75469ff4cde84ad 100755 (executable)
@@ -4,12 +4,13 @@ public class MMG {
     public static void main(String[] args) {\r
        MMG mmg = new MMG();\r
        \r
-       //System.printString("Startup\n");\r
+       //System.printString("Task Startup\n");\r
        int nrofpacs = 4;\r
        int nrofghosts = 8;\r
        Map map = new Map(nrofpacs, nrofghosts);\r
        \r
        // Initiate the map\r
+       //System.printString("Task initMap\n");\r
        map.init();\r
        //System.printString("Init finish\n");\r
        int i = 0;\r
@@ -30,6 +31,10 @@ public class MMG {
            map.placePacman(pacman);\r
            map.m_desX[i] = tx*(i/2);\r
            map.m_desY[i] = ty*(i%2);\r
+           map.m_pacOriX[i] = pacman.m_locX;\r
+           map.m_pacOriY[i] = pacman.m_locY;\r
+           map.m_leftLives[i] = map.m_leftLevels[i] = 10;\r
+           pacman.m_leftLives = pacman.m_leftLevels = 10;\r
            map.m_pacmen[i] = pacman;\r
            //System.printString("destination: " + map.desX[i] + "," + map.desY[i] + "\n");\r
        }\r
@@ -40,17 +45,20 @@ public class MMG {
        while(map.m_nrofpacs > 0) {\r
            // try to move ghost\r
            for(i = 0; i < nrofghosts; i++) {\r
+               //System.printString("Task moveGhost\n");\r
                map.m_ghosts[i].tryMove();\r
            }\r
            // try to move pacmen\r
            for(i = 0; i < nrofpacs; i++) {\r
                if(map.m_pacMenX[i] != -1) {\r
+                   //System.printString("Task movePacman\n");\r
                    map.m_pacmen[i].tryMove();\r
                }\r
            }\r
            \r
            // update ghosts\r
            for(i = 0; i < nrofghosts; i++) {\r
+               //System.printString("Task updateGhost\n");\r
                map.m_ghosts[i].doMove();\r
                map.placeGhost(map.m_ghosts[i]);\r
            }\r
@@ -61,23 +69,22 @@ public class MMG {
            // update pacmen\r
            for(i = 0; i < nrofpacs; i++) {\r
                if(map.m_pacMenX[i] != -1) {\r
+                   //System.printString("Task updatePac\n");\r
                    map.m_pacmen[i].doMove();\r
                    map.placePacman(map.m_pacmen[i]);\r
                    //System.printString("Pacman " + map.m_pacmen[i].m_index + ": (" + map.m_pacMenX[map.m_pacmen[i].m_index] + "," + map.m_pacMenY[map.m_pacmen[i].m_index] + ")\n");\r
                    boolean death = map.check(map.m_pacmen[i]);\r
-                   /*if(death) {\r
-                       System.printString("Pacman " + map.m_pacmen[i].m_index + " caught!\n");\r
-                   } */\r
                }\r
            }\r
            map.m_nrofpacs -= map.m_deathcount;\r
            //System.printString(map.m_nrofpacs + " pacmen left. \n");\r
            \r
            // reset for next run\r
+           //System.printString("Task next\n");\r
            map.m_paccount = 0;\r
            map.m_deathcount = 0;\r
        }\r
        \r
-       System.printString("Finish\n");\r
+       System.printString("Task Finish\n");\r
     }\r
 }\r
index 4720b2e826b7c3d44dc66477462803a6e8cda1b7..33127364e1131cec3e0e782b963e7e4d7da16eb3 100755 (executable)
@@ -10,6 +10,10 @@ public class Map {
     public int[] m_pacMenX;\r
     public int[] m_pacMenY;\r
     public int[] m_directions;\r
+    public int[] m_pacOriX;\r
+    public int[] m_pacOriY;\r
+    public int[] m_leftLives;\r
+    public int[] m_leftLevels;\r
     public int[] m_desX;\r
     public int[] m_desY;\r
     public int m_paccount;\r
@@ -35,6 +39,10 @@ public class Map {
        this.m_pacMenX = new int[this.m_nrofpacs];\r
        this.m_pacMenY = new int[this.m_nrofpacs];\r
        this.m_directions = new int[this.m_nrofpacs];\r
+       this.m_pacOriX = new int[this.m_nrofpacs];\r
+       this.m_pacOriY = new int[this.m_nrofpacs];\r
+       this.m_leftLives = new int[this.m_nrofpacs];\r
+       this.m_leftLevels = new int[this.m_nrofpacs];\r
        this.m_desX = new int[this.m_nrofpacs];\r
        this.m_desY = new int[this.m_nrofpacs];\r
        this.m_paccount = 0;\r
@@ -59,6 +67,9 @@ public class Map {
        //System.printString("step 2\n");\r
        for(int i = 0; i < this.m_nrofpacs; i++) {\r
            this.m_pacMenX[i] = this.m_pacMenY[i] = -1;\r
+           this.m_directions[i] = 0;\r
+           this.m_pacOriX[i] = this.m_pacOriY[i] = -1;\r
+           this.m_leftLives[i] = this.m_leftLevels[i] = 0;\r
            this.m_desX[i] = this.m_desY[i] = -1;\r
            this.m_pacmen[i] = null;\r
        }\r
@@ -109,6 +120,9 @@ public class Map {
        while((!death) && (i < this.m_ghostsX.length)) {\r
            if((t.m_locX == this.m_ghostsX[i]) && (t.m_locY == this.m_ghostsY[i])) {\r
                death = true;\r
+               t.m_death = true;\r
+               t.m_leftLives--;\r
+               //System.printString("Pacman " + t.m_index + " caught! " + t.m_leftLives + "\n");\r
            }\r
            i++;\r
        }\r
@@ -116,16 +130,29 @@ public class Map {
            // reach the destination\r
            //System.printString("Hit destination!\n");\r
            death = true;\r
+           t.m_success = true;\r
+           t.m_leftLevels--;\r
+           //System.printString("Pacman " + t.m_index + " hit, upgrade! " + t.m_leftLevels + "\n");\r
        }\r
        if(death) {\r
-           // pacman caught by ghost\r
-           // set pacman as death\r
-           t.m_death = true;\r
-           // kick it out\r
-           //this.m_map[t.y * this.m_nrofblocks + t.x - 1] -= 16;\r
-           this.m_deathcount++;\r
-           this.m_pacMenX[t.m_index] = -1;\r
-           this.m_pacMenY[t.m_index] = -1;\r
+           if(t.isFinish()) {\r
+               // pacman has no more lives or no more levels\r
+               // kick it out\r
+               this.m_deathcount++;\r
+               this.m_pacMenX[t.m_index] = -1;\r
+               this.m_pacMenY[t.m_index] = -1;\r
+           } else {\r
+               if(t.m_death) {\r
+                   this.m_leftLives[t.m_index]--;\r
+               } else if(t.m_success) {\r
+                   this.m_leftLevels[t.m_index]--;\r
+               }\r
+               t.reset();\r
+               this.m_pacMenX[t.m_index] = t.m_locX;\r
+               this.m_pacMenY[t.m_index] = t.m_locY;\r
+               this.m_directions[t.m_index] = 0;\r
+               //System.printString("Pacman " + t.m_index + " reset: (" + t.m_locX + ", " + t.m_locY + ")\n");\r
+           }\r
        }\r
        return death;\r
     }\r
index 386704e5df275e47ed33c647afdf425504fb8b26..17034312badf41a238b12b41ae04b7629d5bfbd9 100755 (executable)
@@ -2,22 +2,30 @@ public class Pacman {
     public int m_locX;\r
     public int m_locY;\r
     public boolean m_death;\r
+    public boolean m_success;\r
     public int m_index;\r
     public int m_direction;  // 0:still, 1:up, 2:down, 3:left, 4:right\r
     int m_dx;\r
     int m_dy;\r
     public int m_tx;\r
     public int m_ty;\r
+    public int m_oriLocX;\r
+    public int m_oriLocY;\r
+    public int m_leftLives;\r
+    public int m_leftLevels;\r
     Map m_map;\r
     \r
     public Pacman(int x, int y, Map map) {\r
-       this.m_locX = x;\r
-       this.m_locY = y;\r
+       this.m_oriLocX = this.m_locX = x;\r
+       this.m_oriLocY = this.m_locY = y;\r
        this.m_dx = this.m_dy = 0;\r
        this.m_death = false;\r
+       this.m_success = false;\r
        this.m_index = -1;\r
        this.m_tx = this.m_ty = -1;\r
        this.m_direction = 0;\r
+       this.m_leftLives = 0;\r
+       this.m_leftLevels = 0;\r
        this.m_map = map;\r
     }\r
     \r
@@ -26,6 +34,26 @@ public class Pacman {
        this.m_ty = y;\r
     }\r
     \r
+    public void reset() {\r
+       if(this.m_death) {\r
+           this.m_locX = this.m_oriLocX;\r
+           this.m_locY = this.m_oriLocY;\r
+           this.m_death = false;\r
+       } else if(this.m_success) {\r
+           this.m_locX = this.m_oriLocX;\r
+           this.m_locY = this.m_oriLocY;\r
+           this.m_success = false;\r
+       }\r
+    }\r
+    \r
+    public boolean isFinish() {\r
+       if(this.m_death) {\r
+           return this.m_leftLives == 0;\r
+       } else if(this.m_success) {\r
+           return this.m_leftLevels == 0;\r
+       }\r
+    }\r
+    \r
     public void tryMove() {\r
        // decide dx & dy\r
        \r
index e150c31e45f181927c62a29fc29a920fbb7e33d6..8d0b19424e2ef6cc9cfe695b660d4c8cb7e086ba 100755 (executable)
@@ -28,6 +28,7 @@ public class Ghost {
        this.m_target = -1;
        // find the shortest possible way to the chosen target
        setNextDirection();
+       //System.printString("step 2\n");
     }
     
     private void setNextDirection() {
@@ -54,46 +55,58 @@ public class Ghost {
                //System.printString("Use first choice: (" + this.m_dx + ", " + this.m_dy + ")\n");
            } else {
                // Reversely go over the parents array to find the next node to reach
-               boolean found = false;
                int index = this.m_map.m_pacMenY[this.m_target] * this.m_map.m_nrofblocks + this.m_map.m_pacMenX[this.m_target];
+               //System.printString("Index: " + index + "\n");
                //System.printString("Target: " + this.m_target + "\n");
-               while(!found) {
-                   int parent = parents[index];
-                   if(parent == start) {
-                       found = true;
-                   } else {
-                       index = parent;
+               int steps = parents[parents.length - 1];
+               if(steps == 0) {
+                   // already caught one pacman, stay still
+                   this.m_dx = this.m_dy = 0;
+                   this.m_map.m_ghostdirections[this.m_index] = this.m_direction = 0;
+                   //System.printString("Stay still\n");
+                   set = true;
+               } else {
+                   boolean found = false;
+                   while(!found) {
+                       int parent = parents[index];
+                       if(parent == start) {
+                           found = true;
+                       } else {
+                           index = parent;
+                       }
+                       // System.printString("parent: " + parent + "\n");
                    }
-               }
+                   //System.printString("Index: " + index + "\n");
 
-               // set the chase direction
-               int nx = index % this.m_map.m_nrofblocks;
-               int ny = index / this.m_map.m_nrofblocks;
-               this.m_dx = nx - this.m_locX;
-               this.m_dy = ny - this.m_locY;
-               if(this.m_dx > 0) {
-                   // right
-                   this.m_direction = 4;
-               } else if(this.m_dx < 0) {
-                   // left
-                   this.m_direction = 3;
-               } else if(this.m_dy > 0) {
-                   // down
-                   this.m_direction = 2;
-               } else if(this.m_dy < 0) {
-                   // up
-                   this.m_direction = 1;
-               } else {
-                   // still
-                   this.m_direction = 0;
-               }
-               if(first) {
-                   tmptarget = this.m_target;
-                   tmpdx = this.m_dx;
-                   tmpdy = this.m_dy;
-                   tmpdirection = this.m_direction;
-                   first = false;
-                   //System.printString("First choice: (" + tmpdx + ", " + tmpdy + ")\n");
+                   // set the chase direction
+                   int nx = index % this.m_map.m_nrofblocks;
+                   int ny = index / this.m_map.m_nrofblocks;
+                   this.m_dx = nx - this.m_locX;
+                   this.m_dy = ny - this.m_locY;
+                   if(this.m_dx > 0) {
+                       // right
+                       this.m_direction = 4;
+                   } else if(this.m_dx < 0) {
+                       // left
+                       this.m_direction = 3;
+                   } else if(this.m_dy > 0) {
+                       // down
+                       this.m_direction = 2;
+                   } else if(this.m_dy < 0) {
+                       // up
+                       this.m_direction = 1;
+                   } else {
+                       // still
+                       this.m_direction = 0;
+                   }
+                   if(first) {
+                       tmptarget = this.m_target;
+                       tmpdx = this.m_dx;
+                       tmpdy = this.m_dy;
+                       tmpdirection = this.m_direction;
+                       first = false;
+                       //System.printString("First choice: (" + tmpdx + ", " + tmpdy + ")\n");
+                   }
                }
 
                // check if this choice follows some other ghosts' path
index 54043358f946d6484382c8c768fdd0e8c5a52548..332a88478e1392fde825d239b4678462168402be 100755 (executable)
@@ -22,13 +22,26 @@ task initMap(Map map{init}) {
     // create pacmen
     int tx = 14;
     int ty = 14;
+    //int tmpx = 0;
+    //int tmpy = 0;
     for(i = 0; i < map.m_nrofpacs; i++) {
-         Pacman pacman = new Pacman(5, 7, map){move};
-         pacman.setTarget(tx*(i/2), ty*(i%2));
-         pacman.m_index = i;
-         map.placePacman(pacman);
-         map.m_desX[i] = tx*(i/2);
-         map.m_desY[i] = ty*(i%2);
+       /*do {
+           tmpx = map.m_r.nextInt() % 14;
+       } while(tmpx < 0);
+       do {
+           tmpy = map.m_r.nextInt() % 14;
+       } while(tmpy < 0);*/
+       Pacman pacman = new Pacman(5, 7, map){move};
+       //Pacman pacman = new Pacman(tmpx, tmpy, map){move};
+       //System.printString("Pacman: (" + tmpx + ", " + tmpy + ")\n");
+       pacman.setTarget(tx*(i/2), ty*(i%2));
+       pacman.m_index = i;
+       map.placePacman(pacman);
+       map.m_desX[i] = tx*(i/2);
+       map.m_desY[i] = ty*(i%2);
+       map.m_pacOriX[i] = pacman.m_locX;
+       map.m_pacOriY[i] = pacman.m_locY;
+       map.m_leftLives[i] = map.m_leftLevels[i] = 10;
     }
     
     map.m_ghostcount = 0;
@@ -56,9 +69,12 @@ task movePacman(Pacman p{move}) {
 task updateGhost(Map map{updateGhost}, /*optional*/ Ghost g{update}) {
     //System.printString("Task updateGhost\n");
     
+    //System.printString("Ghost: " + g.m_index + "\n");
+    
     //if(isavailable(g)) {
        g.doMove();
        map.placeGhost(g);
+       //System.printString("place Ghost\n");
     /*} else {
        map.m_ghostcount++;
     }*/
@@ -84,9 +100,6 @@ task updatePac(Map map{updatePac}, /*optional*/ Pacman p{update}) {
        map.placePacman(p);
        //System.printString("Pacman " + p.m_index + ": (" + map.m_pacMenX[p.m_index] + "," + map.m_pacMenY[p.m_index] + ")\n");
        boolean death = map.check(p);
-       /*if(death) {
-           System.printString("Pacman " + p.m_index + " caught!\n");
-       }*/
     /*} else {
        map.m_deathcount++;
        map.m_paccount++;
@@ -124,6 +137,10 @@ task next(Map map{next}) {
            pacman.setTarget(map.m_desX[i], map.m_desY[i]);
            pacman.m_index = i;
            pacman.m_direction = map.m_directions[i];
+           pacman.m_oriLocX = map.m_pacOriX[i];
+           pacman.m_oriLocY = map.m_pacOriY[i];
+           pacman.m_leftLives = map.m_leftLives[i];
+           pacman.m_leftLevels = map.m_leftLevels[i];
        }
     }
     
index 14903686f51a8d4f985b5cdd164e34a138d04aae..7dd1972a7d506821d10fce2ce3e99267557c5e5b 100755 (executable)
@@ -14,6 +14,10 @@ public class Map {
     public int[] m_pacMenX;
     public int[] m_pacMenY;
     public int[] m_directions;
+    public int[] m_pacOriX;
+    public int[] m_pacOriY;
+    public int[] m_leftLives;
+    public int[] m_leftLevels;
     public int[] m_desX;
     public int[] m_desY;
     public int m_paccount;
@@ -40,6 +44,10 @@ public class Map {
        this.m_pacMenX = new int[this.m_nrofpacs];
        this.m_pacMenY = new int[this.m_nrofpacs];
        this.m_directions = new int[this.m_nrofpacs];
+       this.m_pacOriX = new int[this.m_nrofpacs];
+       this.m_pacOriY = new int[this.m_nrofpacs];
+       this.m_leftLives = new int[this.m_nrofpacs];
+       this.m_leftLevels = new int[this.m_nrofpacs];
        this.m_desX = new int[this.m_nrofpacs];
        this.m_desY = new int[this.m_nrofpacs];
        this.m_paccount = 0;
@@ -62,6 +70,9 @@ public class Map {
        //System.printString("step 2\n");
        for(int i = 0; i < this.m_nrofpacs; i++) {
            this.m_pacMenX[i] = this.m_pacMenY[i] = -1;
+           this.m_directions[i] = 0;
+           this.m_pacOriX[i] = this.m_pacOriY[i] = -1;
+           this.m_leftLives[i] = this.m_leftLevels[i] = 0;
            this.m_desX[i] = this.m_desY[i] = -1;
        }
        //System.printString("step 3\n");
@@ -110,6 +121,9 @@ public class Map {
        while((!death) && (i < this.m_ghostsX.length)) {
            if((t.m_locX == this.m_ghostsX[i]) && (t.m_locY == this.m_ghostsY[i])) {
                death = true;
+               t.m_death = true;
+               t.m_leftLives--;
+               //System.printString("Pacman " + t.m_index + " caught! " + t.m_leftLives + "\n");
            }
            i++;
        }
@@ -117,16 +131,39 @@ public class Map {
            // reach the destination
            //System.printString("Hit destination!\n");
            death = true;
+           t.m_success = true;
+           t.m_leftLevels--;
+           //System.printString("Pacman " + t.m_index + " hit, upgrade! " + t.m_leftLevels + "\n");
+       }
+       if((!death) && (this.m_directions[t.m_index] == 0)) {
+           // the pacman stay still
+           // check if the pacman can not move any more
+           if(this.m_map[t.m_locX + t.m_locY * this.m_nrofblocks] == (1+2+4+8)) {
+               // no way to go out, dead
+               death = true;
+               t.m_death = true;
+               t.m_leftLives--;
+           }
        }
        if(death) {
-           // pacman caught by ghost
-           // set pacman as death
-           t.m_death = true;
-           // kick it out
-           //this.m_map[t.y * this.m_nrofblocks + t.x - 1] -= 16;
-           this.m_deathcount++;
-           this.m_pacMenX[t.m_index] = -1;
-           this.m_pacMenY[t.m_index] = -1;
+           if(t.isFinish()) {
+               // pacman has no more lives or no more levels
+               // kick it out
+               this.m_deathcount++;
+               this.m_pacMenX[t.m_index] = -1;
+               this.m_pacMenY[t.m_index] = -1;
+           } else {
+               if(t.m_death) {
+                   this.m_leftLives[t.m_index]--;
+               } else if(t.m_success) {
+                   this.m_leftLevels[t.m_index]--;
+               }
+               t.reset();
+               this.m_pacMenX[t.m_index] = t.m_locX;
+               this.m_pacMenY[t.m_index] = t.m_locY;
+               this.m_directions[t.m_index] = 0;
+               //System.printString("Pacman " + t.m_index + " reset: (" + t.m_locX + ", " + t.m_locY + ")\n");
+           }
        }
        return death;
     }
index 2737b80c85289c1f21d1bcf81983e7c01156ee40..117914c0841db856bf09d8eb3f236cdd6bc998ca 100755 (executable)
@@ -5,22 +5,30 @@ public class Pacman {
     public int m_locX;
     public int m_locY;
     public boolean m_death;
+    public boolean m_success;
     public int m_index;
     public int m_direction;  // 0:still, 1:up, 2:down, 3:left, 4:right
     int m_dx;
     int m_dy;
     public int m_tx;
     public int m_ty;
+    public int m_oriLocX;
+    public int m_oriLocY;
+    public int m_leftLives;
+    public int m_leftLevels;
     Map m_map;
     
     public Pacman(int x, int y, Map map) {
-       this.m_locX = x;
-       this.m_locY = y;
+       this.m_oriLocX = this.m_locX = x;
+       this.m_oriLocY = this.m_locY = y;
        this.m_dx = this.m_dy = 0;
        this.m_death = false;
+       this.m_success = false;
        this.m_index = -1;
        this.m_tx = this.m_ty = -1;
        this.m_direction = 0;
+       this.m_leftLives = 0;
+       this.m_leftLevels = 0;
        this.m_map = map;
     }
     
@@ -29,6 +37,26 @@ public class Pacman {
        this.m_ty = y;
     }
     
+    public void reset() {
+       if(this.m_death) {
+           this.m_locX = this.m_oriLocX;
+           this.m_locY = this.m_oriLocY;
+           this.m_death = false;
+       } else if(this.m_success) {
+           this.m_locX = this.m_oriLocX;
+           this.m_locY = this.m_oriLocY;
+           this.m_success = false;
+       }
+    }
+    
+    public boolean isFinish() {
+       if(this.m_death) {
+           return this.m_leftLives == 0;
+       } else if(this.m_success) {
+           return this.m_leftLevels == 0;
+       }
+    }
+    
     public void tryMove() {
        // decide dx & dy
        
index e150c31e45f181927c62a29fc29a920fbb7e33d6..1f6e4e54e810742c952f9df5c6e84aedd6b9f061 100755 (executable)
@@ -9,6 +9,8 @@ public class Ghost {
     public int m_direction;  // 0:still, 1:up, 2:down, 3:left, 4:right
     int m_dx;
     int m_dy;
+    //int m_destinationX;
+    //int m_destinationY;
     Map m_map;
     
     public Ghost(int x, int y, Map map) {
@@ -18,6 +20,7 @@ public class Ghost {
        this.m_index = -1;
        this.m_target = -1;
        this.m_direction = 0;
+       //this.m_destinationX = this.m_destinationY = -1;
        this.m_map = map;
     }
     
@@ -39,7 +42,15 @@ public class Ghost {
        int tmpdx = 0;
        int tmpdy = 0;
        int tmpdirection = 0;
+       //int tmpdestinationX = -1;
+       //int tmpdestinationY = -1;
        boolean first = true;
+       /*int[] candidateDesX = new int[this.m_map.m_destinationX.length];
+       int[] candidateDesY = new int[this.m_map.m_destinationX.length];
+       for(int i = 0; i < this.m_map.m_destinationX.length; i++) {
+           candidateDesX = this.m_map.m_destinationX[i];
+       }*/
+       
        while(!set) {
            int parents[] = new int[this.m_map.m_nrofblocks * this.m_map.m_nrofblocks + 1];
            for(int i = 0; i < parents.length; i++) {
@@ -49,51 +60,63 @@ public class Ghost {
                this.m_target = tmptarget;
                this.m_dx = tmpdx;
                this.m_dy = tmpdy;
+               //this.m_destinationX = tmpdestinationX;
+               //this.m_destinationY = tmpdestinationY;
                this.m_map.m_ghostdirections[this.m_index] = this.m_direction = tmpdirection;
                set = true;
                //System.printString("Use first choice: (" + this.m_dx + ", " + this.m_dy + ")\n");
            } else {
                // Reversely go over the parents array to find the next node to reach
-               boolean found = false;
                int index = this.m_map.m_pacMenY[this.m_target] * this.m_map.m_nrofblocks + this.m_map.m_pacMenX[this.m_target];
-               //System.printString("Target: " + this.m_target + "\n");
-               while(!found) {
-                   int parent = parents[index];
-                   if(parent == start) {
-                       found = true;
-                   } else {
-                       index = parent;
+               int steps = parents[parents.length - 1];
+               if(steps == 0) {
+                   // already caught one pacman, stay still
+                   this.m_dx = this.m_dy = 0;
+                   this.m_map.m_ghostdirections[this.m_index] = this.m_direction = 0;
+                   //System.printString("Stay still\n");
+                   set = true;
+               } else {
+                   boolean found = false;
+                   while(!found) {
+                       int parent = parents[index];
+                       if(parent == start) {
+                           found = true;
+                       } else {
+                           index = parent;
+                       }
+                       // System.printString("parent: " + parent + "\n");
                    }
-               }
+                   //System.printString("Index: " + index + "\n");
 
-               // set the chase direction
-               int nx = index % this.m_map.m_nrofblocks;
-               int ny = index / this.m_map.m_nrofblocks;
-               this.m_dx = nx - this.m_locX;
-               this.m_dy = ny - this.m_locY;
-               if(this.m_dx > 0) {
-                   // right
-                   this.m_direction = 4;
-               } else if(this.m_dx < 0) {
-                   // left
-                   this.m_direction = 3;
-               } else if(this.m_dy > 0) {
-                   // down
-                   this.m_direction = 2;
-               } else if(this.m_dy < 0) {
-                   // up
-                   this.m_direction = 1;
-               } else {
-                   // still
-                   this.m_direction = 0;
-               }
-               if(first) {
-                   tmptarget = this.m_target;
-                   tmpdx = this.m_dx;
-                   tmpdy = this.m_dy;
-                   tmpdirection = this.m_direction;
-                   first = false;
-                   //System.printString("First choice: (" + tmpdx + ", " + tmpdy + ")\n");
+                   // set the chase direction
+                   int nx = index % this.m_map.m_nrofblocks;
+                   int ny = index / this.m_map.m_nrofblocks;
+                   this.m_dx = nx - this.m_locX;
+                   this.m_dy = ny - this.m_locY;
+                   if(this.m_dx > 0) {
+                       // right
+                       this.m_direction = 4;
+                   } else if(this.m_dx < 0) {
+                       // left
+                       this.m_direction = 3;
+                   } else if(this.m_dy > 0) {
+                       // down
+                       this.m_direction = 2;
+                   } else if(this.m_dy < 0) {
+                       // up
+                       this.m_direction = 1;
+                   } else {
+                       // still
+                       this.m_direction = 0;
+                   }
+                   if(first) {
+                       tmptarget = this.m_target;
+                       tmpdx = this.m_dx;
+                       tmpdy = this.m_dy;
+                       tmpdirection = this.m_direction;
+                       first = false;
+                       //System.printString("First choice: (" + tmpdx + ", " + tmpdy + ")\n");
+                   }
                }
 
                // check if this choice follows some other ghosts' path
index 72884abb53bddb5d78f470dfa833571f0284ca0f..f5d5571e72c81c427850a772528096d40f921fc9 100755 (executable)
@@ -29,6 +29,12 @@ task initMap(Map map{init}) {
          map.placePacman(pacman);
          map.m_desX[i] = tx*(i/2);
          map.m_desY[i] = ty*(i%2);
+         map.m_pacOriX[i] = pacman.m_locX;
+         map.m_pacOriY[i] = pacman.m_locY;
+         map.m_leftLives[i] = map.m_leftLevels[i] = 10;
+         pacman.m_leftLives = pacman.m_leftLevels = 10;
+         //map.m_destinationX[i] = pacman.m_locX;
+         //map.m_destinationY[i] = pacman.m_locY;
     }
     
     map.m_ghostcount = 0;
@@ -84,9 +90,6 @@ task updatePac(Map map{updatePac}, optional Pacman p{update}) {
        map.placePacman(p);
        //System.printString("Pacman " + p.m_index + ": (" + map.m_pacMenX[p.m_index] + "," + map.m_pacMenY[p.m_index] + ")\n");
        boolean death = map.check(p);
-       /*if(death) {
-           System.printString("Pacman " + p.m_index + " caught!\n");
-       }*/
     } else {
        map.m_deathcount++;
        map.m_paccount++;
@@ -124,6 +127,19 @@ task next(Map map{next}) {
            pacman.setTarget(map.m_desX[i], map.m_desY[i]);
            pacman.m_index = i;
            pacman.m_direction = map.m_directions[i];
+           pacman.m_oriLocX = map.m_pacOriX[i];
+           pacman.m_oriLocY = map.m_pacOriY[i];
+           pacman.m_leftLives = map.m_leftLives[i];
+           pacman.m_leftLevels = map.m_leftLevels[i];
+           
+           // reset the destinations of all the pacmen in map
+           //int[] point = new int[2];
+           //pacman.getDestination(pacman.m_direction, pacman.m_locX, pacman.m_locY, point);
+           //map.m_destinationX[i] = point[0];
+           //map.m_destinationY[i] = point[1];
+       } else {
+           // reset the destinations of all the pacmen in map
+           //map.m_destinationX[i] = map.m_destinationY[i] = -1;
        }
     }
     
index 4afacc75cbd2e2507782a2bd36d72075c9b95fd9..c67842f0e75e9c3b942224d7c47cb23231acf052 100755 (executable)
@@ -14,6 +14,12 @@ public class Map {
     public int[] m_pacMenX;
     public int[] m_pacMenY;
     public int[] m_directions;
+    public int[] m_pacOriX;
+    public int[] m_pacOriY;
+    public int[] m_leftLives;
+    public int[] m_leftLevels;
+    //public int[] m_destinationX; // for ghosts to expact pamen's behaviour
+    //public int[] m_destinationY;
     public int[] m_desX;
     public int[] m_desY;
     public int m_paccount;
@@ -40,6 +46,12 @@ public class Map {
        this.m_pacMenX = new int[this.m_nrofpacs];
        this.m_pacMenY = new int[this.m_nrofpacs];
        this.m_directions = new int[this.m_nrofpacs];
+       this.m_pacOriX = new int[this.m_nrofpacs];
+       this.m_pacOriY = new int[this.m_nrofpacs];
+       this.m_leftLives = new int[this.m_nrofpacs];
+       this.m_leftLevels = new int[this.m_nrofpacs];
+       //this.m_destinationX = new int[this.m_nrofpacs * 2];
+       //this.m_destinationY = new int[this.m_nrofpacs * 2];
        this.m_desX = new int[this.m_nrofpacs];
        this.m_desY = new int[this.m_nrofpacs];
        this.m_paccount = 0;
@@ -62,6 +74,11 @@ public class Map {
        //System.printString("step 2\n");
        for(int i = 0; i < this.m_nrofpacs; i++) {
            this.m_pacMenX[i] = this.m_pacMenY[i] = -1;
+           this.m_directions[i] = 0;
+           this.m_pacOriX[i] = this.m_pacOriY[i] = -1;
+           this.m_leftLives[i] = this.m_leftLevels[i] = 0;
+           //this.m_destinationX[i] = this.m_destinationY[i] = -1;
+           //this.m_destinationX[this.m_nrofpacs + i] = this.m_destinationY[this.m_nrofpacs + i] = -1;
            this.m_desX[i] = this.m_desY[i] = -1;
        }
        //System.printString("step 3\n");
@@ -110,6 +127,9 @@ public class Map {
        while((!death) && (i < this.m_ghostsX.length)) {
            if((t.m_locX == this.m_ghostsX[i]) && (t.m_locY == this.m_ghostsY[i])) {
                death = true;
+               t.m_death = true;
+               t.m_leftLives--;
+               //System.printString("Pacman " + t.m_index + " caught! " + t.m_leftLives + "\n");
            }
            i++;
        }
@@ -117,16 +137,31 @@ public class Map {
            // reach the destination
            //System.printString("Hit destination!\n");
            death = true;
+           t.m_success = true;
+           t.m_leftLevels--;
+           //System.printString("Pacman " + t.m_index + " hit, upgrade! " + t.m_leftLevels + "\n");
        }
        if(death) {
-           // pacman caught by ghost
-           // set pacman as death
-           t.m_death = true;
-           // kick it out
-           //this.m_map[t.y * this.m_nrofblocks + t.x - 1] -= 16;
-           this.m_deathcount++;
-           this.m_pacMenX[t.m_index] = -1;
-           this.m_pacMenY[t.m_index] = -1;
+           if(t.isFinish()) {
+               // pacman has no more lives or no more levels
+               // kick it out
+               this.m_deathcount++;
+               this.m_pacMenX[t.m_index] = -1;
+               this.m_pacMenY[t.m_index] = -1;
+           } else {
+               if(t.m_death) {
+                   this.m_leftLives[t.m_index]--;
+               } else if(t.m_success) {
+                   this.m_leftLevels[t.m_index]--;
+               }
+               t.reset();
+               this.m_pacMenX[t.m_index] = t.m_locX;
+               this.m_pacMenY[t.m_index] = t.m_locY;
+               this.m_directions[t.m_index] = 0;
+               //System.printString("Pacman " + t.m_index + " reset: (" + t.m_locX + ", " + t.m_locY + ")\n");
+           }
+           //this.m_destinationX[t.m_index] = this.m_destinationY[t.m_index] = -1;
+           //this.m_destinationX[this.m_nrofblocks + t.m_index] = this.m_destinationY[this.m_nrofblocks + t.m_index] = -1;
        }
        return death;
     }
@@ -174,4 +209,28 @@ public class Map {
        }
        return neighbours;
     }
+    
+    /*public int[] getDominatePoint(int locX, int locY) {
+       int[] point = new int[2];
+       point[0] = locX;
+       point[1] = locY;
+       if(((int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 2) + 
+               (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 1) +
+               (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 8) +
+               (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 4)) == 4) {
+           // can not reach this point
+           point[0] = -1;
+           point[1] = -1;
+       } else if (((int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 2) + 
+               (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 1) +
+               (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 8) +
+               (int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 4)) < 3)  {
+           // can only reach it from multiple place
+       } else {
+           if (((int)(this.m_map.m_map[locX + locY * this.m_map.m_nrofblocks] & 2) {
+               
+           }
+       }
+       return point;
+    }*/
 }
\ No newline at end of file
index 2737b80c85289c1f21d1bcf81983e7c01156ee40..117914c0841db856bf09d8eb3f236cdd6bc998ca 100755 (executable)
@@ -5,22 +5,30 @@ public class Pacman {
     public int m_locX;
     public int m_locY;
     public boolean m_death;
+    public boolean m_success;
     public int m_index;
     public int m_direction;  // 0:still, 1:up, 2:down, 3:left, 4:right
     int m_dx;
     int m_dy;
     public int m_tx;
     public int m_ty;
+    public int m_oriLocX;
+    public int m_oriLocY;
+    public int m_leftLives;
+    public int m_leftLevels;
     Map m_map;
     
     public Pacman(int x, int y, Map map) {
-       this.m_locX = x;
-       this.m_locY = y;
+       this.m_oriLocX = this.m_locX = x;
+       this.m_oriLocY = this.m_locY = y;
        this.m_dx = this.m_dy = 0;
        this.m_death = false;
+       this.m_success = false;
        this.m_index = -1;
        this.m_tx = this.m_ty = -1;
        this.m_direction = 0;
+       this.m_leftLives = 0;
+       this.m_leftLevels = 0;
        this.m_map = map;
     }
     
@@ -29,6 +37,26 @@ public class Pacman {
        this.m_ty = y;
     }
     
+    public void reset() {
+       if(this.m_death) {
+           this.m_locX = this.m_oriLocX;
+           this.m_locY = this.m_oriLocY;
+           this.m_death = false;
+       } else if(this.m_success) {
+           this.m_locX = this.m_oriLocX;
+           this.m_locY = this.m_oriLocY;
+           this.m_success = false;
+       }
+    }
+    
+    public boolean isFinish() {
+       if(this.m_death) {
+           return this.m_leftLives == 0;
+       } else if(this.m_success) {
+           return this.m_leftLevels == 0;
+       }
+    }
+    
     public void tryMove() {
        // decide dx & dy