1 public class Pacman {
\r
7 public boolean death;
\r
9 public int direction; // 0:still, 1:up, 2:down, 3:left, 4:right
\r
18 public Pacman(int x, int y, Map map) {
\r
21 this.dx = this.dy = 0;
\r
24 this.tx = this.ty = -1;
\r
26 this.destinationX = -1;
\r
27 this.destinationY = -1;
\r
31 public void setTarget(int x, int y) {
\r
36 public void tryMove() {
\r
39 // Don't let the pacman go back the way it came.
\r
40 int prevDirection = 0;
\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
49 prevDirection = direction;
\r
51 // Otherwise, we haven't reached the destionation so
\r
52 // continue in same direction.
\r
56 setNextDirection (prevDirection);
\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
67 //System.printString("bbb\n");
\r
68 getDestination (this.direction, targetx, targety, nextLocation);
\r
69 targetx = nextLocation[0];
\r
70 targety = nextLocation[1];
\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
82 bestDirection[0] = 1;
\r
83 bestDirection[3] = 2;
\r
85 bestDirection[1] = 3;
\r
86 bestDirection[2] = 4;
\r
88 bestDirection[1] = 4;
\r
89 bestDirection[2] = 3;
\r
92 bestDirection[0] = 2;
\r
93 bestDirection[3] = 1;
\r
95 bestDirection[1] = 3;
\r
96 bestDirection[2] = 4;
\r
98 bestDirection[1] = 4;
\r
99 bestDirection[2] = 3;
\r
104 bestDirection[0] = 3;
\r
105 bestDirection[3] = 4;
\r
107 bestDirection[1] = 1;
\r
108 bestDirection[2] = 2;
\r
110 bestDirection[1] = 2;
\r
111 bestDirection[2] = 1;
\r
114 bestDirection[0] = 4;
\r
115 bestDirection[3] = 3;
\r
117 bestDirection[1] = 1;
\r
118 bestDirection[2] = 2;
\r
120 bestDirection[1] = 2;
\r
121 bestDirection[2] = 1;
\r
125 /*for(int i = 0; i < 4; i++) {
\r
126 System.printString(bestDirection[i] + ",");
\r
128 System.printString("\n");*/
\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
138 //System.printString("step 3\n");
\r
139 // try to move one by one
\r
141 boolean set = false;
\r
144 while((!set) && (i < 4)) {
\r
145 if(bestDirection[i] == 1) {
\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
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
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
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
188 //System.printString("step 4\n");
\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
205 // Start off by advancing one in direction for specified location
\r
206 if (direction == 1) {
\r
209 } else if (direction == 2) {
\r
212 } else if (direction == 3) {
\r
215 } else if (direction == 4) {
\r
220 // If we violate the grid boundary,
\r
221 // then return false.
\r
224 locY == this.map.nrofblocks ||
\r
225 locX == this.map.nrofblocks) {
\r
229 boolean set = false;
\r
230 // Determine next turning location..
\r
232 if (direction == 1 || direction == 2) {
\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
242 if (direction == 1) {
\r
243 // Check for Top Warp
\r
246 point[1] = this.map.nrofblocks - 1;
\r
252 // Check for Bottom Warp
\r
253 if (locY == this.map.nrofblocks - 1) {
\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
272 if (direction == 3) {
\r
273 // Check for Left Warp
\r
275 point[0] = this.map.nrofblocks - 1;
\r
282 // Check for Right Warp
\r
283 if (locX == this.map.nrofblocks - 1) {
\r
297 public void doMove() {
\r
298 // System.printString("dx: " + this.dx + ", dy: " + this.dy + "\n");
\r