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