Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
morri564 committed Dec 6, 2023
1 parent c03843f commit fd0b6da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
44 changes: 44 additions & 0 deletions src/main/java/team/rocket/Entities/Fox.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,48 @@ public AbstractOrganism[] findNeighbors(Map map, int y, int x) {

return neighbors;
}

public void eat(Map map, int row, int column) {
if (map.getGrid()[row][column] != null) {
AbstractOrganism org = map.getGrid()[row][column];
if (org.instancedToIcon() == 'R') {
this.hunger += org.getNutrition();
org.reduceCount();
map.removeOrganism(row, column);
}
}
}

public AbstractOrganism[] findNeighbors(Map map, int y, int x) {
AbstractOrganism[] neighbors = new AbstractOrganism[4];
if (y == 0) {
neighbors[0] = OrganismFactory.getInstance().createOrganism("Fox"); //Acting as walls
neighbors[0].reduceCount(); //Keeping the Fox count accurate
} else {
neighbors[0] = map.getOrganism(y - 1, x);
}

if (y == map.getHeight() - 1) {
neighbors[1] = OrganismFactory.getInstance().createOrganism("Fox");
neighbors[1].reduceCount();
} else {
neighbors[1] = map.getOrganism(y + 1, x);
}

if (x == 0) {
neighbors[2] = OrganismFactory.getInstance().createOrganism("Fox");
neighbors[2].reduceCount();
} else {
neighbors[2] = map.getOrganism(y, x - 1);
}

if (x == map.getWidth() - 1) {
neighbors[3] = OrganismFactory.getInstance().createOrganism("Fox");
neighbors[3].reduceCount();
} else {
neighbors[3] = map.getOrganism(y, x + 1);
}

return neighbors;
}
}
15 changes: 4 additions & 11 deletions src/main/java/team/rocket/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import team.rocket.util.TimeManager;
import java.util.*;

import java.util.Arrays;

/**
* team.rocket.Simulation is the class that controls the backend of the simulation. It contains a grid of animals. It also runs
* multiple time steps and days worth of simulated time during which animals can breed.
Expand Down Expand Up @@ -129,12 +127,6 @@ private void breed() {
Map oldMap = map; // A copy of the current map
boolean hasBred; // Indicates if the animal in the current grid space has bred yet
int rabbitsBred = 0;

// Reset the breed counter at the start of each day
if (currentTimeStep == 1) {
breedsToday = 0;
}

int randomValue;

/* Looks for an organism to breed */
Expand All @@ -157,7 +149,7 @@ private void breed() {
// Check if the random value is less than the breed chance
if (randomValue < breedChance) {
// Breed the animals in the closest available tile
int[] closestEmptyTile = findClosestEmptyTile(oldMap, i, j, maxDistance);
int[] closestEmptyTile = findClosestEmptyTile(oldMap, i, j);
if (closestEmptyTile != null) {
map.addOrganism(OrganismFactory.getInstance().createOrganism("Rabbit"), closestEmptyTile[0], closestEmptyTile[1]);
rabbitsBred++;
Expand Down Expand Up @@ -200,10 +192,11 @@ private int[] findClosestEmptyTile(Map map, int y, int x) {

// Ensure the tile is within the map but not on the boundary
if (offsetX > 0 && offsetX < map.getWidth() - 1 && offsetY > 0 && offsetY < map.getHeight() - 1 && map.getOrganism(offsetY, offsetX) == null) {
return new int[]{offsetY, offsetX}; // Correct the order of offsetY and offsetX if necessary
return new int[] {offsetY, offsetX}; // Correct the order of offsetY and offsetX if necessary
}
}
return closestEmptyTile; // instead of return new int[0];

return new int[0];
}

/**
Expand Down

0 comments on commit fd0b6da

Please sign in to comment.