1ab40031094de27216feeab327fb7b2a057b205a
[IRC.git] / Robust / src / Benchmarks / MMG / Java / Pacman.java
1 public class Pacman {\r
2     public int x;\r
3     public int y;\r
4     public boolean death;\r
5     public int index;\r
6     public int direction;  // 0:still, 1:up, 2:down, 3:left, 4:right\r
7     int dx;\r
8     int dy;\r
9     public int tx;\r
10     public int ty;\r
11     int destinationX;\r
12     int destinationY;\r
13     Map map;\r
14     \r
15     public Pacman(int x, int y, Map map) {\r
16         this.x = x;\r
17         this.y = y;\r
18         this.dx = this.dy = 0;\r
19         this.death = false;\r
20         this.index = -1;\r
21         this.tx = this.ty = -1;\r
22         this.direction = 0;\r
23         this.destinationX = -1;\r
24         this.destinationY = -1;\r
25         this.map = map;\r
26     }\r
27     \r
28     public void setTarget(int x, int y) {\r
29         this.tx = x;\r
30         this.ty = y;\r
31     }\r
32     \r
33     public void tryMove() {\r
34         // decide dx & dy\r
35         \r
36         // Don't let the pacman go back the way it came.\r
37         int prevDirection = 0;\r
38 \r
39         // If there is a destination, then check if the destination has been reached.\r
40         if (destinationX >= 0 && destinationY >= 0) {\r
41             // Check if the destination has been reached, if so, then\r
42             // get new destination.\r
43             if (destinationX == x && destinationY == y) {\r
44                 destinationX = -1;\r
45                 destinationY = -1;\r
46                 prevDirection = direction;\r
47             } else {\r
48                 // Otherwise, we haven't reached the destionation so\r
49                 // continue in same direction.\r
50                 return;\r
51             }\r
52         }\r
53         setNextDirection (prevDirection);\r
54     }\r
55     \r
56     private void setNextDirection(int prevDirection) {\r
57         // get target's position\r
58         int targetx = this.tx;\r
59         //System.printString("aaa\n");\r
60         int targety = this.ty;\r
61         int[] nextLocation = new int[2];\r
62         nextLocation[0] = nextLocation[1] = -1;\r
63         \r
64         //System.printString("bbb\n");\r
65         getDestination (this.direction, targetx, targety, nextLocation);\r
66         targetx = nextLocation[0];\r
67         targety = nextLocation[1];\r
68         \r
69         //System.printString("step 2\n");\r
70         // check the distance\r
71         int deltax = this.x - targetx; // <0: move right; >0: move left\r
72         int deltay = this.y - targety; // <0: move down; >0: move up\r
73         // decide the priority of four moving directions\r
74         int[] bestDirection = new int[4];\r
75         //System.printString("dx: " + deltax + "; dy: " + deltay + "\n");\r
76         if((Math.abs(deltax) > Math.abs(deltay)) && (deltay != 0)) {\r
77             // go first along y\r
78             if(deltay > 0) {\r
79                 bestDirection[0] = 1;\r
80                 bestDirection[3] = 2;\r
81                 if(deltax > 0) {\r
82                     bestDirection[1] = 3;\r
83                     bestDirection[2] = 4;\r
84                 } else {\r
85                     bestDirection[1] = 4;\r
86                     bestDirection[2] = 3;\r
87                 }\r
88             } else {\r
89                 bestDirection[0] = 2;\r
90                 bestDirection[3] = 1;\r
91                 if(deltax > 0) {\r
92                     bestDirection[1] = 3;\r
93                     bestDirection[2] = 4;\r
94                 } else {\r
95                     bestDirection[1] = 4;\r
96                     bestDirection[2] = 3;\r
97                 }\r
98             }\r
99         } else {\r
100             if(deltax > 0) {\r
101                 bestDirection[0] = 3;\r
102                 bestDirection[3] = 4;\r
103                 if(deltay > 0) {\r
104                     bestDirection[1] = 1;\r
105                     bestDirection[2] = 2;\r
106                 } else {\r
107                     bestDirection[1] = 2;\r
108                     bestDirection[2] = 1;\r
109                 }\r
110             } else {\r
111                 bestDirection[0] = 4;\r
112                 bestDirection[3] = 3;\r
113                 if(deltay > 0) {\r
114                     bestDirection[1] = 1;\r
115                     bestDirection[2] = 2;\r
116                 } else {\r
117                     bestDirection[1] = 2;\r
118                     bestDirection[2] = 1;\r
119                 }\r
120             }\r
121         }\r
122         /*for(int i = 0; i < 4; i++) {\r
123             System.printString(bestDirection[i] + ",");\r
124         }\r
125         System.printString("\n");*/\r
126         \r
127         // There's a 50% chance that the ghost will try the sub-optimal direction first.\r
128         // This will keep the ghosts from following each other and to trap Pacman.\r
129         if (this.map.r.nextDouble() < .50) {  \r
130             int temp = bestDirection[0];\r
131             bestDirection[0] = bestDirection[1];\r
132             bestDirection[1] = temp;\r
133         }\r
134               \r
135         //System.printString("step 3\n");\r
136         // try to move one by one\r
137         int i = 0;\r
138         boolean set = false;\r
139         this.dx = 0;\r
140         this.dy = 0;\r
141         while((!set) && (i < 4)) {\r
142             if(bestDirection[i] == 1) {\r
143                 // try to move up\r
144                 if((prevDirection != 2) && ((int)(this.map.map[y * this.map.nrofblocks + x] & 2) == 0)) {\r
145                     //System.printString("a\n");\r
146                     if (getDestination (1, this.x, this.y, nextLocation)) {\r
147                         this.dx = 0;\r
148                         this.dy = -1;\r
149                         set = true;\r
150                     }\r
151                 }\r
152             } else if (bestDirection[i] == 2) {\r
153                 // try to move down\r
154                 if((prevDirection != 1) && ((int)(this.map.map[y * this.map.nrofblocks + x] & 8) == 0)) {\r
155                     //System.printString("b\n");\r
156                     if (getDestination (2, this.x, this.y, nextLocation)) {\r
157                         this.dx = 0;\r
158                         this.dy = 1;\r
159                         set = true;\r
160                     }\r
161                 }\r
162             } else if (bestDirection[i] == 3) {\r
163                 // try to move left\r
164                 if((prevDirection != 4) && ((int)(this.map.map[y * this.map.nrofblocks + x] & 1) == 0)) {\r
165                     //System.printString("c\n");\r
166                     if (getDestination (3, this.x, this.y, nextLocation)) {\r
167                         this.dx = -1;\r
168                         this.dy = 0;\r
169                         set = true;\r
170                     }\r
171                 }\r
172             } else if (bestDirection[i] == 4) {\r
173                 // try to move right\r
174                 if((prevDirection != 3) && ((int)(this.map.map[y * this.map.nrofblocks + x] & 4) == 0)) {\r
175                     //System.printString("d\n");\r
176                     if (getDestination (4, this.x, this.y, nextLocation)) {\r
177                         this.dx = 1;\r
178                         this.dy = 0;\r
179                         set = true;\r
180                     }\r
181                 }\r
182             }\r
183             i++;\r
184         }\r
185         //System.printString("step 4\n");\r
186     }\r
187     \r
188     // This method will take the specified location and direction and determine\r
189     // for the given location if the thing moved in that direction, what the\r
190     // next possible turning location would be.\r
191     boolean getDestination (int direction, int locX, int locY, int[] point) {\r
192        // If the request direction is blocked by a wall, then just return the current location\r
193        if ((direction == 1 && (this.map.map[locX + locY * this.map.nrofblocks] & 2) != 0) || // up\r
194            (direction == 3 && (this.map.map[locX + locY * this.map.nrofblocks] & 1) != 0) ||  // left\r
195            (direction == 2 && (this.map.map[locX + locY * this.map.nrofblocks] & 8) != 0) || // down\r
196            (direction == 4 && (this.map.map[locX + locY * this.map.nrofblocks] & 4) != 0)) { // right \r
197           point[0] = locX;\r
198           point[1] = locY;\r
199           return false;\r
200        }\r
201           \r
202        // Start off by advancing one in direction for specified location\r
203        if (direction == 1) {\r
204            // up\r
205            locY--;\r
206        } else if (direction == 2) {\r
207            // down\r
208            locY++;\r
209        } else if (direction == 3) {\r
210            // left\r
211            locX--;\r
212        } else if (direction == 4) {\r
213            // right\r
214            locX++;\r
215        }\r
216        \r
217        // If we violate the grid boundary,\r
218        // then return false.\r
219        if (locY < 0 ||\r
220            locX < 0 ||\r
221            locY == this.map.nrofblocks ||\r
222            locX == this.map.nrofblocks) {\r
223            return false;\r
224        }\r
225        \r
226        boolean set = false;\r
227        // Determine next turning location..\r
228        while (!set) {\r
229           if (direction == 1 || direction == 2) { \r
230               // up or down\r
231               if ((this.map.map[locX + locY * this.map.nrofblocks] & 4) == 0 || // right\r
232                       (this.map.map[locX + locY * this.map.nrofblocks] & 1) == 0 || // left\r
233                       (this.map.map[locX + locY * this.map.nrofblocks] & 2) != 0 || // up\r
234                       (this.map.map[locX + locY * this.map.nrofblocks] & 8) != 0)  { // down\r
235                   point[0] = locX;\r
236                   point[1] = locY;\r
237                   set = true;\r
238                } else {\r
239                    if (direction == 1) {\r
240                        // Check for Top Warp\r
241                        if (locY == 0) {\r
242                            point[0] = locX;\r
243                            point[1] = this.map.nrofblocks - 1;\r
244                            set = true;\r
245                        } else {\r
246                            locY--;\r
247                        }\r
248                    } else {\r
249                        // Check for Bottom Warp\r
250                        if (locY == this.map.nrofblocks - 1) {\r
251                            point[0] = locX;\r
252                            point[1] = 0;\r
253                            set = true;\r
254                        } else {\r
255                            locY++;\r
256                        }\r
257                    }\r
258                }\r
259           } else {\r
260               // left or right\r
261               if ((this.map.map[locX + locY * this.map.nrofblocks] & 2) == 0 || // up\r
262                       (this.map.map[locX + locY * this.map.nrofblocks] & 8) == 0 || // down\r
263                       (this.map.map[locX + locY * this.map.nrofblocks] & 4) != 0 || // right\r
264                       (this.map.map[locX + locY * this.map.nrofblocks] & 1) != 0) { // left  \r
265                   point[0] = locX;\r
266                   point[1] = locY;\r
267                   set = true;\r
268               } else {\r
269                   if (direction == 3) {\r
270                       // Check for Left Warp\r
271                       if (locX == 0) {\r
272                           point[0] = this.map.nrofblocks - 1;\r
273                           point[1] = locY;\r
274                           set = true;\r
275                       } else {\r
276                           locX--;\r
277                       }\r
278                   } else {\r
279                       // Check for Right Warp\r
280                       if (locX == this.map.nrofblocks - 1) {\r
281                           point[0] = 0;\r
282                           point[1] = locY;\r
283                           set = true;\r
284                       } else {\r
285                           locX++;\r
286                       }\r
287                   }\r
288               }\r
289           }\r
290        }\r
291        return true;\r
292     }\r
293     \r
294     public void doMove() {\r
295         //System.printString("dx: " + this.dx + ", dy: " + this.dy + "\n");\r
296         this.x += this.dx;\r
297         this.y += this.dy;\r
298         //this.dx = 0;\r
299         //this.dy = 0;\r
300     }\r
301 }