From 8a0e947214f937714a47379db6a036ec9c734913 Mon Sep 17 00:00:00 2001 From: jzhou Date: Thu, 4 Sep 2008 01:01:13 +0000 Subject: [PATCH] make pacmen be able to resurrect --- Robust/src/Benchmarks/MMG/Java/Ghost.java | 84 ++++++++++--------- Robust/src/Benchmarks/MMG/Java/MMG.java | 17 ++-- Robust/src/Benchmarks/MMG/Java/Map.java | 43 ++++++++-- Robust/src/Benchmarks/MMG/Java/Pacman.java | 32 ++++++- Robust/src/Benchmarks/MMG/Nor/Ghost.java | 85 +++++++++++-------- Robust/src/Benchmarks/MMG/Nor/MMG.java | 35 ++++++-- Robust/src/Benchmarks/MMG/Nor/Map.java | 53 ++++++++++-- Robust/src/Benchmarks/MMG/Nor/Pacman.java | 32 ++++++- Robust/src/Benchmarks/MMG/Tag/Ghost.java | 97 +++++++++++++--------- Robust/src/Benchmarks/MMG/Tag/MMG.java | 22 ++++- Robust/src/Benchmarks/MMG/Tag/Map.java | 75 +++++++++++++++-- Robust/src/Benchmarks/MMG/Tag/Pacman.java | 32 ++++++- 12 files changed, 450 insertions(+), 157 deletions(-) diff --git a/Robust/src/Benchmarks/MMG/Java/Ghost.java b/Robust/src/Benchmarks/MMG/Java/Ghost.java index 9b3c46f2..b842d306 100755 --- a/Robust/src/Benchmarks/MMG/Java/Ghost.java +++ b/Robust/src/Benchmarks/MMG/Java/Ghost.java @@ -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 diff --git a/Robust/src/Benchmarks/MMG/Java/MMG.java b/Robust/src/Benchmarks/MMG/Java/MMG.java index 8ca79073..781d9ff1 100755 --- a/Robust/src/Benchmarks/MMG/Java/MMG.java +++ b/Robust/src/Benchmarks/MMG/Java/MMG.java @@ -4,12 +4,13 @@ public class MMG { public static void main(String[] args) { MMG mmg = new MMG(); - //System.printString("Startup\n"); + //System.printString("Task Startup\n"); int nrofpacs = 4; int nrofghosts = 8; Map map = new Map(nrofpacs, nrofghosts); // Initiate the map + //System.printString("Task initMap\n"); map.init(); //System.printString("Init finish\n"); int i = 0; @@ -30,6 +31,10 @@ public class MMG { 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_pacmen[i] = pacman; //System.printString("destination: " + map.desX[i] + "," + map.desY[i] + "\n"); } @@ -40,17 +45,20 @@ public class MMG { while(map.m_nrofpacs > 0) { // try to move ghost for(i = 0; i < nrofghosts; i++) { + //System.printString("Task moveGhost\n"); map.m_ghosts[i].tryMove(); } // try to move pacmen for(i = 0; i < nrofpacs; i++) { if(map.m_pacMenX[i] != -1) { + //System.printString("Task movePacman\n"); map.m_pacmen[i].tryMove(); } } // update ghosts for(i = 0; i < nrofghosts; i++) { + //System.printString("Task updateGhost\n"); map.m_ghosts[i].doMove(); map.placeGhost(map.m_ghosts[i]); } @@ -61,23 +69,22 @@ public class MMG { // update pacmen for(i = 0; i < nrofpacs; i++) { if(map.m_pacMenX[i] != -1) { + //System.printString("Task updatePac\n"); map.m_pacmen[i].doMove(); map.placePacman(map.m_pacmen[i]); //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"); boolean death = map.check(map.m_pacmen[i]); - /*if(death) { - System.printString("Pacman " + map.m_pacmen[i].m_index + " caught!\n"); - } */ } } map.m_nrofpacs -= map.m_deathcount; //System.printString(map.m_nrofpacs + " pacmen left. \n"); // reset for next run + //System.printString("Task next\n"); map.m_paccount = 0; map.m_deathcount = 0; } - System.printString("Finish\n"); + System.printString("Task Finish\n"); } } diff --git a/Robust/src/Benchmarks/MMG/Java/Map.java b/Robust/src/Benchmarks/MMG/Java/Map.java index 4720b2e8..33127364 100755 --- a/Robust/src/Benchmarks/MMG/Java/Map.java +++ b/Robust/src/Benchmarks/MMG/Java/Map.java @@ -10,6 +10,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; @@ -35,6 +39,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; @@ -59,6 +67,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; this.m_pacmen[i] = null; } @@ -109,6 +120,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++; } @@ -116,16 +130,29 @@ 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"); + } } return death; } diff --git a/Robust/src/Benchmarks/MMG/Java/Pacman.java b/Robust/src/Benchmarks/MMG/Java/Pacman.java index 386704e5..17034312 100755 --- a/Robust/src/Benchmarks/MMG/Java/Pacman.java +++ b/Robust/src/Benchmarks/MMG/Java/Pacman.java @@ -2,22 +2,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; } @@ -26,6 +34,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 diff --git a/Robust/src/Benchmarks/MMG/Nor/Ghost.java b/Robust/src/Benchmarks/MMG/Nor/Ghost.java index e150c31e..8d0b1942 100755 --- a/Robust/src/Benchmarks/MMG/Nor/Ghost.java +++ b/Robust/src/Benchmarks/MMG/Nor/Ghost.java @@ -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 diff --git a/Robust/src/Benchmarks/MMG/Nor/MMG.java b/Robust/src/Benchmarks/MMG/Nor/MMG.java index 54043358..332a8847 100755 --- a/Robust/src/Benchmarks/MMG/Nor/MMG.java +++ b/Robust/src/Benchmarks/MMG/Nor/MMG.java @@ -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]; } } diff --git a/Robust/src/Benchmarks/MMG/Nor/Map.java b/Robust/src/Benchmarks/MMG/Nor/Map.java index 14903686..7dd1972a 100755 --- a/Robust/src/Benchmarks/MMG/Nor/Map.java +++ b/Robust/src/Benchmarks/MMG/Nor/Map.java @@ -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; } diff --git a/Robust/src/Benchmarks/MMG/Nor/Pacman.java b/Robust/src/Benchmarks/MMG/Nor/Pacman.java index 2737b80c..117914c0 100755 --- a/Robust/src/Benchmarks/MMG/Nor/Pacman.java +++ b/Robust/src/Benchmarks/MMG/Nor/Pacman.java @@ -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 diff --git a/Robust/src/Benchmarks/MMG/Tag/Ghost.java b/Robust/src/Benchmarks/MMG/Tag/Ghost.java index e150c31e..1f6e4e54 100755 --- a/Robust/src/Benchmarks/MMG/Tag/Ghost.java +++ b/Robust/src/Benchmarks/MMG/Tag/Ghost.java @@ -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 diff --git a/Robust/src/Benchmarks/MMG/Tag/MMG.java b/Robust/src/Benchmarks/MMG/Tag/MMG.java index 72884abb..f5d5571e 100755 --- a/Robust/src/Benchmarks/MMG/Tag/MMG.java +++ b/Robust/src/Benchmarks/MMG/Tag/MMG.java @@ -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; } } diff --git a/Robust/src/Benchmarks/MMG/Tag/Map.java b/Robust/src/Benchmarks/MMG/Tag/Map.java index 4afacc75..c67842f0 100755 --- a/Robust/src/Benchmarks/MMG/Tag/Map.java +++ b/Robust/src/Benchmarks/MMG/Tag/Map.java @@ -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 diff --git a/Robust/src/Benchmarks/MMG/Tag/Pacman.java b/Robust/src/Benchmarks/MMG/Tag/Pacman.java index 2737b80c..117914c0 100755 --- a/Robust/src/Benchmarks/MMG/Tag/Pacman.java +++ b/Robust/src/Benchmarks/MMG/Tag/Pacman.java @@ -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 -- 2.34.1