use more sophiscated strategy for character moving
[IRC.git] / Robust / src / Benchmarks / MMG / Nor / Map.java
index 1a861b64f35bcfcc40bb1af984c9ddd707562cc7..a75ec229ac2d52fb0c90f842d6bf75aa4d819f8a 100755 (executable)
-public class Map {\r
-    flag init;\r
-    flag updateGhost;\r
-    flag updatePac;\r
-    flag next;\r
-    flag finish;\r
-    \r
-    public int[] map;\r
-    public int[] pacMenX;\r
-    public int[] pacMenY;\r
-    public int[] directions;\r
-    public int[] ghostsX;\r
-    public int[] ghostsY;\r
-    public int[] ghostdirections;\r
-    public int[] targets;\r
-    public int[] desX;\r
-    public int[] desY;\r
-    \r
-    public int nrofghosts;\r
-    public int nrofpacs;\r
-    private int nrofblocks;\r
-    //public boolean toupdate;\r
-    public int ghostcount;\r
-    public int paccount;\r
-    public int deathcount;\r
-    public int failghostcount;\r
-    \r
-    public Random r;\r
-    \r
-    public Map(int nrofpacs, int nrofghosts) {\r
-       //System.printString("step 1\n");\r
-       this.nrofblocks = 15;\r
-       this.map = new int[this.nrofblocks*this.nrofblocks];\r
-       this.nrofpacs = nrofpacs;\r
-       this.nrofghosts = nrofghosts;\r
-       this.pacMenX = new int[this.nrofpacs];\r
-       this.pacMenY = new int[this.nrofpacs];\r
-       this.directions = new int[this.nrofpacs];\r
-       this.ghostsX = new int[this.nrofghosts];\r
-       this.ghostsY = new int[this.nrofghosts];\r
-       this.ghostdirections = new int[this.nrofghosts];\r
-       this.targets = new int[this.nrofghosts];\r
-       this.desX = new int[this.nrofpacs];\r
-       this.desY = new int[this.nrofpacs];\r
-       //this.toupdate = false;\r
-       this.ghostcount = 0;\r
-       this.paccount = 0;\r
-       this.deathcount = 0;\r
-       this.failghostcount = 0;\r
-       \r
-       this.r = new Random();\r
-       \r
-       //System.printString("step 2\n");\r
-       for(int i = 0; i < this.nrofpacs; i++) {\r
-           this.pacMenX[i] = this.pacMenY[i] = -1;\r
-           this.desX[i] = this.desY[i] = -1;\r
-       }\r
-       //System.printString("step 3\n");\r
-       for(int i = 0; i < this.nrofghosts; i++) {\r
-           this.ghostsX[i] = this.ghostsY[i] = -1;\r
-           this.targets[i] = -1;\r
-       }\r
-       //System.printString("step 4\n");\r
-    }\r
-    \r
-    public void init() {\r
-       int i = 0;\r
-       this.map[i++]=3;this.map[i++]=10;this.map[i++]=10;this.map[i++]=6;this.map[i++]=9;this.map[i++]=12;this.map[i++]=3;this.map[i++]=10;this.map[i++]=6;this.map[i++]=9;this.map[i++]=12;this.map[i++]=3;this.map[i++]=10;this.map[i++]=10;this.map[i++]=6;\r
-       this.map[i++]=5;this.map[i++]=11;this.map[i++]=14;this.map[i++]=1;this.map[i++]=10;this.map[i++]=10;this.map[i++]=4;this.map[i++]=15;this.map[i++]=1;this.map[i++]=10;this.map[i++]=10;this.map[i++]=4;this.map[i++]=11;this.map[i++]=14;this.map[i++]=5;\r
-       this.map[i++]=1;this.map[i++]=10;this.map[i++]=10;this.map[i++]=4;this.map[i++]=11;this.map[i++]=6;this.map[i++]=1;this.map[i++]=10;this.map[i++]=4;this.map[i++]=3;this.map[i++]=14;this.map[i++]=1;this.map[i++]=10;this.map[i++]=10;this.map[i++]=4;\r
-       this.map[i++]=5;this.map[i++]=3;this.map[i++]=6;this.map[i++]=9;this.map[i++]=6;this.map[i++]=5;this.map[i++]=5;this.map[i++]=7;this.map[i++]=5;this.map[i++]=5;this.map[i++]=3;this.map[i++]=12;this.map[i++]=3;this.map[i++]=6;this.map[i++]=5;\r
-       this.map[i++]=5;this.map[i++]=9;this.map[i++]=8;this.map[i++]=14;this.map[i++]=5;this.map[i++]=13;this.map[i++]=5;this.map[i++]=5;this.map[i++]=5;this.map[i++]=13;this.map[i++]=5;this.map[i++]=11;this.map[i++]=8;this.map[i++]=12;this.map[i++]=5;\r
-       this.map[i++]=9;this.map[i++]=2;this.map[i++]=10;this.map[i++]=2;this.map[i++]=8;this.map[i++]=2;this.map[i++]=12;this.map[i++]=5;this.map[i++]=9;this.map[i++]=2;this.map[i++]=8;this.map[i++]=2;this.map[i++]=10;this.map[i++]=2;this.map[i++]=12;\r
-       this.map[i++]=6;this.map[i++]=5;this.map[i++]=7;this.map[i++]=5;this.map[i++]=7;this.map[i++]=5;this.map[i++]=11;this.map[i++]=8;this.map[i++]=14;this.map[i++]=5;this.map[i++]=7;this.map[i++]=5;this.map[i++]=7;this.map[i++]=5;this.map[i++]=3;\r
-       this.map[i++]=4;this.map[i++]=5;this.map[i++]=5;this.map[i++]=5;this.map[i++]=5;this.map[i++]=5;this.map[i++]=10;this.map[i++]=10;this.map[i++]=10;this.map[i++]=5;this.map[i++]=5;this.map[i++]=5;this.map[i++]=5;this.map[i++]=5;this.map[i++]=1;\r
-       this.map[i++]=12;this.map[i++]=5;this.map[i++]=13;this.map[i++]=5;this.map[i++]=13;this.map[i++]=5;this.map[i++]=11;this.map[i++]=10;this.map[i++]=14;this.map[i++]=5;this.map[i++]=13;this.map[i++]=5;this.map[i++]=13;this.map[i++]=5;this.map[i++]=9;\r
-       this.map[i++]=3;this.map[i++]=8;this.map[i++]=10;this.map[i++]=8;this.map[i++]=10;this.map[i++]=0;this.map[i++]=10;this.map[i++]=2;this.map[i++]=10;this.map[i++]=0;this.map[i++]=10;this.map[i++]=8;this.map[i++]=10;this.map[i++]=8;this.map[i++]=6;\r
-       this.map[i++]=5;this.map[i++]=3;this.map[i++]=2;this.map[i++]=2;this.map[i++]=6;this.map[i++]=5;this.map[i++]=15;this.map[i++]=5;this.map[i++]=15;this.map[i++]=5;this.map[i++]=3;this.map[i++]=2;this.map[i++]=2;this.map[i++]=6;this.map[i++]=5;\r
-       this.map[i++]=5;this.map[i++]=9;this.map[i++]=8;this.map[i++]=8;this.map[i++]=4;this.map[i++]=1;this.map[i++]=10;this.map[i++]=8;this.map[i++]=10;this.map[i++]=4;this.map[i++]=1;this.map[i++]=8;this.map[i++]=8;this.map[i++]=12;this.map[i++]=5;\r
-       this.map[i++]=1;this.map[i++]=10;this.map[i++]=10;this.map[i++]=6;this.map[i++]=13;this.map[i++]=5;this.map[i++]=11;this.map[i++]=2;this.map[i++]=14;this.map[i++]=5;this.map[i++]=13;this.map[i++]=3;this.map[i++]=10;this.map[i++]=10;this.map[i++]=4;\r
-       this.map[i++]=5;this.map[i++]=11;this.map[i++]=14;this.map[i++]=1;this.map[i++]=10;this.map[i++]=8;this.map[i++]=6;this.map[i++]=13;this.map[i++]=3;this.map[i++]=8;this.map[i++]=10;this.map[i++]=4;this.map[i++]=11;this.map[i++]=14;this.map[i++]=5;\r
-       this.map[i++]=9;this.map[i++]=10;this.map[i++]=10;this.map[i++]=12;this.map[i++]=3;this.map[i++]=6;this.map[i++]=9;this.map[i++]=10;this.map[i++]=12;this.map[i++]=3;this.map[i++]=6;this.map[i++]=9;this.map[i++]=10;this.map[i++]=10;this.map[i++]=12; // 15*15    \r
-    } \r
-\r
-    public void placePacman(Pacman t) {\r
-       this.pacMenX[t.index] = t.x;\r
-       this.pacMenY[t.index] = t.y;\r
-       //this.map[t.y * this.nrofblocks + t.x - 1] |= 16;\r
-       this.paccount++;\r
-    }\r
-    \r
-    public void placeGhost(Ghost t) {\r
-       this.ghostsX[t.index] = t.x;\r
-       this.ghostsY[t.index] = t.y;\r
-       //this.map[t.y * this.nrofblocks + t.x - 1] |= 32;\r
-       this.ghostcount++;\r
-    }\r
-    \r
-    public boolean check(Pacman t) {\r
-       boolean death = false;\r
-       int i = 0;\r
-       while((!death) && (i < this.nrofghosts)) {\r
-           if((t.x == this.ghostsX[i]) && (t.y == this.ghostsY[i])) {\r
-               death = true;\r
-           }\r
-           i++;\r
-       }\r
-       if((!death) && (t.x == t.tx) && (t.y == t.ty)) {\r
-           // reach the destination\r
-           //System.printString("Hit destination!\n");\r
-           death = true;\r
-       }\r
-       if(death) {\r
-           // pacman caught by ghost\r
-           // set pacman as death\r
-           t.death = true;\r
-           // kick it out\r
-           //this.map[t.y * this.nrofblocks + t.x - 1] -= 16;\r
-           this.deathcount++;\r
-           this.pacMenX[t.index] = -1;\r
-           this.pacMenY[t.index] = -1;\r
-       }\r
-       return death;\r
-    }\r
-    \r
-    public boolean isfinish() {\r
-       return nrofpacs == 0;\r
-    }\r
-}\r
+public class Map {
+    flag init;
+    flag updateGhost;
+    flag updatePac;
+    flag next;
+    flag finish;
+    
+    // maze
+    private int m_nrofblocks;
+    public int[] m_map;
+    public Node[] m_mapNodes;
+    
+    // pacmen information
+    public int m_nrofpacs;
+    public int[] m_pacMenX;
+    public int[] m_pacMenY;
+    public int[] m_directions;
+    public int[] m_desX;
+    public int[] m_desY;
+    public int m_paccount;
+    public int m_deathcount;
+    
+    // ghosts information
+    public int m_nrofghosts;
+    public int[] m_ghostsX;
+    public int[] m_ghostsY;
+    public int[] m_ghostdirections;
+    public int[] m_targets;
+    public int m_ghostcount;
+    public int m_failghostcount;
+    
+    // helper member
+    public Random m_r;
+    
+    public Map(int nrofpacs, int nrofghosts) {
+       //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];
+       this.m_pacMenY = new int[this.m_nrofpacs];
+       this.m_directions = 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;
+       this.m_deathcount = 0;
+       
+       this.m_nrofghosts = nrofghosts;
+       this.m_ghostsX = new int[this.m_nrofghosts];
+       this.m_ghostsY = new int[this.m_nrofghosts];
+       this.m_ghostdirections = new int[this.m_nrofghosts];
+       this.m_targets = new int[this.m_nrofghosts];
+       this.m_ghostcount = 0;
+       this.m_failghostcount = 0;
+       
+       this.m_r = new Random();
+       
+       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");
+       for(int i = 0; i < this.m_nrofpacs; i++) {
+           this.m_pacMenX[i] = this.m_pacMenY[i] = -1;
+           this.m_desX[i] = this.m_desY[i] = -1;
+       }
+       //System.printString("step 3\n");
+       for(int i = 0; i < this.m_nrofghosts; i++) {
+           this.m_ghostsX[i] = this.m_ghostsY[i] = -1;
+           this.m_targets[i] = -1;
+       }
+       //System.printString("step 4\n");
+    }
+    
+    public void init() {
+       // initilize the maze
+       int i = 0;
+       this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=6;
+       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++]=10;this.m_map[i++]=4;this.m_map[i++]=15;this.m_map[i++]=1;this.m_map[i++]=10;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++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=11;this.m_map[i++]=6;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=3;this.m_map[i++]=14;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=4;
+       this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=9;this.m_map[i++]=6;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=12;this.m_map[i++]=3;this.m_map[i++]=6;this.m_map[i++]=5;
+       this.m_map[i++]=5;this.m_map[i++]=9;this.m_map[i++]=8;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=8;this.m_map[i++]=12;this.m_map[i++]=5;
+       this.m_map[i++]=9;this.m_map[i++]=2;this.m_map[i++]=10;this.m_map[i++]=2;this.m_map[i++]=8;this.m_map[i++]=2;this.m_map[i++]=12;this.m_map[i++]=5;this.m_map[i++]=9;this.m_map[i++]=2;this.m_map[i++]=8;this.m_map[i++]=2;this.m_map[i++]=10;this.m_map[i++]=2;this.m_map[i++]=12;
+       this.m_map[i++]=6;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=8;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=7;this.m_map[i++]=5;this.m_map[i++]=3;
+       this.m_map[i++]=4;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=10;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=5;this.m_map[i++]=1;
+       this.m_map[i++]=12;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=11;this.m_map[i++]=10;this.m_map[i++]=14;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=13;this.m_map[i++]=5;this.m_map[i++]=9;
+       this.m_map[i++]=3;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=0;this.m_map[i++]=10;this.m_map[i++]=2;this.m_map[i++]=10;this.m_map[i++]=0;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=6;
+       this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=2;this.m_map[i++]=2;this.m_map[i++]=6;this.m_map[i++]=5;this.m_map[i++]=15;this.m_map[i++]=5;this.m_map[i++]=15;this.m_map[i++]=5;this.m_map[i++]=3;this.m_map[i++]=2;this.m_map[i++]=2;this.m_map[i++]=6;this.m_map[i++]=5;
+       this.m_map[i++]=5;this.m_map[i++]=9;this.m_map[i++]=8;this.m_map[i++]=8;this.m_map[i++]=4;this.m_map[i++]=1;this.m_map[i++]=10;this.m_map[i++]=8;this.m_map[i++]=10;this.m_map[i++]=4;this.m_map[i++]=1;this.m_map[i++]=8;this.m_map[i++]=8;this.m_map[i++]=12;this.m_map[i++]=5;
+       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) {
+       this.m_pacMenX[t.m_index] = t.m_locX;
+       this.m_pacMenY[t.m_index] = t.m_locY;
+       this.m_paccount++;
+    }
+    
+    public void placeGhost(Ghost t) {
+       this.m_ghostsX[t.m_index] = t.m_locX;
+       this.m_ghostsY[t.m_index] = t.m_locY;
+       this.m_ghostcount++;
+    }
+    
+    public boolean check(Pacman t) {
+       boolean death = false;
+       int i = 0;
+       while((!death) && (i < this.m_ghostsX.length)) {
+           if((t.m_locX == this.m_ghostsX[i]) && (t.m_locY == this.m_ghostsY[i])) {
+               death = true;
+           }
+           i++;
+       }
+       if((!death) && (t.m_locX == t.m_tx) && (t.m_locY == t.m_ty)) {
+           // reach the destination
+           //System.printString("Hit destination!\n");
+           death = true;
+       }
+       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;
+       }
+       return death;
+    }
+    
+    public boolean isfinish() {
+       return this.m_nrofpacs == 0;
+    }
+}
\ No newline at end of file