It is clear to me that the x and y of the creatures are being switched every time they move by ther coordinates it prints out every time i update the progam, but I cant locate where this is happening, please help.
Here is the code:
package matrixproject;
import java.util.*;
public class Creatures {
private int x;
private int y;
private int health;
public Creatures() {
x = (int) (Math.random() * MatrixProject.environment[0].length);
y = (int) (Math.random() * MatrixProject.environment.length);
health = 3;
MatrixProject.creatureCounts[y][x]++;
System.out.println("Creature created at position (" + x + ", " + y + ")");
}
public int getX() {
return x;
}
public int getY() {
return y;
}
void move(int[][] environment) {
Random random = new Random();
int prevX = x;
int prevY = y;
List<int[]> validMoves = checkMoves(environment, x, y);
if (!validMoves.isEmpty()) {
if (!isPeakOrValley(environment, x, y)) {
int[] newPosition = validMoves.get(random.nextInt(validMoves.size()));
int newX = newPosition[0];
int newY = newPosition[1];
// Decrement creature count in previous position
MatrixProject.creatureCounts[prevY][prevX]--;
// Update creature's position
x = newX;
y = newY;
// Increment creature count in new position
MatrixProject.creatureCounts[y][x]++;
}
}
System.out.println("Creature created at position (" + x + ", " + y + ")");
}
boolean isPeakOrValley(int[][] environment, int x, int y) {
int currentElevation = environment[y][x];
boolean isPeak = true;
boolean isValley = true;
// Check all neighbors
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (!(i == 0 && j == 0) && isValidPosition(environment, y + i, x + j)) {
int neighborElevation = environment[y + i][x + j];
if (neighborElevation >= currentElevation) {
isPeak = false; // If any neighbor is equal or higher, it's not a peak
}
if (neighborElevation <= currentElevation) {
isValley = false; // If any neighbor is equal or lower, it's not a valley
}
}
}
}
return isPeak || isValley; // Return true if it's either a peak or a valley
}
private List<int[]> checkMoves(int[][] environment, int row, int col) {
List<int[]> validMoves = new ArrayList<>();
int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
// Include diagonal directions
for (int[] direction : directions) {
int newRow = row + direction[1];
int newCol = col + direction[0];
if (isValidPosition(environment, newRow, newCol)) {
validMoves.add(new int[]{newCol, newRow});
}
}
return validMoves;
}
private boolean isValidPosition(int[][] environment, int row, int col) {
return row >= 0 && row < environment.length && col >= 0 && col < environment[0].length;
}
}
And here is what is being output:
Creature created at position (0, 0)
Creature created at position (5, 5)
Creature created at position (1, 3)
Creature created at position (0, 4)
Creature created at position (0, 3)
105 – C1 110 – C0 122 – C0 118 – C0 101 – C0 125 – C0 130 – C0 112 – C0
107 – C0 112 – C0 125 – C0 122 – C0 110 – C0 128 – C0 135 – C0 119 – C0
110 – C0 119 – C0 148 – C0 130 – C0 121 – C0 129 – C0 155 – C0 102 – C0
110 – C1 120 – C1 156 – C0 136 – C0 109 – C0 120 – C0 142 – C0 105 – C0
106 – C1 118 – C0 150 – C0 125 – C0 110 – C0 115 – C0 130 – C0 105 – C0
111 – C0 112 – C0 131 – C0 120 – C0 114 – C0 113 – C1 120 – C0 107 – C0
Creature created at position (0, 0)
Creature created at position (6, 4)
Creature created at position (4, 0)
Creature created at position (0, 4)
Creature created at position (4, 1)
105 – C1 110 – C0 122 – C0 118 – C0 101 – C1 125 – C0 130 – C0 112 – C0
107 – C0 112 – C0 125 – C0 122 – C0 110 – C1 128 – C0 135 – C0 119 – C0
110 – C0 119 – C0 148 – C0 130 – C0 121 – C0 129 – C0 155 – C0 102 – C0
110 – C0 120 – C0 156 – C0 136 – C0 109 – C0 120 – C0 142 – C0 105 – C0
106 – C1 118 – C0 150 – C0 125 – C0 110 – C0 115 – C0 130 – C1 105 – C0
111 – C0 112 – C0 131 – C0 120 – C0 114 – C0 113 – C0 120 – C0 107 – C0
Creature created at position (0, 0)
Creature created at position (3, 5)
Creature created at position (4, 0)
Creature created at position (0, 4)
Creature created at position (2, 4)
105 – C1 110 – C0 122 – C0 118 – C0 101 – C1 125 – C0 130 – C0 112 – C0
107 – C0 112 – C0 125 – C0 122 – C0 110 – C0 128 – C0 135 – C0 119 – C0
110 – C0 119 – C0 148 – C0 130 – C0 121 – C0 129 – C0 155 – C0 102 – C0
110 – C0 120 – C0 156 – C0 136 – C0 109 – C0 120 – C0 142 – C0 105 – C0
106 – C1 118 – C0 150 – C1 125 – C0 110 – C0 115 – C0 130 – C0 105 – C0
111 – C0 112 – C0 131 – C0 120 – C1 114 – C0 113 – C0 120 – C0 107 – C0
All though its not obvious for all of them because some are stuck in their position , you can tell by the fifth creatures position that the x and y are being swapped.
Thank you
Calder Lewis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.