forked from dtschust/javapacman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
316 lines (316 loc) · 6.52 KB
/
Player.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* Ligeramente modificado, todos los creditos para: */
/* Drew Schuster */ /* https://github.com/dtschust/javapacman */
import java.util.HashSet;
import java.util.Set;
/* This is the pacman object */ /* Agregado serializable */
class Player extends Mover implements java.io.Serializable
{
/* Direction is used in demoMode, currDirection and desiredDirection are used in non demoMode*/
char direction;
char currDirection;
char desiredDirection;
/* Keeps track of pellets eaten to determine end of game */
int pelletsEaten;
/* Last location */
int lastX;
int lastY;
/* Current location */
int x;
int y;
/* Which pellet the pacman is on top of */
int pelletX;
int pelletY;
/* Para cuando el jugador esta en modo fantasma */
int lastPelletX, lastPelletY;
/* teleport is true when travelling through the teleport tunnels*/
boolean teleport;
/* Stopped is set when the pacman is not moving or has been killed */
boolean stopped=false;
// Nombre del jugador
String nombre;
/* Score information */ // Trasplantado de Board
int currScore;
/* Es un comecoco o fantasma */
boolean comecoco=true;
/* Constructor places pacman in initial location and orientation */
public Player(int x, int y, String nombre)
{
this.nombre=nombre;
teleport=false;
pelletsEaten=0;
pelletX=x/gridSize-1;
pelletY=y/gridSize-1;
lastPelletX=pelletX;
lastPelletY=pelletY;
this.lastX=x;
this.lastY=y;
this.x=x;
this.y=y;
currDirection='L';
desiredDirection='L';
}
/* This function is used for demoMode. It is copied from the Ghost class. See that for comments */
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 (newDirection==backwards||!isValidDest(lookX, lookY))
{
if (set.size()==3)
{
newDirection=backwards;
break;
}
newX=x;
newY=y;
lookX=x;
lookY=y;
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;
}
/* This function is used for demoMode. It is copied from the Ghost class. See that for comments */
public boolean isChoiceDest()
{
if ( x%gridSize==0&&y%gridSize==0 )
{
return true;
}
return false;
}
/* This function is used for demoMode. It is copied from the Ghost class. See that for comments */
public void demoMove()
{
lastX=x;
lastY=y;
if (isChoiceDest())
{
direction=newDirection();
}
switch(direction)
{
case 'L':
if ( isValidDest(x-increment, y))
{
x-=increment;
}
else if (y==9*gridSize&&x<2 * gridSize)
{
x=max-gridSize*1;
teleport=true;
}
break;
case 'R':
if ( isValidDest(x+gridSize, y))
{
x+=increment;
}
else if (y==9*gridSize&&x>max-gridSize*2)
{
x=1*gridSize;
teleport=true;
}
break;
case 'U':
if ( isValidDest(x, y-increment))
y-=increment;
break;
case 'D':
if ( isValidDest(x, y+gridSize))
y+=increment;
break;
}
currDirection=direction;
frameCount++;
}
/* The move function moves the pacman for one frame in non demo mode */
public void move()
{
int gridSize=20;
lastX=x;
lastY=y;
/* Try to turn in the direction input by the user */
/*Can only turn if we're in center of a grid*/
if (x %20==0&&y%20==0||
/* Or if we're reversing*/
(desiredDirection=='L'&&currDirection=='R')||
(desiredDirection=='R'&&currDirection=='L')||
(desiredDirection=='U'&&currDirection=='D')||
(desiredDirection=='D'&&currDirection=='U')
)
{
switch(desiredDirection)
{
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;
}
}
/* If we haven't moved, then move in the direction the pacman was headed anyway */
if (lastX==x&&lastY==y)
{
switch(currDirection)
{
case 'L':
if ( isValidDest(x-increment, y))
x-=increment;
else if (y==9*gridSize&&x<2 * gridSize)
{
x=max-gridSize*1;
teleport=true;
}
break;
case 'R':
if ( isValidDest(x+gridSize, y))
x+=increment;
else if (y==9*gridSize&&x>max-gridSize*2)
{
x=1*gridSize;
teleport=true;
}
break;
case 'U':
if ( isValidDest(x, y-increment))
y-=increment;
break;
case 'D':
if ( isValidDest(x, y+gridSize))
y+=increment;
break;
}
}
/* If we did change direction, update currDirection to reflect that */
else
{
currDirection=desiredDirection;
}
/* If we didn't move at all, set the stopped flag */
if (lastX==x&&lastY==y)
stopped=true;
/* Otherwise, clear the stopped flag and increment the frameCount for animation purposes*/
else
{
stopped=false;
frameCount++;
}
}
/* Update what pellet the pacman is on top of */
public void updatePellet()
{
if (comecoco) {
if (x%gridSize==0&&y%gridSize==0)
{
pelletX=x/gridSize-1;
pelletY=y/gridSize-1;
}
}
else {
int tempX, tempY;
tempX=x/gridSize-1;
tempY=y/gridSize-1;
if (tempX!=pelletX||tempY!=pelletY)
{
lastPelletX=pelletX;
lastPelletY=pelletY;
pelletX=tempX;
pelletY=tempY;
}
}
}
/* Nombre del jugador */
public String getNombre() {
return this.nombre;
}
/* Posicion x actual del jugador */
public int getX()
{
return this.x;
}
/* Posicion y actual del jugador */
public int getY()
{
return this.y;
}
public int getScore() {
return this.currScore;
}
public boolean getComecoco() {
return this.comecoco;
}
public void setComecoco(boolean a) {
this.comecoco=a;
}
public void printState(boolean[][] state)
{
int num;
for(int i=0; i<20; i++)
{
for(int j=0; j<20; j++)
{
num=(this.state[i][j]) ? 1 : 8;
System.out.print(num+" ");
}
System.out.print("\n");
}
}
}