Repository navigation
Expand file tree
/
Copy pathFrog.java
More file actions
173 lines (159 loc) · 6.36 KB
/
Copy pathFrog.java
File metadata and controls
173 lines (159 loc) · 6.36 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
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
import javafx.scene.image.Image;
import java.util.*;
/**
* This class represents a frog on the game grid.
* @author Karamveer Singh.
*/
public class Frog extends Element {
public static boolean dropDiamond = false;
/**
* Constructor for the Frog class.
* @param row row position.
* @param column column position.
*/
public Frog(final int row, final int column) {
super(row, column);
image = new Image("images/frog.png");
canBeEntered = false;
canExplode = true;
name = "Frog";
}
/**
* Moves the Frog one step closer to the player using Dijkstra's algorithm.
* Removes the player from the grid on collision.
* @param gridManager gridManager.
* @param player player.
*/
// Uses Dijkstra's algorithm to move the Frog one step closer to the player.
public void seekAndKill(final GridManager gridManager, final Player player) {
if (player == null) {
return; // No player to seek
}
Element[][] grid = gridManager.getElementGrid();
int rows = grid.length;
int cols = grid[0].length;
// Get player and frog positions
int playerRow = player.getRow();
int playerCol = player.getColumn();
int frogRow = this.getRow();
int frogCol = this.getColumn();
// Dijkstra's algorithm setup
int[][] distances = new int[rows][cols];
boolean[][] visited = new boolean[rows][cols];
int[][][] previous = new int[rows][cols][2];
for (int i = 0; i < rows; i++) {
Arrays.fill(distances[i], Integer.MAX_VALUE);
Arrays.fill(visited[i], false);
}
distances[frogRow][frogCol] = 0;
PriorityQueue<int[]> pq =
new PriorityQueue<>(Comparator.comparingInt(cell ->
distances[cell[0]][cell[1]]));
pq.add(new int[]{frogRow, frogCol});
int[][] directions = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1} // Up, down, left, right
};
while (!pq.isEmpty()) {
int[] current = pq.poll();
int currRow = current[0];
int currCol = current[1];
if (visited[currRow][currCol]) {
continue;
}
visited[currRow][currCol] = false;
if (currRow == playerRow && currCol == playerCol) {
break; // Stop if we reach the player
}
for (int[] dir : directions) {
int newRow = currRow + dir[0];
int newCol = currCol + dir[1];
// Check bounds and valid paths
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols
&& !visited[newRow][newCol]
&& (grid[newRow][newCol] instanceof Path
|| grid[newRow][newCol] instanceof Player)) {
int newDist = distances[currRow][currCol] + 1; // Add movement cost
if (newDist < distances[newRow][newCol]) {
distances[newRow][newCol] = newDist;
previous[newRow][newCol] = new int[]{currRow, currCol};
pq.add(new int[]{newRow, newCol});
}
}
}
}
// Check if the player is unreachable, if yes move randomly
if (distances[playerRow][playerCol] == Integer.MAX_VALUE) {
List<int[]> validMoves = new ArrayList<>();
for (int[] dir : directions) {
int newRow = frogRow + dir[0];
int newCol = frogCol + dir[1];
// Check bounds and if the position can be entered
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols
&& grid[newRow][newCol] instanceof Path) {
validMoves.add(new int[]{newRow, newCol});
}
}
if (!validMoves.isEmpty()) {
// Random move
Random random = new Random();
int[] randomMove = validMoves.get(random.nextInt(validMoves.size()));
int newRow = randomMove[0];
int newCol = randomMove[1];
gridManager.setElement(frogRow, frogCol, new Path(frogRow, frogCol));
gridManager.setElement(newRow, newCol, this);
this.setRow(newRow);
this.setColumn(newCol);
}
return;
}
moveToNextStep(gridManager,grid,previous,frogRow,frogCol,playerRow,playerCol);
}
/**
*
* Moves the Frog to its next step.
* @param gridManager gridManager.
* @param grid the element grid.
* @param previous the previous cell in the shortest path.
* @param frogRow the frog row position.
* @param frogCol the frog column position.
* @param playerRow the player row position.
* @param playerCol the player column position.
*/
private void moveToNextStep(GridManager gridManager, Element[][] grid, int[][][] previous,
int frogRow, int frogCol, int playerRow, int playerCol) {
LinkedList<int[]> path = new LinkedList<>();
int[] current = new int[]{playerRow, playerCol};
while (!Arrays.equals(current, new int[]{frogRow, frogCol})) {
path.addFirst(current);
current = previous[current[0]][current[1]];
if (current == null) {
return;
}
}
if (!path.isEmpty()) {
int[] nextStep = path.getFirst();
int newRow = nextStep[0];
int newCol = nextStep[1];
gridManager.setElement(frogRow, frogCol, new Path(frogRow, frogCol));
Element target = grid[newRow][newCol];
if (target instanceof Path) {
gridManager.setElement(newRow, newCol, this);
this.setRow(newRow);
this.setColumn(newCol);
} else if (target instanceof Player) {
gridManager.setElement(newRow, newCol, this);
gridManager.destroyRemoveFromList((Player) target);
this.setRow(newRow);
this.setColumn(newCol);
}
}
}
/**
* Returns a string representation of the Frog object.
* @return a string "Frog".
*/
@Override
public String toString() {
return "Frog";
}
}