forked from dtschust/javapacman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGhost.java
157 lines (157 loc) · 3.19 KB
/
Ghost.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Ligeramente modificado, todos los creditos para: */
/* Drew Schuster */ /* https://github.com/dtschust/javapacman */
import java.util.HashSet;
import java.util.Set;
/* Ghost class controls the ghost. */ /* Agregado serializable */
class Ghost extends Mover implements java.io.Serializable
{
/* Direction ghost is heading */
char direction;
/* Last ghost location*/
int lastX;
int lastY;
/* Current ghost location */
int x;
int y;
/* The pellet the ghost is on top of */
int pelletX,pelletY;
/* The pellet the ghost was last on top of */
int lastPelletX,lastPelletY;
/*Constructor places ghost and updates states*/
public Ghost(int x, int y)
{
direction='L';
pelletX=x/gridSize-1;
pelletY=x/gridSize-1;
lastPelletX=pelletX;
lastPelletY=pelletY;
this.lastX = x;
this.lastY = y;
this.x = x;
this.y = y;
}
/* update pellet status */
public void updatePellet()
{
int tempX,tempY;
tempX = x/gridSize-1;
tempY = y/gridSize-1;
if (tempX != pelletX || tempY != pelletY)
{
lastPelletX = pelletX;
lastPelletY = pelletY;
pelletX=tempX;
pelletY = tempY;
}
}
/* Determines if the location is one where the ghost has to make a decision*/
public boolean isChoiceDest()
{
if ( x%gridSize==0&& y%gridSize==0 )
{
return true;
}
return false;
}
/* Chooses a new direction randomly for the ghost to move */
public char newDirection()
{
int random;
char backwards='U';
int newX=x,newY=y;
int lookX=x,lookY=y;
Set<Character> set = new HashSet<Character>();
switch(direction)
{
case 'L':
backwards='R';
break;
case 'R':
backwards='L';
break;
case 'U':
backwards='D';
break;
case 'D':
backwards='U';
break;
}
char newDirection = backwards;
/* While we still haven't found a valid direction */
while (newDirection == backwards || !isValidDest(lookX,lookY))
{
/* If we've tried every location, turn around and break the loop */
if (set.size()==3)
{
newDirection=backwards;
break;
}
newX=x;
newY=y;
lookX=x;
lookY=y;
/* Randomly choose a direction */
random = (int)(Math.random()*4) + 1;
if (random == 1)
{
newDirection = 'L';
newX-=increment;
lookX-= increment;
}
else if (random == 2)
{
newDirection = 'R';
newX+=increment;
lookX+= gridSize;
}
else if (random == 3)
{
newDirection = 'U';
newY-=increment;
lookY-=increment;
}
else if (random == 4)
{
newDirection = 'D';
newY+=increment;
lookY+=gridSize;
}
if (newDirection != backwards)
{
set.add(new Character(newDirection));
}
}
return newDirection;
}
/* Random move function for ghost */
public void move()
{
lastX=x;
lastY=y;
/* If we can make a decision, pick a new direction randomly */
if (isChoiceDest())
{
direction = newDirection();
}
/* If that direction is valid, move that way */
switch(direction)
{
case 'L':
if ( isValidDest(x-increment,y))
x -= increment;
break;
case 'R':
if ( isValidDest(x+gridSize,y))
x+= increment;
break;
case 'U':
if ( isValidDest(x,y-increment))
y-= increment;
break;
case 'D':
if ( isValidDest(x,y+gridSize))
y+= increment;
break;
}
}
}