//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
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
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
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
// 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
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
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
//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
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
// 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
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
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
this.m_target = -1;
// find the shortest possible way to the chosen target
setNextDirection();
+ //System.printString("step 2\n");
}
private void setNextDirection() {
//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
// 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;
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++;
}*/
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++;
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];
}
}
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;
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;
//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");
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++;
}
// 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;
}
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;
}
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
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) {
this.m_index = -1;
this.m_target = -1;
this.m_direction = 0;
+ //this.m_destinationX = this.m_destinationY = -1;
this.m_map = map;
}
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++) {
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
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;
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++;
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;
}
}
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;
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;
//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");
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++;
}
// 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;
}
}
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
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;
}
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