helpful progress reporting
[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 Ghost[] m_ghosts;\r
6     public Pacman[] m_pacmen;\r
7     \r
8     // pacmen information\r
9     public int m_nrofpacs;\r
10     public int[] m_pacMenX;\r
11     public int[] m_pacMenY;\r
12     public int[] m_directions;\r
13     public int[] m_pacOriX;\r
14     public int[] m_pacOriY;\r
15     public int[] m_leftLives;\r
16     public int[] m_leftLevels;\r
17     public int[] m_desX;\r
18     public int[] m_desY;\r
19     public int m_paccount;\r
20     public int m_deathcount;\r
21     \r
22     // ghosts information\r
23     public int m_nrofghosts;\r
24     public int[] m_ghostsX;\r
25     public int[] m_ghostsY;\r
26     public int[] m_ghostdirections;\r
27     public int[] m_targets;\r
28     public int m_ghostcount;\r
29     \r
30     // helper member\r
31     public Random m_r;\r
32     \r
33     public Map(int nrofpacs, int nrofghosts) {\r
34         //System.printString("step 1\n");\r
35         this.m_nrofblocks = 15;\r
36         this.m_map = new int[this.m_nrofblocks*this.m_nrofblocks];\r
37         \r
38         this.m_nrofpacs = nrofpacs;\r
39         this.m_pacMenX = new int[this.m_nrofpacs];\r
40         this.m_pacMenY = new int[this.m_nrofpacs];\r
41         this.m_directions = new int[this.m_nrofpacs];\r
42         this.m_pacOriX = new int[this.m_nrofpacs];\r
43         this.m_pacOriY = new int[this.m_nrofpacs];\r
44         this.m_leftLives = new int[this.m_nrofpacs];\r
45         this.m_leftLevels = new int[this.m_nrofpacs];\r
46         this.m_desX = new int[this.m_nrofpacs];\r
47         this.m_desY = new int[this.m_nrofpacs];\r
48         this.m_paccount = 0;\r
49         this.m_deathcount = 0;\r
50         \r
51         this.m_nrofghosts = nrofghosts;\r
52         this.m_ghostsX = new int[this.m_nrofghosts];\r
53         this.m_ghostsY = new int[this.m_nrofghosts];\r
54         this.m_ghostdirections = new int[this.m_nrofghosts];\r
55         this.m_targets = new int[this.m_nrofghosts];\r
56         this.m_ghostcount = 0;\r
57         \r
58         this.m_ghosts = new Ghost[this.m_nrofghosts];\r
59         this.m_pacmen = new Pacman[this.m_nrofpacs];\r
60         \r
61         this.m_r = new Random();\r
62         \r
63         for(int i = 0; i < this.m_nrofblocks*this.m_nrofblocks; i++) {\r
64             this.m_map[i] = -1;\r
65         }\r
66         \r
67         //System.printString("step 2\n");\r
68         for(int i = 0; i < this.m_nrofpacs; i++) {\r
69             this.m_pacMenX[i] = this.m_pacMenY[i] = -1;\r
70             this.m_directions[i] = 0;\r
71             this.m_pacOriX[i] = this.m_pacOriY[i] = -1;\r
72             this.m_leftLives[i] = this.m_leftLevels[i] = 0;\r
73             this.m_desX[i] = this.m_desY[i] = -1;\r
74             this.m_pacmen[i] = null;\r
75         }\r
76         //System.printString("step 3\n");\r
77         for(int i = 0; i < this.m_nrofghosts; i++) {\r
78             this.m_ghostsX[i] = this.m_ghostsY[i] = -1;\r
79             this.m_targets[i] = -1;\r
80             this.m_ghosts[i] = null;\r
81         }\r
82         //System.printString("step 4\n");\r
83     }\r
84     \r
85     public void init() {\r
86         // initilize the maze\r
87         int i = 0;\r
88         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
89         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
90         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
91         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
92         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
93         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
94         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
95         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
96         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
97         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
98         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
99         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
100         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
101         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
102         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
103     } \r
104 \r
105     public void placePacman(Pacman t) {\r
106         this.m_pacMenX[t.m_index] = t.m_locX;\r
107         this.m_pacMenY[t.m_index] = t.m_locY;\r
108         this.m_paccount++;\r
109     }\r
110     \r
111     public void placeGhost(Ghost t) {\r
112         this.m_ghostsX[t.m_index] = t.m_locX;\r
113         this.m_ghostsY[t.m_index] = t.m_locY;\r
114         this.m_ghostcount++;\r
115     }\r
116     \r
117     public boolean check(Pacman t) {\r
118         boolean death = false;\r
119         int i = 0;\r
120         while((!death) && (i < this.m_ghostsX.length)) {\r
121             if((t.m_locX == this.m_ghostsX[i]) && (t.m_locY == this.m_ghostsY[i])) {\r
122                 death = true;\r
123                 t.m_death = true;\r
124                 t.m_leftLives--;\r
125                 //System.printString("Pacman " + t.m_index + " caught! " + t.m_leftLives + "\n");\r
126             }\r
127             i++;\r
128         }\r
129         if((!death) && (t.m_locX == t.m_tx) && (t.m_locY == t.m_ty)) {\r
130             // reach the destination\r
131             //System.printString("Hit destination!\n");\r
132             death = true;\r
133             t.m_success = true;\r
134             t.m_leftLevels--;\r
135             //System.printString("Pacman " + t.m_index + " hit, upgrade! " + t.m_leftLevels + "\n");\r
136         }\r
137         if(death) {\r
138             if(t.isFinish()) {\r
139                 // pacman has no more lives or no more levels\r
140                 // kick it out\r
141                 this.m_deathcount++;\r
142                 this.m_pacMenX[t.m_index] = -1;\r
143                 this.m_pacMenY[t.m_index] = -1;\r
144             } else {\r
145                 if(t.m_death) {\r
146                     this.m_leftLives[t.m_index]--;\r
147                 } else if(t.m_success) {\r
148                     this.m_leftLevels[t.m_index]--;\r
149                 }\r
150                 t.reset();\r
151                 this.m_pacMenX[t.m_index] = t.m_locX;\r
152                 this.m_pacMenY[t.m_index] = t.m_locY;\r
153                 this.m_directions[t.m_index] = 0;\r
154                 //System.printString("Pacman " + t.m_index + " reset: (" + t.m_locX + ", " + t.m_locY + ")\n");\r
155             }\r
156         }\r
157         return death;\r
158     }\r
159     \r
160     public boolean isfinish() {\r
161         return this.m_nrofpacs == 0;\r
162     }\r
163     \r
164     public Vector getNeighbours(int index) {\r
165         Vector neighbours = new Vector();\r
166         int tmp = this.m_map[index];\r
167         int locX = index % this.m_nrofblocks;\r
168         int locY = index / this.m_nrofblocks;\r
169         if((int)(tmp & 1) == 0) {\r
170             // can go left\r
171             if(locX == 0) {\r
172                 neighbours.addElement(new Integer(locY * this.m_nrofblocks + this.m_nrofblocks - 1));\r
173             } else {\r
174                 neighbours.addElement(new Integer(index - 1));\r
175             }\r
176         } \r
177         if((int)(tmp & 2) == 0) {\r
178             // can go up\r
179             if(locY == 0) {\r
180                 neighbours.addElement(new Integer((this.m_nrofblocks - 1) * this.m_nrofblocks + locX));\r
181             } else {\r
182                 neighbours.addElement(new Integer((locY - 1) * this.m_nrofblocks + locX));\r
183             }\r
184         }\r
185         if((int)(tmp & 4) == 0) {\r
186             // can go right\r
187             if(locX == this.m_nrofblocks - 1) {\r
188                 neighbours.addElement(new Integer(locY * this.m_nrofblocks));\r
189             } else {\r
190                 neighbours.addElement(new Integer(index + 1));\r
191             }\r
192         }\r
193         if((int)(tmp & 8) == 0) {\r
194             // can go down\r
195             if(locY == this.m_nrofblocks - 1) {\r
196                 neighbours.addElement(new Integer(locX));\r
197             } else {\r
198                 neighbours.addElement(new Integer((locY + 1) * this.m_nrofblocks + locX));\r
199             }\r
200         }\r
201         return neighbours;\r
202     }\r
203 }\r