use more sophiscated strategy for character moving
[IRC.git] / Robust / src / Benchmarks / MMG / Java / Map.java
1 public class Map {\r
2     // maze\r
3     private int m_nrofblocks;\r
4     public int[] m_map;\r
5     public Node[] m_mapNodes;\r
6     public Ghost[] m_ghosts;\r
7     public Pacman[] m_pacmen;\r
8     \r
9     // pacmen information\r
10     public int m_nrofpacs;\r
11     public int[] m_pacMenX;\r
12     public int[] m_pacMenY;\r
13     public int[] m_directions;\r
14     public int[] m_desX;\r
15     public int[] m_desY;\r
16     public int m_paccount;\r
17     public int m_deathcount;\r
18     \r
19     // ghosts information\r
20     public int m_nrofghosts;\r
21     public int[] m_ghostsX;\r
22     public int[] m_ghostsY;\r
23     public int[] m_ghostdirections;\r
24     public int[] m_targets;\r
25     public int m_ghostcount;\r
26     \r
27     // helper member\r
28     public Random m_r;\r
29     \r
30     public Map(int nrofpacs, int nrofghosts) {\r
31         //System.printString("step 1\n");\r
32         this.m_nrofblocks = 15;\r
33         this.m_map = new int[this.m_nrofblocks*this.m_nrofblocks];\r
34         this.m_mapNodes = new Node[this.m_nrofblocks*this.m_nrofblocks];\r
35         \r
36         this.m_nrofpacs = nrofpacs;\r
37         this.m_pacMenX = new int[this.m_nrofpacs];\r
38         this.m_pacMenY = new int[this.m_nrofpacs];\r
39         this.m_directions = new int[this.m_nrofpacs];\r
40         this.m_desX = new int[this.m_nrofpacs];\r
41         this.m_desY = new int[this.m_nrofpacs];\r
42         this.m_paccount = 0;\r
43         this.m_deathcount = 0;\r
44         \r
45         this.m_nrofghosts = nrofghosts;\r
46         this.m_ghostsX = new int[this.m_nrofghosts];\r
47         this.m_ghostsY = new int[this.m_nrofghosts];\r
48         this.m_ghostdirections = new int[this.m_nrofghosts];\r
49         this.m_targets = new int[this.m_nrofghosts];\r
50         this.m_ghostcount = 0;\r
51         \r
52         this.m_ghosts = new Ghost[this.m_nrofghosts];\r
53         this.m_pacmen = new Pacman[this.m_nrofpacs];\r
54         \r
55         this.m_r = new Random();\r
56         \r
57         for(int i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {\r
58             this.m_map[i] = -1;\r
59             this.m_mapNodes[i] = new Node(i%this.m_nrofblocks, i/this.m_nrofblocks, i);\r
60         }\r
61         \r
62         //System.printString("step 2\n");\r
63         for(int i = 0; i < this.m_nrofpacs; i++) {\r
64             this.m_pacMenX[i] = this.m_pacMenY[i] = -1;\r
65             this.m_desX[i] = this.m_desY[i] = -1;\r
66             this.m_pacmen[i] = null;\r
67         }\r
68         //System.printString("step 3\n");\r
69         for(int i = 0; i < this.m_nrofghosts; i++) {\r
70             this.m_ghostsX[i] = this.m_ghostsY[i] = -1;\r
71             this.m_targets[i] = -1;\r
72             this.m_ghosts[i] = null;\r
73         }\r
74         //System.printString("step 4\n");\r
75     }\r
76     \r
77     public void init() {\r
78         // initilize the maze\r
79         int i = 0;\r
80         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;\r
81         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;\r
82         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;\r
83         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;\r
84         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;\r
85         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;\r
86         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;\r
87         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;\r
88         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;\r
89         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;\r
90         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;\r
91         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;\r
92         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
93         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
94         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
95         \r
96         // initilize the graph of the maze\r
97         for(i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {\r
98             int tmp = this.m_map[i];\r
99             Node tmpNode = this.m_mapNodes[i];\r
100             int locX = tmpNode.getXLoc();\r
101             int locY = tmpNode.getYLoc();\r
102             if((int)(tmp & 1) == 0) {\r
103                 // can go left\r
104                 if(locX == 0) {\r
105                     tmpNode.addNeighbour(this.m_mapNodes[locY * this.m_nrofblocks + this.m_nrofblocks - 1]);\r
106                 } else {\r
107                     tmpNode.addNeighbour(this.m_mapNodes[i - 1]);\r
108                 }\r
109             } \r
110             if((int)(tmp & 2) == 0) {\r
111                 // can go up\r
112                 if(locY == 0) {\r
113                     tmpNode.addNeighbour(this.m_mapNodes[(this.m_nrofblocks - 1) * this.m_nrofblocks + locX]);\r
114                 } else {\r
115                     tmpNode.addNeighbour(this.m_mapNodes[(locY - 1) * this.m_nrofblocks + locX]);\r
116                 }\r
117             }\r
118             if((int)(tmp & 4) == 0) {\r
119                 // can go right\r
120                 if(locX == this.m_nrofblocks - 1) {\r
121                     tmpNode.addNeighbour(this.m_mapNodes[locY * this.m_nrofblocks]);\r
122                 } else {\r
123                     tmpNode.addNeighbour(this.m_mapNodes[i + 1]);\r
124                 }\r
125             }\r
126             if((int)(tmp & 8) == 0) {\r
127                 // can go down\r
128                 if(locY == this.m_nrofblocks - 1) {\r
129                     tmpNode.addNeighbour(this.m_mapNodes[locX]);\r
130                 } else {\r
131                     tmpNode.addNeighbour(this.m_mapNodes[(locY + 1) * this.m_nrofblocks + locX]);\r
132                 }\r
133             }\r
134         }\r
135     } \r
136 \r
137     public void placePacman(Pacman t) {\r
138         this.m_pacMenX[t.m_index] = t.m_locX;\r
139         this.m_pacMenY[t.m_index] = t.m_locY;\r
140         this.m_paccount++;\r
141     }\r
142     \r
143     public void placeGhost(Ghost t) {\r
144         this.m_ghostsX[t.m_index] = t.m_locX;\r
145         this.m_ghostsY[t.m_index] = t.m_locY;\r
146         this.m_ghostcount++;\r
147     }\r
148     \r
149     public boolean check(Pacman t) {\r
150         boolean death = false;\r
151         int i = 0;\r
152         while((!death) && (i < this.m_ghostsX.length)) {\r
153             if((t.m_locX == this.m_ghostsX[i]) && (t.m_locY == this.m_ghostsY[i])) {\r
154                 death = true;\r
155             }\r
156             i++;\r
157         }\r
158         if((!death) && (t.m_locX == t.m_tx) && (t.m_locY == t.m_ty)) {\r
159             // reach the destination\r
160             //System.printString("Hit destination!\n");\r
161             death = true;\r
162         }\r
163         if(death) {\r
164             // pacman caught by ghost\r
165             // set pacman as death\r
166             t.m_death = true;\r
167             // kick it out\r
168             //this.m_map[t.y * this.m_nrofblocks + t.x - 1] -= 16;\r
169             this.m_deathcount++;\r
170             this.m_pacMenX[t.m_index] = -1;\r
171             this.m_pacMenY[t.m_index] = -1;\r
172         }\r
173         return death;\r
174     }\r
175     \r
176     public boolean isfinish() {\r
177         return this.m_nrofpacs == 0;\r
178     }\r
179 }