1 public class Pacman {
\r
4 public boolean death;
\r
6 public int direction; // 0:still, 1:up, 2:down, 3:left, 4:right
\r
15 public Pacman(int x, int y, Map map) {
\r
18 this.dx = this.dy = 0;
\r
21 this.tx = this.ty = -1;
\r
23 this.destinationX = -1;
\r
24 this.destinationY = -1;
\r
28 public void setTarget(int x, int y) {
\r
33 public void tryMove() {
\r
36 // Don't let the pacman go back the way it came.
\r
37 int prevDirection = 0;
\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
46 prevDirection = direction;
\r
48 // Otherwise, we haven't reached the destionation so
\r
49 // continue in same direction.
\r
53 setNextDirection (prevDirection);
\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
64 //System.printString("bbb\n");
\r
65 getDestination (this.direction, targetx, targety, nextLocation);
\r
66 targetx = nextLocation[0];
\r
67 targety = nextLocation[1];
\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
79 bestDirection[0] = 1;
\r
80 bestDirection[3] = 2;
\r
82 bestDirection[1] = 3;
\r
83 bestDirection[2] = 4;
\r
85 bestDirection[1] = 4;
\r
86 bestDirection[2] = 3;
\r
89 bestDirection[0] = 2;
\r
90 bestDirection[3] = 1;
\r
92 bestDirection[1] = 3;
\r
93 bestDirection[2] = 4;
\r
95 bestDirection[1] = 4;
\r
96 bestDirection[2] = 3;
\r
101 bestDirection[0] = 3;
\r
102 bestDirection[3] = 4;
\r
104 bestDirection[1] = 1;
\r
105 bestDirection[2] = 2;
\r
107 bestDirection[1] = 2;
\r
108 bestDirection[2] = 1;
\r
111 bestDirection[0] = 4;
\r
112 bestDirection[3] = 3;
\r
114 bestDirection[1] = 1;
\r
115 bestDirection[2] = 2;
\r
117 bestDirection[1] = 2;
\r
118 bestDirection[2] = 1;
\r
122 /*for(int i = 0; i < 4; i++) {
\r
123 System.printString(bestDirection[i] + ",");
\r
125 System.printString("\n");*/
\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
135 //System.printString("step 3\n");
\r
136 // try to move one by one
\r
138 boolean set = false;
\r
141 while((!set) && (i < 4)) {
\r
142 if(bestDirection[i] == 1) {
\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
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
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
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
185 //System.printString("step 4\n");
\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
202 // Start off by advancing one in direction for specified location
\r
203 if (direction == 1) {
\r
206 } else if (direction == 2) {
\r
209 } else if (direction == 3) {
\r
212 } else if (direction == 4) {
\r
217 // If we violate the grid boundary,
\r
218 // then return false.
\r
221 locY == this.map.nrofblocks ||
\r
222 locX == this.map.nrofblocks) {
\r
226 boolean set = false;
\r
227 // Determine next turning location..
\r
229 if (direction == 1 || direction == 2) {
\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
239 if (direction == 1) {
\r
240 // Check for Top Warp
\r
243 point[1] = this.map.nrofblocks - 1;
\r
249 // Check for Bottom Warp
\r
250 if (locY == this.map.nrofblocks - 1) {
\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
269 if (direction == 3) {
\r
270 // Check for Left Warp
\r
272 point[0] = this.map.nrofblocks - 1;
\r
279 // Check for Right Warp
\r
280 if (locX == this.map.nrofblocks - 1) {
\r
294 public void doMove() {
\r
295 //System.printString("dx: " + this.dx + ", dy: " + this.dy + "\n");
\r