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