-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
44 lines (34 loc) · 1.35 KB
/
Player.java
File metadata and controls
44 lines (34 loc) · 1.35 KB
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
// Represents the player in the game. The player has a position on the grid and can move in different directions
public class Player {
private Point position; // Current position of the player on the grid
// Constructor for Player
public Player(Point position) {
this.position = position; // Initialize the player's position
}
// Moves the player in the specified direction, if possible. The player cannot move outside the boundaries of the grid
public void move(String direction, int maxWidth, int maxHeight) {
int newX = position.getX();
int newY = position.getY();
switch (direction) {
case "N":
newY = Math.min(newY + 1, maxHeight); // Move north if not at the top edge
break;
case "S":
newY = Math.max(newY - 1, 0); // Move south if not at the bottom edge
break;
case "E":
newX = Math.min(newX + 1, maxWidth); // Move east if not at the right edge
break;
case "W":
newX = Math.max(newX - 1, 0); // Move west if not at the left edge
break;
}
// Update the player's position
position.setX(newX);
position.setY(newY);
}
// Returns the current position of the player
public Point getPosition() {
return position;
}
} // end of Player