private void setNextDirection() {
// current position of the ghost
- Node start = this.m_map.m_mapNodes[this.m_locY * this.m_map.m_nrofblocks + this.m_locX];
+ int start = this.m_locY * this.m_map.m_nrofblocks + this.m_locX;
boolean set = false;
Vector cuts = new Vector();
int tmptarget = 0;
//System.printString("Target: " + this.m_target + "\n");
while(!found) {
int parent = parents[index];
- if(parent == start.getIndex()) {
+ if(parent == start) {
found = true;
} else {
index = parent;
}
// set the chase direction
- int nx = this.m_map.m_mapNodes[index].getXLoc();
- int ny = this.m_map.m_mapNodes[index].getYLoc();
+ 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) {
// Array parents records parent for a node in the BFS search,
// the last item of parents records the least steps to reach end node from start node
// Vector cuts specifies which nodes can not be the first one to access in this BFS
- private boolean BFS(Node start, int[] parents, Vector cuts) {
+ private boolean BFS(int start, int[] parents, Vector cuts) {
int steps = 0;
Vector toaccess = new Vector();
- toaccess.addElement(start);
+ toaccess.addElement(new Integer(start));
while(toaccess.size() > 0) {
//System.printString("bbb\n");
// pull out the first one to access
- Node access = (Node)toaccess.elementAt(0);
+ int access = ((Integer)toaccess.elementAt(0)).intValue();
toaccess.removeElementAt(0);
for(int i = 0; i < this.m_map.m_pacMenX.length; i++) {
- if((access.getXLoc() == this.m_map.m_pacMenX[i]) && (access.getYLoc() == this.m_map.m_pacMenY[i])) {
+ if(((access%this.m_map.m_nrofblocks) == this.m_map.m_pacMenX[i]) && ((access/this.m_map.m_nrofblocks) == this.m_map.m_pacMenY[i])) {
// hit one pacman
this.m_target = i;
parents[parents.length - 1] = steps;
}
}
steps++;
- Vector neighbours = access.getNeighbours();
+ Vector neighbours = this.m_map.getNeighbours(access);
for(int i = 0; i < neighbours.size(); i++) {
- Node neighbour = (Node)neighbours.elementAt(i);
- if(parents[neighbour.getIndex()] == -1) {
+ int neighbour = ((Integer)neighbours.elementAt(i)).intValue();
+ if(parents[neighbour] == -1) {
// not accessed
boolean ignore = false;
- if(access.getIndex() == start.getIndex()) {
+ if(access == start) {
// start node, check if the neighbour node is in cuts
int j = 0;
while((!ignore) && (j < cuts.size())) {
int tmp = ((Integer)cuts.elementAt(j)).intValue();
- if(tmp == neighbour.getIndex()) {
+ if(tmp == neighbour) {
ignore = true;
}
j++;
}
}
if(!ignore) {
- parents[neighbour.getIndex()] = access.getIndex();
- toaccess.addElement(neighbour);
+ parents[neighbour] = access;
+ toaccess.addElement(new Integer(neighbour));
}
}
}
// maze\r
private int m_nrofblocks;\r
public int[] m_map;\r
- public Node[] m_mapNodes;\r
public Ghost[] m_ghosts;\r
public Pacman[] m_pacmen;\r
\r
//System.printString("step 1\n");\r
this.m_nrofblocks = 15;\r
this.m_map = new int[this.m_nrofblocks*this.m_nrofblocks];\r
- this.m_mapNodes = new Node[this.m_nrofblocks*this.m_nrofblocks];\r
\r
this.m_nrofpacs = nrofpacs;\r
this.m_pacMenX = new int[this.m_nrofpacs];\r
\r
for(int i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {\r
this.m_map[i] = -1;\r
- this.m_mapNodes[i] = new Node(i%this.m_nrofblocks, i/this.m_nrofblocks, i);\r
}\r
\r
//System.printString("step 2\n");\r
this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=2;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;\r
this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=6;this.m_map[i++]=13;this.m_map[i++]=3;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=5;\r
this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=12; // 15*15\r
- \r
- // initilize the graph of the maze\r
- for(i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {\r
- int tmp = this.m_map[i];\r
- Node tmpNode = this.m_mapNodes[i];\r
- int locX = tmpNode.getXLoc();\r
- int locY = tmpNode.getYLoc();\r
- if((int)(tmp & 1) == 0) {\r
- // can go left\r
- if(locX == 0) {\r
- tmpNode.addNeighbour(this.m_mapNodes[locY * this.m_nrofblocks + this.m_nrofblocks - 1]);\r
- } else {\r
- tmpNode.addNeighbour(this.m_mapNodes[i - 1]);\r
- }\r
- } \r
- if((int)(tmp & 2) == 0) {\r
- // can go up\r
- if(locY == 0) {\r
- tmpNode.addNeighbour(this.m_mapNodes[(this.m_nrofblocks - 1) * this.m_nrofblocks + locX]);\r
- } else {\r
- tmpNode.addNeighbour(this.m_mapNodes[(locY - 1) * this.m_nrofblocks + locX]);\r
- }\r
- }\r
- if((int)(tmp & 4) == 0) {\r
- // can go right\r
- if(locX == this.m_nrofblocks - 1) {\r
- tmpNode.addNeighbour(this.m_mapNodes[locY * this.m_nrofblocks]);\r
- } else {\r
- tmpNode.addNeighbour(this.m_mapNodes[i + 1]);\r
- }\r
- }\r
- if((int)(tmp & 8) == 0) {\r
- // can go down\r
- if(locY == this.m_nrofblocks - 1) {\r
- tmpNode.addNeighbour(this.m_mapNodes[locX]);\r
- } else {\r
- tmpNode.addNeighbour(this.m_mapNodes[(locY + 1) * this.m_nrofblocks + locX]);\r
- }\r
- }\r
- }\r
} \r
\r
public void placePacman(Pacman t) {\r
public boolean isfinish() {\r
return this.m_nrofpacs == 0;\r
}\r
+ \r
+ public Vector getNeighbours(int index) {\r
+ Vector neighbours = new Vector();\r
+ int tmp = this.m_map[index];\r
+ int locX = index % this.m_nrofblocks;\r
+ int locY = index / this.m_nrofblocks;\r
+ if((int)(tmp & 1) == 0) {\r
+ // can go left\r
+ if(locX == 0) {\r
+ neighbours.addElement(new Integer(locY * this.m_nrofblocks + this.m_nrofblocks - 1));\r
+ } else {\r
+ neighbours.addElement(new Integer(index - 1));\r
+ }\r
+ } \r
+ if((int)(tmp & 2) == 0) {\r
+ // can go up\r
+ if(locY == 0) {\r
+ neighbours.addElement(new Integer((this.m_nrofblocks - 1) * this.m_nrofblocks + locX));\r
+ } else {\r
+ neighbours.addElement(new Integer((locY - 1) * this.m_nrofblocks + locX));\r
+ }\r
+ }\r
+ if((int)(tmp & 4) == 0) {\r
+ // can go right\r
+ if(locX == this.m_nrofblocks - 1) {\r
+ neighbours.addElement(new Integer(locY * this.m_nrofblocks));\r
+ } else {\r
+ neighbours.addElement(new Integer(index + 1));\r
+ }\r
+ }\r
+ if((int)(tmp & 8) == 0) {\r
+ // can go down\r
+ if(locY == this.m_nrofblocks - 1) {\r
+ neighbours.addElement(new Integer(locX));\r
+ } else {\r
+ neighbours.addElement(new Integer((locY + 1) * this.m_nrofblocks + locX));\r
+ }\r
+ }\r
+ return neighbours;\r
+ }\r
}\r
+++ /dev/null
-public class Node {
-
- int m_locX;
- int m_locY;
- int m_index;
- Vector m_neighbours;
-
- public Node(int locX, int locY, int index) {
- this.m_locX = locX;
- this.m_locY = locY;
- this.m_index = index;
- this.m_neighbours = new Vector();
- }
-
- public void addNeighbour(Node n) {
- this.m_neighbours.addElement(n);
- }
-
- public Vector getNeighbours() {
- return this.m_neighbours;
- }
-
- public int getXLoc() {
- return this.m_locX;
- }
-
- public int getYLoc() {
- return this.m_locY;
- }
-
- public int getIndex() {
- return this.m_index;
- }
-}
\ No newline at end of file
\r
private void setNextDirection() {\r
// current position of the ghost\r
- Node start = this.m_map.m_mapNodes[this.m_locY * this.m_map.m_nrofblocks + this.m_locX];\r
+ int start = this.m_locY * this.m_map.m_nrofblocks + this.m_locX;\r
\r
// get target's position\r
int targetx = this.m_tx;\r
nextLocation[0] = nextLocation[1] = -1;\r
\r
// target's position\r
- Node end = this.m_map.m_mapNodes[targety * this.m_map.m_nrofblocks + targetx];\r
+ int end = targety * this.m_map.m_nrofblocks + targetx;\r
\r
// breadth-first traverse the graph view of the maze\r
// check the shortest path for the start node to the end node\r
} else {\r
// Reversely go over the parents array to find the next node to reach\r
boolean found = false;\r
- int index = end.getIndex();\r
+ int index = end;\r
while(!found) {\r
int parent = parents[index];\r
- if(parent == start.getIndex()) {\r
+ if(parent == start) {\r
found = true;\r
} else {\r
index = parent;\r
}\r
\r
// set the chase direction\r
- int nx = this.m_map.m_mapNodes[index].getXLoc();\r
- int ny = this.m_map.m_mapNodes[index].getYLoc();\r
+ int nx = index % this.m_map.m_nrofblocks;\r
+ int ny = index / this.m_map.m_nrofblocks;\r
this.m_dx = nx - this.m_locX;\r
this.m_dy = ny - this.m_locY;\r
if(this.m_dx > 0) {\r
// Array parents records parent for a node in the BFS search, \r
// the last item of parents records the least steps to reach end node from start node\r
// Vector cuts specifies which nodes can not be the first one to access in this BFS\r
- private boolean BFS(Node start, Node end, int[] parents, Vector cuts) {\r
+ private boolean BFS(int start, int end, int[] parents, Vector cuts) {\r
int steps = 0;\r
Vector toaccess = new Vector();\r
- toaccess.addElement(start);\r
+ toaccess.addElement(new Integer(start));\r
while(toaccess.size() > 0) {\r
// pull out the first one to access\r
- Node access = (Node)toaccess.elementAt(0);\r
+ int access = ((Integer)toaccess.elementAt(0)).intValue();\r
toaccess.removeElementAt(0);\r
- if(access.getIndex() == end.getIndex()) {\r
+ if(access == end) {\r
// hit the end node\r
parents[parents.length - 1] = steps;\r
return true;\r
}\r
steps++;\r
- Vector neighbours = access.getNeighbours();\r
+ Vector neighbours = this.m_map.getNeighbours(access);\r
for(int i = 0; i < neighbours.size(); i++) {\r
- Node neighbour = (Node)neighbours.elementAt(i);\r
- if(parents[neighbour.getIndex()] == -1) {\r
+ int neighbour = ((Integer)neighbours.elementAt(i)).intValue();\r
+ if(parents[neighbour] == -1) {\r
// not accessed\r
boolean ignore = false;\r
- if(access.getIndex() == start.getIndex()) {\r
+ if(access == start) {\r
// start node, check if the neighbour node is in cuts\r
int j = 0;\r
while((!ignore) && (j < cuts.size())) {\r
int tmp = ((Integer)cuts.elementAt(j)).intValue();\r
- if(tmp == neighbour.getIndex()) {\r
+ if(tmp == neighbour) {\r
ignore = true;\r
}\r
j++;\r
}\r
}\r
if(!ignore) {\r
- parents[neighbour.getIndex()] = access.getIndex();\r
- toaccess.addElement(neighbour);\r
+ parents[neighbour] = access;\r
+ toaccess.addElement(new Integer(neighbour));\r
}\r
}\r
}\r
\r
// check the least steps for the ghosts to reach point location\r
int chasesteps = -1;\r
- Node end = this.m_map.m_mapNodes[point[1] * this.m_map.m_nrofblocks + point[0]];\r
+ int end = point[1] * this.m_map.m_nrofblocks + point[0];\r
for(int i = 0; i < this.m_map.m_ghostsX.length; i++) {\r
- Node start = this.m_map.m_mapNodes[this.m_map.m_ghostsY[i] * this.m_map.m_nrofblocks + this.m_map.m_ghostsX[i]];\r
+ int start = this.m_map.m_ghostsY[i] * this.m_map.m_nrofblocks + this.m_map.m_ghostsX[i];\r
int parents[] = new int[this.m_map.m_nrofblocks * this.m_map.m_nrofblocks + 1];\r
for(int j = 0; j < parents.length; j++) {\r
parents[j] = -1;\r
private void setNextDirection() {
// current position of the ghost
- Node start = this.m_map.m_mapNodes[this.m_locY * this.m_map.m_nrofblocks + this.m_locX];
+ int start = this.m_locY * this.m_map.m_nrofblocks + this.m_locX;
boolean set = false;
Vector cuts = new Vector();
int tmptarget = 0;
//System.printString("Target: " + this.m_target + "\n");
while(!found) {
int parent = parents[index];
- if(parent == start.getIndex()) {
+ if(parent == start) {
found = true;
} else {
index = parent;
}
// set the chase direction
- int nx = this.m_map.m_mapNodes[index].getXLoc();
- int ny = this.m_map.m_mapNodes[index].getYLoc();
+ 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) {
// Array parents records parent for a node in the BFS search,
// the last item of parents records the least steps to reach end node from start node
// Vector cuts specifies which nodes can not be the first one to access in this BFS
- private boolean BFS(Node start, int[] parents, Vector cuts) {
+ private boolean BFS(int start, int[] parents, Vector cuts) {
+ //System.printString("aaa\n");
int steps = 0;
Vector toaccess = new Vector();
- toaccess.addElement(start);
+ toaccess.addElement(new Integer(start));
while(toaccess.size() > 0) {
//System.printString("bbb\n");
// pull out the first one to access
- Node access = (Node)toaccess.elementAt(0);
+ int access = ((Integer)toaccess.elementAt(0)).intValue();
toaccess.removeElementAt(0);
for(int i = 0; i < this.m_map.m_pacMenX.length; i++) {
- if((access.getXLoc() == this.m_map.m_pacMenX[i]) && (access.getYLoc() == this.m_map.m_pacMenY[i])) {
+ if(((access%this.m_map.m_nrofblocks) == this.m_map.m_pacMenX[i]) && ((access/this.m_map.m_nrofblocks) == this.m_map.m_pacMenY[i])) {
// hit one pacman
this.m_target = i;
parents[parents.length - 1] = steps;
}
}
steps++;
- Vector neighbours = access.getNeighbours();
+ Vector neighbours = this.m_map.getNeighbours(access);
for(int i = 0; i < neighbours.size(); i++) {
- Node neighbour = (Node)neighbours.elementAt(i);
- if(parents[neighbour.getIndex()] == -1) {
+ int neighbour = ((Integer)neighbours.elementAt(i)).intValue();
+ if(parents[neighbour] == -1) {
// not accessed
boolean ignore = false;
- if(access.getIndex() == start.getIndex()) {
+ if(access == start) {
// start node, check if the neighbour node is in cuts
int j = 0;
while((!ignore) && (j < cuts.size())) {
int tmp = ((Integer)cuts.elementAt(j)).intValue();
- if(tmp == neighbour.getIndex()) {
+ if(tmp == neighbour) {
ignore = true;
}
j++;
}
}
if(!ignore) {
- parents[neighbour.getIndex()] = access.getIndex();
- toaccess.addElement(neighbour);
+ parents[neighbour] = access;
+ toaccess.addElement(new Integer(neighbour));
}
}
}
}
+ //System.printString("ccc\n");
parents[parents.length - 1] = -1;
return false;
}
this.m_dy = 0;
//System.printString("Ghost " + this.m_index + ": (" + this.m_locX + ", " + this.m_locY + ")\n");
}
-}
+}
\ No newline at end of file
}
task finish(Map map{finish}) {
- System.printString("Task Finish\n");
+ System.printString("Task Finish\n");
taskexit(map{!finish});
}
// maze
private int m_nrofblocks;
public int[] m_map;
- public Node[] m_mapNodes;
// pacmen information
public int m_nrofpacs;
//System.printString("step 1\n");
this.m_nrofblocks = 15;
this.m_map = new int[this.m_nrofblocks*this.m_nrofblocks];
- this.m_mapNodes = new Node[this.m_nrofblocks*this.m_nrofblocks];
this.m_nrofpacs = nrofpacs;
this.m_pacMenX = new int[this.m_nrofpacs];
for(int i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {
this.m_map[i] = -1;
- this.m_mapNodes[i] = new Node(i%this.m_nrofblocks, i/this.m_nrofblocks, i);
}
//System.printString("step 2\n");
this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=2;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;
this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=6;this.m_map[i++]=13;this.m_map[i++]=3;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=5;
this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=12; // 15*15
-
- // initilize the graph of the maze
- for(i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {
- int tmp = this.m_map[i];
- Node tmpNode = this.m_mapNodes[i];
- int locX = tmpNode.getXLoc();
- int locY = tmpNode.getYLoc();
- if((int)(tmp & 1) == 0) {
- // can go left
- if(locX == 0) {
- tmpNode.addNeighbour(this.m_mapNodes[locY * this.m_nrofblocks + this.m_nrofblocks - 1]);
- } else {
- tmpNode.addNeighbour(this.m_mapNodes[i - 1]);
- }
- }
- if((int)(tmp & 2) == 0) {
- // can go up
- if(locY == 0) {
- tmpNode.addNeighbour(this.m_mapNodes[(this.m_nrofblocks - 1) * this.m_nrofblocks + locX]);
- } else {
- tmpNode.addNeighbour(this.m_mapNodes[(locY - 1) * this.m_nrofblocks + locX]);
- }
- }
- if((int)(tmp & 4) == 0) {
- // can go right
- if(locX == this.m_nrofblocks - 1) {
- tmpNode.addNeighbour(this.m_mapNodes[locY * this.m_nrofblocks]);
- } else {
- tmpNode.addNeighbour(this.m_mapNodes[i + 1]);
- }
- }
- if((int)(tmp & 8) == 0) {
- // can go down
- if(locY == this.m_nrofblocks - 1) {
- tmpNode.addNeighbour(this.m_mapNodes[locX]);
- } else {
- tmpNode.addNeighbour(this.m_mapNodes[(locY + 1) * this.m_nrofblocks + locX]);
- }
- }
- }
}
public void placePacman(Pacman t) {
public boolean isfinish() {
return this.m_nrofpacs == 0;
}
+
+ public Vector getNeighbours(int index) {
+ Vector neighbours = new Vector();
+ int tmp = this.m_map[index];
+ int locX = index % this.m_nrofblocks;
+ int locY = index / this.m_nrofblocks;
+ if((int)(tmp & 1) == 0) {
+ // can go left
+ if(locX == 0) {
+ neighbours.addElement(new Integer(locY * this.m_nrofblocks + this.m_nrofblocks - 1));
+ } else {
+ neighbours.addElement(new Integer(index - 1));
+ }
+ }
+ if((int)(tmp & 2) == 0) {
+ // can go up
+ if(locY == 0) {
+ neighbours.addElement(new Integer((this.m_nrofblocks - 1) * this.m_nrofblocks + locX));
+ } else {
+ neighbours.addElement(new Integer((locY - 1) * this.m_nrofblocks + locX));
+ }
+ }
+ if((int)(tmp & 4) == 0) {
+ // can go right
+ if(locX == this.m_nrofblocks - 1) {
+ neighbours.addElement(new Integer(locY * this.m_nrofblocks));
+ } else {
+ neighbours.addElement(new Integer(index + 1));
+ }
+ }
+ if((int)(tmp & 8) == 0) {
+ // can go down
+ if(locY == this.m_nrofblocks - 1) {
+ neighbours.addElement(new Integer(locX));
+ } else {
+ neighbours.addElement(new Integer((locY + 1) * this.m_nrofblocks + locX));
+ }
+ }
+ return neighbours;
+ }
}
+++ /dev/null
-public class Node {
-
- int m_locX;
- int m_locY;
- int m_index;
- Vector m_neighbours;
-
- public Node(int locX, int locY, int index) {
- this.m_locX = locX;
- this.m_locY = locY;
- this.m_index = index;
- this.m_neighbours = new Vector();
- }
-
- public void addNeighbour(Node n) {
- this.m_neighbours.addElement(n);
- }
-
- public Vector getNeighbours() {
- return this.m_neighbours;
- }
-
- public int getXLoc() {
- return this.m_locX;
- }
-
- public int getYLoc() {
- return this.m_locY;
- }
-
- public int getIndex() {
- return this.m_index;
- }
-}
\ No newline at end of file
private void setNextDirection() {
// current position of the ghost
- Node start = this.m_map.m_mapNodes[this.m_locY * this.m_map.m_nrofblocks + this.m_locX];
+ int start = this.m_locY * this.m_map.m_nrofblocks + this.m_locX;
// get target's position
int targetx = this.m_tx;
nextLocation[0] = nextLocation[1] = -1;
// target's position
- Node end = this.m_map.m_mapNodes[targety * this.m_map.m_nrofblocks + targetx];
+ int end = targety * this.m_map.m_nrofblocks + targetx;
// breadth-first traverse the graph view of the maze
// check the shortest path for the start node to the end node
} else {
// Reversely go over the parents array to find the next node to reach
boolean found = false;
- int index = end.getIndex();
+ int index = end;
while(!found) {
int parent = parents[index];
- if(parent == start.getIndex()) {
+ if(parent == start) {
found = true;
} else {
index = parent;
}
// set the chase direction
- int nx = this.m_map.m_mapNodes[index].getXLoc();
- int ny = this.m_map.m_mapNodes[index].getYLoc();
+ 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) {
// Array parents records parent for a node in the BFS search,
// the last item of parents records the least steps to reach end node from start node
// Vector cuts specifies which nodes can not be the first one to access in this BFS
- private boolean BFS(Node start, Node end, int[] parents, Vector cuts) {
+ private boolean BFS(int start, int end, int[] parents, Vector cuts) {
int steps = 0;
Vector toaccess = new Vector();
- toaccess.addElement(start);
+ toaccess.addElement(new Integer(start));
while(toaccess.size() > 0) {
// pull out the first one to access
- Node access = (Node)toaccess.elementAt(0);
+ int access = ((Integer)toaccess.elementAt(0)).intValue();
toaccess.removeElementAt(0);
- if(access.getIndex() == end.getIndex()) {
+ if(access == end) {
// hit the end node
parents[parents.length - 1] = steps;
return true;
}
steps++;
- Vector neighbours = access.getNeighbours();
+ Vector neighbours = this.m_map.getNeighbours(access);
for(int i = 0; i < neighbours.size(); i++) {
- Node neighbour = (Node)neighbours.elementAt(i);
- if(parents[neighbour.getIndex()] == -1) {
+ int neighbour = ((Integer)neighbours.elementAt(i)).intValue();
+ if(parents[neighbour] == -1) {
// not accessed
boolean ignore = false;
- if(access.getIndex() == start.getIndex()) {
+ if(access == start) {
// start node, check if the neighbour node is in cuts
int j = 0;
while((!ignore) && (j < cuts.size())) {
int tmp = ((Integer)cuts.elementAt(j)).intValue();
- if(tmp == neighbour.getIndex()) {
+ if(tmp == neighbour) {
ignore = true;
}
j++;
}
}
if(!ignore) {
- parents[neighbour.getIndex()] = access.getIndex();
- toaccess.addElement(neighbour);
+ parents[neighbour] = access;
+ toaccess.addElement(new Integer(neighbour));
}
}
}
locX++;
}
steps++;
-
+
boolean set = false;
// Determine next turning location.
while (!set) {
// check the least steps for the ghosts to reach point location
int chasesteps = -1;
- Node end = this.m_map.m_mapNodes[point[1] * this.m_map.m_nrofblocks + point[0]];
+ int end = point[1] * this.m_map.m_nrofblocks + point[0];
for(int i = 0; i < this.m_map.m_ghostsX.length; i++) {
- Node start = this.m_map.m_mapNodes[this.m_map.m_ghostsY[i] * this.m_map.m_nrofblocks + this.m_map.m_ghostsX[i]];
+ int start = this.m_map.m_ghostsY[i] * this.m_map.m_nrofblocks + this.m_map.m_ghostsX[i];
int parents[] = new int[this.m_map.m_nrofblocks * this.m_map.m_nrofblocks + 1];
for(int j = 0; j < parents.length; j++) {
parents[j] = -1;
private void setNextDirection() {
// current position of the ghost
- Node start = this.m_map.m_mapNodes[this.m_locY * this.m_map.m_nrofblocks + this.m_locX];
+ int start = this.m_locY * this.m_map.m_nrofblocks + this.m_locX;
boolean set = false;
Vector cuts = new Vector();
int tmptarget = 0;
//System.printString("Target: " + this.m_target + "\n");
while(!found) {
int parent = parents[index];
- if(parent == start.getIndex()) {
+ if(parent == start) {
found = true;
} else {
index = parent;
}
// set the chase direction
- int nx = this.m_map.m_mapNodes[index].getXLoc();
- int ny = this.m_map.m_mapNodes[index].getYLoc();
+ 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) {
// Array parents records parent for a node in the BFS search,
// the last item of parents records the least steps to reach end node from start node
// Vector cuts specifies which nodes can not be the first one to access in this BFS
- private boolean BFS(Node start, int[] parents, Vector cuts) {
+ private boolean BFS(int start, int[] parents, Vector cuts) {
//System.printString("aaa\n");
int steps = 0;
Vector toaccess = new Vector();
- toaccess.addElement(start);
+ toaccess.addElement(new Integer(start));
while(toaccess.size() > 0) {
//System.printString("bbb\n");
// pull out the first one to access
- Node access = (Node)toaccess.elementAt(0);
+ int access = ((Integer)toaccess.elementAt(0)).intValue();
toaccess.removeElementAt(0);
for(int i = 0; i < this.m_map.m_pacMenX.length; i++) {
- if((access.getXLoc() == this.m_map.m_pacMenX[i]) && (access.getYLoc() == this.m_map.m_pacMenY[i])) {
+ if(((access%this.m_map.m_nrofblocks) == this.m_map.m_pacMenX[i]) && ((access/this.m_map.m_nrofblocks) == this.m_map.m_pacMenY[i])) {
// hit one pacman
this.m_target = i;
parents[parents.length - 1] = steps;
}
}
steps++;
- Vector neighbours = access.getNeighbours();
+ Vector neighbours = this.m_map.getNeighbours(access);
for(int i = 0; i < neighbours.size(); i++) {
- Node neighbour = (Node)neighbours.elementAt(i);
- if(parents[neighbour.getIndex()] == -1) {
+ int neighbour = ((Integer)neighbours.elementAt(i)).intValue();
+ if(parents[neighbour] == -1) {
// not accessed
boolean ignore = false;
- if(access.getIndex() == start.getIndex()) {
+ if(access == start) {
// start node, check if the neighbour node is in cuts
int j = 0;
while((!ignore) && (j < cuts.size())) {
int tmp = ((Integer)cuts.elementAt(j)).intValue();
- if(tmp == neighbour.getIndex()) {
+ if(tmp == neighbour) {
ignore = true;
}
j++;
}
}
if(!ignore) {
- parents[neighbour.getIndex()] = access.getIndex();
- toaccess.addElement(neighbour);
+ parents[neighbour] = access;
+ toaccess.addElement(new Integer(neighbour));
}
}
}
// maze
private int m_nrofblocks;
public int[] m_map;
- public Node[] m_mapNodes;
// pacmen information
public int m_nrofpacs;
//System.printString("step 1\n");
this.m_nrofblocks = 15;
this.m_map = new int[this.m_nrofblocks*this.m_nrofblocks];
- this.m_mapNodes = new Node[this.m_nrofblocks*this.m_nrofblocks];
this.m_nrofpacs = nrofpacs;
this.m_pacMenX = new int[this.m_nrofpacs];
for(int i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {
this.m_map[i] = -1;
- this.m_mapNodes[i] = new Node(i%this.m_nrofblocks, i/this.m_nrofblocks, i);
}
//System.printString("step 2\n");
this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=2;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;
this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=6;this.m_map[i++]=13;this.m_map[i++]=3;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=11;this.m_map[i++]=14;this.m_map[i++]=5;
this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=12; // 15*15
-
- // initilize the graph of the maze
- for(i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {
- int tmp = this.m_map[i];
- Node tmpNode = this.m_mapNodes[i];
- int locX = tmpNode.getXLoc();
- int locY = tmpNode.getYLoc();
- if((int)(tmp & 1) == 0) {
- // can go left
- if(locX == 0) {
- tmpNode.addNeighbour(this.m_mapNodes[locY * this.m_nrofblocks + this.m_nrofblocks - 1]);
- } else {
- tmpNode.addNeighbour(this.m_mapNodes[i - 1]);
- }
- }
- if((int)(tmp & 2) == 0) {
- // can go up
- if(locY == 0) {
- tmpNode.addNeighbour(this.m_mapNodes[(this.m_nrofblocks - 1) * this.m_nrofblocks + locX]);
- } else {
- tmpNode.addNeighbour(this.m_mapNodes[(locY - 1) * this.m_nrofblocks + locX]);
- }
- }
- if((int)(tmp & 4) == 0) {
- // can go right
- if(locX == this.m_nrofblocks - 1) {
- tmpNode.addNeighbour(this.m_mapNodes[locY * this.m_nrofblocks]);
- } else {
- tmpNode.addNeighbour(this.m_mapNodes[i + 1]);
- }
- }
- if((int)(tmp & 8) == 0) {
- // can go down
- if(locY == this.m_nrofblocks - 1) {
- tmpNode.addNeighbour(this.m_mapNodes[locX]);
- } else {
- tmpNode.addNeighbour(this.m_mapNodes[(locY + 1) * this.m_nrofblocks + locX]);
- }
- }
- }
}
public void placePacman(Pacman t) {
public boolean isfinish() {
return this.m_nrofpacs == 0;
}
+
+ public Vector getNeighbours(int index) {
+ Vector neighbours = new Vector();
+ int tmp = this.m_map[index];
+ int locX = index % this.m_nrofblocks;
+ int locY = index / this.m_nrofblocks;
+ if((int)(tmp & 1) == 0) {
+ // can go left
+ if(locX == 0) {
+ neighbours.addElement(new Integer(locY * this.m_nrofblocks + this.m_nrofblocks - 1));
+ } else {
+ neighbours.addElement(new Integer(index - 1));
+ }
+ }
+ if((int)(tmp & 2) == 0) {
+ // can go up
+ if(locY == 0) {
+ neighbours.addElement(new Integer((this.m_nrofblocks - 1) * this.m_nrofblocks + locX));
+ } else {
+ neighbours.addElement(new Integer((locY - 1) * this.m_nrofblocks + locX));
+ }
+ }
+ if((int)(tmp & 4) == 0) {
+ // can go right
+ if(locX == this.m_nrofblocks - 1) {
+ neighbours.addElement(new Integer(locY * this.m_nrofblocks));
+ } else {
+ neighbours.addElement(new Integer(index + 1));
+ }
+ }
+ if((int)(tmp & 8) == 0) {
+ // can go down
+ if(locY == this.m_nrofblocks - 1) {
+ neighbours.addElement(new Integer(locX));
+ } else {
+ neighbours.addElement(new Integer((locY + 1) * this.m_nrofblocks + locX));
+ }
+ }
+ return neighbours;
+ }
}
\ No newline at end of file
+++ /dev/null
-public class Node {
-
- int m_locX;
- int m_locY;
- int m_index;
- Vector m_neighbours;
-
- public Node(int locX, int locY, int index) {
- this.m_locX = locX;
- this.m_locY = locY;
- this.m_index = index;
- this.m_neighbours = new Vector();
- }
-
- public void addNeighbour(Node n) {
- this.m_neighbours.addElement(n);
- }
-
- public Vector getNeighbours() {
- return this.m_neighbours;
- }
-
- public int getXLoc() {
- return this.m_locX;
- }
-
- public int getYLoc() {
- return this.m_locY;
- }
-
- public int getIndex() {
- return this.m_index;
- }
-}
\ No newline at end of file
private void setNextDirection() {
// current position of the ghost
- Node start = this.m_map.m_mapNodes[this.m_locY * this.m_map.m_nrofblocks + this.m_locX];
+ int start = this.m_locY * this.m_map.m_nrofblocks + this.m_locX;
// get target's position
int targetx = this.m_tx;
nextLocation[0] = nextLocation[1] = -1;
// target's position
- Node end = this.m_map.m_mapNodes[targety * this.m_map.m_nrofblocks + targetx];
+ int end = targety * this.m_map.m_nrofblocks + targetx;
// breadth-first traverse the graph view of the maze
// check the shortest path for the start node to the end node
} else {
// Reversely go over the parents array to find the next node to reach
boolean found = false;
- int index = end.getIndex();
+ int index = end;
while(!found) {
int parent = parents[index];
- if(parent == start.getIndex()) {
+ if(parent == start) {
found = true;
} else {
index = parent;
}
// set the chase direction
- int nx = this.m_map.m_mapNodes[index].getXLoc();
- int ny = this.m_map.m_mapNodes[index].getYLoc();
+ 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) {
// Array parents records parent for a node in the BFS search,
// the last item of parents records the least steps to reach end node from start node
// Vector cuts specifies which nodes can not be the first one to access in this BFS
- private boolean BFS(Node start, Node end, int[] parents, Vector cuts) {
+ private boolean BFS(int start, int end, int[] parents, Vector cuts) {
int steps = 0;
Vector toaccess = new Vector();
- toaccess.addElement(start);
+ toaccess.addElement(new Integer(start));
while(toaccess.size() > 0) {
// pull out the first one to access
- Node access = (Node)toaccess.elementAt(0);
+ int access = ((Integer)toaccess.elementAt(0)).intValue();
toaccess.removeElementAt(0);
- if(access.getIndex() == end.getIndex()) {
+ if(access == end) {
// hit the end node
parents[parents.length - 1] = steps;
return true;
}
steps++;
- Vector neighbours = access.getNeighbours();
+ Vector neighbours = this.m_map.getNeighbours(access);
for(int i = 0; i < neighbours.size(); i++) {
- Node neighbour = (Node)neighbours.elementAt(i);
- if(parents[neighbour.getIndex()] == -1) {
+ int neighbour = ((Integer)neighbours.elementAt(i)).intValue();
+ if(parents[neighbour] == -1) {
// not accessed
boolean ignore = false;
- if(access.getIndex() == start.getIndex()) {
+ if(access == start) {
// start node, check if the neighbour node is in cuts
int j = 0;
while((!ignore) && (j < cuts.size())) {
int tmp = ((Integer)cuts.elementAt(j)).intValue();
- if(tmp == neighbour.getIndex()) {
+ if(tmp == neighbour) {
ignore = true;
}
j++;
}
}
if(!ignore) {
- parents[neighbour.getIndex()] = access.getIndex();
- toaccess.addElement(neighbour);
+ parents[neighbour] = access;
+ toaccess.addElement(new Integer(neighbour));
}
}
}
locX++;
}
steps++;
-
+
boolean set = false;
// Determine next turning location.
while (!set) {
// check the least steps for the ghosts to reach point location
int chasesteps = -1;
- Node end = this.m_map.m_mapNodes[point[1] * this.m_map.m_nrofblocks + point[0]];
+ int end = point[1] * this.m_map.m_nrofblocks + point[0];
for(int i = 0; i < this.m_map.m_ghostsX.length; i++) {
- Node start = this.m_map.m_mapNodes[this.m_map.m_ghostsY[i] * this.m_map.m_nrofblocks + this.m_map.m_ghostsX[i]];
+ int start = this.m_map.m_ghostsY[i] * this.m_map.m_nrofblocks + this.m_map.m_ghostsX[i];
int parents[] = new int[this.m_map.m_nrofblocks * this.m_map.m_nrofblocks + 1];
for(int j = 0; j < parents.length; j++) {
parents[j] = -1;