diff --git a/src/main/java/team/rocket/Entities/AbstractAnimal.java b/src/main/java/team/rocket/Entities/AbstractAnimal.java index 418826b..280b139 100644 --- a/src/main/java/team/rocket/Entities/AbstractAnimal.java +++ b/src/main/java/team/rocket/Entities/AbstractAnimal.java @@ -4,75 +4,92 @@ import team.rocket.Map; /** + * An abstract class to define characteristics and behaviors for multiple types of animals. + * + * @version 0.6.0 * @since 0.1.0 - * @version 0.4.0 */ public abstract class AbstractAnimal extends AbstractOrganism { - private static char icon; - private static int count; - private boolean hasMoved; - - private static int vision; - - private int hunger; + private static char icon; // The icon representation of this type of animal + private static int count; // The number of animals + private boolean hasMoved; // True if this animal has moved this time step, false otherwise + private static int vision; // The number of tiles away that an animal can see + private int hunger; // The hunger value for this animal /** - * @return Animal's icon as a character + * Returns the icon representation of this type of animal. + * + * @return this type of animal's icon as a character */ - public static char toIcon(){ + public static char toIcon() { return icon; } /** - * @return current Animal count + * Returns the total number of existing animals. + * + * @return the current animal count */ - public static int getCount(){ + public static int getCount() { return count; } /** - * Resets hasMoved to false, meant to be used to reset movement each day + * Resets hasMoved to false, meant to be used to reset movement each day. */ - public void resetMove(){ + public void resetMove() { this.hasMoved = false; } /** - * Creates new Animal + * Creates a new animal. */ public abstract void reproduce(); /** - * @return true if hunger is <= zero + * Returns true if this animal is starving, false otherwise. + * + * @return true if this animal is starving, false otherwise */ public abstract boolean isStarving(); /** - * @return Animal's current hunger + * Returns this animal's hunger + * + * @return this animal's current hunger */ public abstract int getHunger(); /** - * Takes array of an Animal's neighbors, randomly chooses an available space, and returns corresponding direction + * Takes array of an animal's neighbors, randomly chooses an available space, and returns corresponding direction. + * * @param neighbors array of animals in adjacent tiles, 0-3 representing UP, DOWN, LEFT, or RIGHT respectively - * @return randomly determined direction based on available spaces + * @return randomly determined direction based on available spaces */ public abstract Direction availableMovementSpace(AbstractOrganism[] neighbors); /** - * Moves Animal in grid based on current position, available movement space, and past movement - * @param map map of simulation - * @param y - y position of Rabbit in grid - * @param x - x position of Rabbit in grid + * Moves this animal in grid based on current position, available movement space, and past movement. + * + * @param map map of simulation + * @param y y position of Rabbit in grid + * @param x x position of Rabbit in grid */ public abstract void move(Map map, int y, int x); /** - * @return nutrition + * Returns this animal's nutrition. + * + * @return nutrition */ public abstract int getNutrition(); + /** + * Returns the vision for this type of animal. + * + * @return this type of animal's vision + */ public static int getVision() { return vision; } -} \ No newline at end of file +} diff --git a/src/main/java/team/rocket/Entities/AbstractOrganism.java b/src/main/java/team/rocket/Entities/AbstractOrganism.java index 3f50dc0..dffa2b9 100644 --- a/src/main/java/team/rocket/Entities/AbstractOrganism.java +++ b/src/main/java/team/rocket/Entities/AbstractOrganism.java @@ -1,55 +1,65 @@ package team.rocket.Entities; /** + * An abstract class to define characteristics and behaviors for multiple types of organisms. + * + * @version 0.6.0 * @since 0.1.0 - * @version 0.4.0 */ public abstract class AbstractOrganism { - private static char icon; - private static int count; - private static int nutrition; + private static char icon; // The icon representation of this type of organism + private static int count; // The total number of existing organisms + private static int nutrition; // The hunger value that this type of organism rewards when eaten /** - * @return Organism's icon as a character + * Returns the icon representation for this type of organism. + * + * @return this type of organism's icon as a character */ - public static char toIcon(){ + public static char toIcon() { return icon; } /** - * gets the icon from an instance - * @return the icon of the organism + * Gets the icon from an instance. + * + * @return the icon of the organism */ abstract public char instancedToIcon(); /** - * @return current Organism count + * Returns the total number of existing organisms + * + * @return current organism count */ - public static int getCount(){ + public static int getCount() { return count; } /** - * Sets the count of animals + * Sets the count of organisms. + * * @param i the number count is being set too */ public abstract void setCount(int i); - /** - * Needed for very specific instance with OrganismEnum so that the instance in the enum doesn't count towards the total number of Organisms + * Needed for very specific instance with OrganismEnum so that the instance in the enum doesn't count towards the + * total number of organisms. */ public abstract void reduceCount(); /** - * Takes the instance of an object and creates a brand new one and returns that new object - * @return a fresh new not-copied AbstractOrganism + * Takes the instance of an object and creates a brand new one and returns that new object. + * + * @return a fresh new not-copied abstract organism */ public abstract AbstractOrganism getNewObjectFromExistingObject(); /** + * Returns the hunger that this type of organism rewards when eaten + * * @return nutrition */ public abstract int getNutrition(); - } diff --git a/src/main/java/team/rocket/Entities/AbstractPlant.java b/src/main/java/team/rocket/Entities/AbstractPlant.java index 96f08f3..8c828b4 100644 --- a/src/main/java/team/rocket/Entities/AbstractPlant.java +++ b/src/main/java/team/rocket/Entities/AbstractPlant.java @@ -1,68 +1,80 @@ package team.rocket.Entities; /** + * An abstract class to define characteristics and behaviors for multiple types of plants. + * + * @version 0.6.0 * @since 0.3.0 - * @version 0.4.0 */ -public abstract class AbstractPlant extends AbstractOrganism{ - private static char icon; - private static int count; - private boolean hasGrown; - private static int nutrition; - +public abstract class AbstractPlant extends AbstractOrganism { + private static char icon; // The icon representation of this type of plant + private static int count; // The total number of existing plants + private boolean hasGrown; // True if this organism has grown today, false otherwise + private static int nutrition; // The hunger value that this type of plant rewards when eaten /** - * @return Plant's icon as a character + * Returns an icon representation of this type of plant. + * + * @return this type of plant's icon as a character */ - public static char toIcon(){ + public static char toIcon() { return icon; } /** - * gets the icon from an instance - * @return the icon of the organism + * Gets the icon from an instance. + * + * @return the icon of the organism */ - public char instancedToIcon(){return icon;} + public char instancedToIcon() { + return icon; + } /** - * @return current Plant count + * Returns the total number of existing plants. + * + * @return current plant count */ public static int getCount(){ return count; } /** - * Sets the count of Plants + * Sets the count of plants. + * * @param i the number count is being set too */ public abstract void setCount(int i); /** - * Needed for very specific instance with OrganismEnum so that the instance in the enum doesn't count towards the total number of Organisms + * Needed for very specific instance with OrganismEnum so that the instance in the enum doesn't count towards the + * total number of organisms. */ public abstract void reduceCount(); /** - * Takes the instance of an object and creates a brand new one and returns that new object - * @return a fresh new not-copied AbstractOrganism + * Takes the instance of an object and creates a brand new one and returns that new object. + * + * @return a fresh new not-copied AbstractOrganism */ public abstract AbstractOrganism getNewObjectFromExistingObject(); /** - * Resets hasGrown to false, meant to be used to reset growth each day + * Resets hasGrown to false, meant to be used to reset growth each day. */ - public void resetGrown(){ + public void resetGrown() { this.hasGrown = false; } /** - * Creates new Organism + * Creates a new plant. */ - public abstract void grow(AbstractOrganism grid[][], AbstractOrganism[] neighbors, int y, int x); + public abstract void grow(AbstractOrganism[][] grid, AbstractOrganism[] neighbors, int y, int x); /** - * @return nutrition + * Returns this plant's nutrition. + * + * @return nutrition */ public abstract int getNutrition(); - } diff --git a/src/main/java/team/rocket/Entities/Carrot.java b/src/main/java/team/rocket/Entities/Carrot.java index 6a4a8c5..eb3c360 100644 --- a/src/main/java/team/rocket/Entities/Carrot.java +++ b/src/main/java/team/rocket/Entities/Carrot.java @@ -6,99 +6,116 @@ import java.util.Random; /** + * A carrot is a plant that provides a moderate amount of nutrition to rabbits. + * + * @version 0.6.0 * @since 0.5.0 - * @version 0.5.0 */ public class Carrot extends AbstractPlant { - private static char icon = 'C'; - private static int nutrition = 25; - private static int count; - private boolean hasGrown; + private static char icon = 'C'; // The icon representation of a carrot + private static int nutrition = 25; // The hunger value that a carrot rewards when eaten + private static int count; // The total number of existing carrots + private boolean hasGrown; // True is this carrot has grown today, false otherwise /** - * Creates new Carrot, not ready to grow + * Creates a new carrot, not ready to grow. */ - public Carrot(){ + public Carrot() { count++; hasGrown = true; } /** - * @return boolean indication if Carrot has grown this cycle + * Returns a boolean indication if this carrot has grown this cycle. + * + * @return true if this carrot has grown this cycle, false otherwise */ - public boolean growthStatus(){ - return this.hasGrown; + public boolean growthStatus() { + return hasGrown; } /** - * @return Carrot's icon as a character + * Returns the icon for a carrot + * + * @return carrot's icon as a character. */ - public static char toIcon(){ + public static char toIcon() { return icon; } /** - * gets the icon from an instance + * Gets the icon from an instance. + * * @return the icon of the organism */ - public char instancedToIcon(){return icon;} + public char instancedToIcon() { + return icon; + } /** - * @return current Carrot count + * Returns the total number of existing carrots. + * + * @return current carrot count */ - public static int getCount(){ + public static int getCount() { return count; } /** - * @return Carrot nutrition + * Returns the nutrition of this carrot. + * + * @return carrot nutrition */ - public int getNutrition(){ + public int getNutrition() { return nutrition; } /** - * Sets the count of Carrot - * @param i the number count is being set too + * Sets the count of Carrot. + * + * @param i the number count is being set to */ public void setCount(int i) { count = i; } /** - * Needed for very specific instance with OrganismEnum so that the instance in the enum doesn't count towards the total number of Organisms + * Needed for very specific instance with OrganismEnum so that the instance in the enum doesn't count towards the + * total number of organisms. */ public void reduceCount() { count--; } /** - * Takes the instance of an object and creates a brand new one and returns that new object + * Takes the instance of an object and creates a brand new one and returns that new object. + * * @return a fresh new not-copied AbstractOrganism */ - public AbstractOrganism getNewObjectFromExistingObject(){ + public AbstractOrganism getNewObjectFromExistingObject() { return new Carrot(); } /** - * Resets hasGrown to false, meant to be used to reset growth each day + * Resets hasGrown to false, meant to be used to reset growth each day. */ - public void resetGrown(){ - this.hasGrown = false; + public void resetGrown() { + hasGrown = false; } /** - * Takes array of a team.rocket.Entities.Rabbit's neighbors, randomly chooses an available space, and returns corresponding direction + * Takes array of a rabbit's neighbors, randomly chooses an available space, and returns corresponding direction. + * * @param neighbors array of animals in adjacent tiles, 0-3 representing UP, DOWN, LEFT, or RIGHT respectively - * @return randomly determined direction based on available spaces + * @return randomly determined direction based on available spaces */ - public Direction availableSpace(AbstractOrganism[] neighbors){ + public Direction availableSpace(AbstractOrganism[] neighbors) { int i = 0; //tracks iterations of for loop int freeSpaceCount = 0; //stores number of free adjacent spaces Direction[] freeSpaces = new Direction[4]; //stores available movement directions - for(i = 0; i < 4; i++){ - if(neighbors[i] == null){ + for (i = 0; i < 4; i++) { + if (neighbors[i] == null) { switch (i) { //identifies which direction is being evaluated case 0 -> { freeSpaces[freeSpaceCount] = Direction.UP; //stores open direction in freeSpaces @@ -120,30 +137,31 @@ public Direction availableSpace(AbstractOrganism[] neighbors){ } } - if(freeSpaceCount==0){ //returns null in case of no free spaces + if (freeSpaceCount == 0) { //returns null in case of no free spaces return null; } - if(freeSpaceCount==1){ + if (freeSpaceCount == 1) { return freeSpaces[0]; - } - else{ - return freeSpaces[RandomManager.getRandom().nextInt(freeSpaceCount)]; //randomly picks and returns a free space + } else { + // Randomly picks and returns a free space + return freeSpaces[RandomManager.getRandom().nextInt(freeSpaceCount)]; } } /** - * Creates new Carrot in free adjacent slot - * @param grid 2D array holding all Organisms in simulation + * Creates a new carrot in free adjacent slot. + * + * @param grid 2D array holding all Organisms in simulation * @param neighbors array of animals in adjacent tiles, 0-3 representing UP, DOWN, LEFT, or RIGHT respectively - * @param y - y position of Carrot in grid - * @param x - x position of Carrot in grid + * @param y y position of Carrot in grid + * @param x x position of Carrot in grid */ - public void grow(AbstractOrganism grid[][], AbstractOrganism[] neighbors, int y, int x) { + public void grow(AbstractOrganism[][] grid, AbstractOrganism[] neighbors, int y, int x) { if (hasGrown) { return; } - Direction direction = this.availableSpace(neighbors); + Direction direction = availableSpace(neighbors); if (direction == null) { return; @@ -164,6 +182,7 @@ public void grow(AbstractOrganism grid[][], AbstractOrganism[] neighbors, int y, if (direction == Direction.RIGHT) { grid[y][x+1] = getNewObjectFromExistingObject(); } + hasGrown = true; } } diff --git a/src/main/java/team/rocket/Entities/Fox.java b/src/main/java/team/rocket/Entities/Fox.java index 2590c8c..b8fd0d6 100644 --- a/src/main/java/team/rocket/Entities/Fox.java +++ b/src/main/java/team/rocket/Entities/Fox.java @@ -6,114 +6,154 @@ import java.util.Random; /** + * A fox is an animal that eats rabbits. + * + * @version 0.6.0 * @since 0.4.0 - * @version 0.4.0 */ public class Fox extends AbstractAnimal { - private static final char icon = 'F'; - private static int count = 0; - private boolean hasMoved; - private boolean hasBred; - private int hunger; - private int nutrition = 0; - private static final int vision = 5; - - public Fox(){ + private static final char icon = 'F'; // The icon representation of a fox + private static int count = 0; // The total number of existing foxes + private boolean hasMoved; // True if this fox has moved this time step, false otherwise + private boolean hasBred; // True if this fox has bred today, false otherwise + private int hunger; // The hunger value of this fox + private int nutrition = 0; // The hunger value that a fox rewards when eaten + private static final int vision = 5; // The number of tiles away that a fox can see + + /** + * A default constructor for foxes. + */ + public Fox() { count++; hasMoved = true; hasBred = true; - hunger = 100; //100 is full, 0 is empty + hunger = 100; // 100 is full, 0 is empty } /** - * gets the icon from an instance - * @return the icon of the organism + * Gets the icon from an instance. + * + * @return the icon of the organism */ - public char instancedToIcon(){return icon;} + public char instancedToIcon() + { + return icon; + } /** - * @return team.rocket.Entities.Fox's icon as a character + * Returns the icon representation of a fox. + * + * @return the icon representation of a fox */ - public static char toIcon(){ + public static char toIcon() { return icon; } /** - * @return current Fox count + * Returns the total number of existing foxes. + * + * @return current fox count */ - public static int getCount(){ + public static int getCount() { return count; } /** - * @return Fox's current hunger + * Returns this fox's hunger value. + * + * @return fox's current hunger */ public int getHunger() { return hunger; } /** - * @return Fox nutrition + * Returns the nutrition of a fox. + * + * @return fox nutrition */ public int getNutrition() { return nutrition; } /** - * decreases Fox's hunger meter + * Decreases this fox's hunger meter. */ - public void reduceHunger(){ - hunger-=10; + public void reduceHunger() { + hunger -= 10; } + /** + * Returns whether this fox is starving. + * + * @return true if this fox is starving, false otherwise + */ public boolean isStarving() { return hunger <= 0; } + /** + * Sets the total number of existing foxes. + * + * @param i the number count is being set too + */ @Override public void setCount(int i) { count = i; } - + /** + * Decrements the count of foxes. + */ @Override public void reduceCount() { count--; } + /** + * Returns a new fox using an existing fox. + * + * @return a new fox + */ @Override public AbstractOrganism getNewObjectFromExistingObject() { return new Fox(); } /** - * Creates new Fox + * Creates a new fox. */ - public void breed(){} //not yet implemented + public void breed() { + + } //not yet implemented /** - * Resets hasMoved to false, meant to be used to reset movement each day + * Resets hasMoved to false, meant to be used to reset movement each day. */ - public void resetMove(){ + public void resetMove() { hasMoved = false; } + /** + * Reproduces and creates a new fox. + */ @Override public void reproduce() { } /** - * Resets hasBred to false, meant to be used to reset breeding each day + * Resets hasBred to false, meant to be used to reset breeding each day. */ - public void resetBreeding(){ + public void resetBreeding() { hasBred = false; } /** - * Takes array of a team.rocket.Entities.Fox's neighbors, randomly chooses an available space, and returns corresponding direction + * Takes array of a fox's neighbors, randomly chooses an available space, and returns corresponding direction. + * * @param neighbors array of organisms in adjacent tiles, 0-3 representing UP, DOWN, LEFT, or RIGHT respectively - * @return randomly determined direction based on available spaces + * @return randomly determined direction based on available spaces */ public Direction availableMovementSpace(AbstractOrganism[] neighbors) { int i = 0; //tracks iterations of for loop @@ -143,23 +183,22 @@ public Direction availableMovementSpace(AbstractOrganism[] neighbors) { } } - if(freeSpaceCount==0){ //returns null in case of no free spaces + if (freeSpaceCount == 0) { //returns null in case of no free spaces return null; } - if(freeSpaceCount==1){ + if (freeSpaceCount == 1) { return freeSpaces[0]; - } - else{ + } else { return freeSpaces[new Random().nextInt(freeSpaceCount)]; //randomly picks and returns a free space } } /** - * Moves Fox in grid based on current position, available movement space, and past movement + * Moves fox in grid based on current position, available movement space, and past movement. * - * @param map map of simulation - * @param y - y position of Fox in grid - * @param x - x position of Fox in grid + * @param map map of simulation + * @param y y position of Fox in grid + * @param x x position of Fox in grid */ public void move(Map map, int y, int x) { if (hasMoved) { @@ -201,10 +240,22 @@ public void move(Map map, int y, int x) { hasMoved = true; } + /** + * Returns the vision of a fox + * + * @return the vision of a fox + */ public static int getVision() { return vision; } + /** + * Causes this fox to eat the organism in the given row and column of the given map. + * + * @param map the map that this fox is in + * @param row the row of the organism that this fox will eat + * @param column the column of the organism that this fox will eat + */ public void eat(Map map, int row, int column) { if (map.getGrid()[row][column] != null) { AbstractOrganism org = map.getGrid()[row][column]; @@ -216,6 +267,14 @@ public void eat(Map map, int row, int column) { } } + /** + * Finds the contents of the four squares up, down, left, and right from this fox in the given map. + * + * @param map the map that this fox is in + * @param y the y value of this fox + * @param x the x value of this fox + * @return the contents of the four squares around this fox + */ public AbstractOrganism[] findNeighbors(Map map, int y, int x) { AbstractOrganism[] neighbors = new AbstractOrganism[4]; if (y == 0) { diff --git a/src/main/java/team/rocket/Entities/Grass.java b/src/main/java/team/rocket/Entities/Grass.java index 81ce096..be905fd 100644 --- a/src/main/java/team/rocket/Entities/Grass.java +++ b/src/main/java/team/rocket/Entities/Grass.java @@ -6,59 +6,73 @@ import java.util.Random; /** + * A patch of grass is a plant that provides a small amount of food to a rabbit + * + * @version 0.6.0 * @since 0.3.0 - * @version 0.4.0 */ public class Grass extends AbstractPlant { - private static char icon = 'G'; - private static int nutrition = 15; - private static int count; - private boolean hasGrown; + private static char icon = 'G'; // The icon representation of grass + private static int nutrition = 15; // The hunger value that grass rewards when eaten + private static int count; // The total number of existing patches of grass + private boolean hasGrown; // True if this patch of grass has grown today, false otherwise /** - * Creates new grass, not ready to grow + * Creates new grass, not ready to grow. */ - public Grass(){ + public Grass() { count++; hasGrown = true; } /** + * Returns whether this patch of grass has grown today. + * * @return boolean indication if grass has grown this cycle */ - public boolean growthStatus(){ - return this.hasGrown; + public boolean growthStatus() { + return hasGrown; } /** + * Returns the icon representation of grass. + * * @return Grass's icon as a character */ - public static char toIcon(){ + public static char toIcon() { return icon; } /** - * gets the icon from an instance + * Gets the icon from an instance. + * * @return the icon of the organism */ - public char instancedToIcon(){return icon;} + public char instancedToIcon() { + return icon; + } /** + * Returns the total number of existing patches of grass. + * * @return current Grass count */ - public static int getCount(){ + public static int getCount() { return count; } /** - * @return Grass nutrition + * The hunger value that grass rewards when eaten. + * + * @return grass nutrition */ - public int getNutrition(){ + public int getNutrition() { return nutrition; } /** - * Sets the count of Grass + * Sets the count of grass. + * * @param i the number count is being set too */ public void setCount(int i) { @@ -66,39 +80,43 @@ public void setCount(int i) { } /** - * Needed for very specific instance with OrganismEnum so that the instance in the enum doesn't count towards the total number of Organisms + * Needed for very specific instance with OrganismEnum so that the instance in the enum doesn't count towards the + * total number of organisms. */ public void reduceCount() { count--; } /** - * Takes the instance of an object and creates a brand new one and returns that new object - * @return a fresh new not-copied AbstractOrganism + * Takes the instance of an object and creates a brand new one and returns that new object. + * + * @return a fresh new not-copied AbstractOrganism */ - public AbstractOrganism getNewObjectFromExistingObject(){ + public AbstractOrganism getNewObjectFromExistingObject() { return new Grass(); } /** - * Resets hasGrown to false, meant to be used to reset growth each day + * Resets hasGrown to false, meant to be used to reset growth each day. */ - public void resetGrown(){ - this.hasGrown = false; + public void resetGrown() { + hasGrown = false; } /** - * Takes array of a team.rocket.Entities.Rabbit's neighbors, randomly chooses an available space, and returns corresponding direction + * Takes array of a patch of grass' neighbors, randomly chooses an available space, and returns corresponding + * direction. + * * @param neighbors array of animals in adjacent tiles, 0-3 representing UP, DOWN, LEFT, or RIGHT respectively - * @return randomly determined direction based on available spaces + * @return randomly determined direction based on available spaces */ - public Direction availableSpace(AbstractOrganism[] neighbors){ + public Direction availableSpace(AbstractOrganism[] neighbors) { int i = 0; //tracks iterations of for loop int freeSpaceCount = 0; //stores number of free adjacent spaces Direction[] freeSpaces = new Direction[4]; //stores available movement directions - for(i = 0; i < 4; i++){ - if(neighbors[i] == null){ + for (i = 0; i < 4; i++) { + if (neighbors[i] == null) { switch (i) { //identifies which direction is being evaluated case 0 -> { freeSpaces[freeSpaceCount] = Direction.UP; //stores open direction in freeSpaces @@ -120,25 +138,25 @@ public Direction availableSpace(AbstractOrganism[] neighbors){ } } - if(freeSpaceCount==0){ //returns null in case of no free spaces + if (freeSpaceCount == 0) { //returns null in case of no free spaces return null; } - if(freeSpaceCount==1){ + if (freeSpaceCount == 1) { return freeSpaces[0]; - } - else{ + } else { return freeSpaces[RandomManager.getRandom().nextInt(freeSpaceCount)]; //randomly picks and returns a free space } } /** - * Creates new Grass in free adjacent slot - * @param grid 2D array holding all Organisms in simulation + * Creates new Grass in free adjacent slot. + * + * @param grid 2D array holding all Organisms in simulation * @param neighbors array of animals in adjacent tiles, 0-3 representing UP, DOWN, LEFT, or RIGHT respectively - * @param y - y position of Grass in grid - * @param x - x position of Grass in grid + * @param y y position of Grass in grid + * @param x x position of Grass in grid */ - public void grow(AbstractOrganism grid[][], AbstractOrganism[] neighbors, int y, int x) { + public void grow(AbstractOrganism[][] grid, AbstractOrganism[] neighbors, int y, int x) { if (hasGrown) { return; } diff --git a/src/main/java/team/rocket/Entities/OrganismFactory.java b/src/main/java/team/rocket/Entities/OrganismFactory.java index 6837620..cb1820c 100644 --- a/src/main/java/team/rocket/Entities/OrganismFactory.java +++ b/src/main/java/team/rocket/Entities/OrganismFactory.java @@ -3,10 +3,11 @@ import java.util.HashMap; /** - * Imitating Singleton - * Creates new objects of organisms after they've been registered + * Imitating Singleton. Creates new objects of organisms after + * they've been registered. + * + * @version 0.6.0 * @since 0.3.0 - * @version 0.3.0 */ public class OrganismFactory { private static OrganismFactory singleton; @@ -14,41 +15,43 @@ public class OrganismFactory { private OrganismFactory(){} /** - * Singleton creator - * @return The single instance of the factory + * Singleton creator. + * + * @return the single instance of the factory */ public static synchronized OrganismFactory getInstance(){ - if(singleton == null){ + if (singleton == null) { singleton = new OrganismFactory(); } + return singleton; } /** - * Registers an organism to the internal Hashmap - * reduces organism count to 0 - * @param OrganismName String name of the organism - * @param organism A new object of the organism + * Registers an organism to the internal Hashmap. Reduces organism count to 0. + * + * @param OrganismName string name of the organism + * @param organism a new object of the organism */ - public void registerOrganism (String OrganismName, AbstractOrganism organism){ + public void registerOrganism (String OrganismName, AbstractOrganism organism) { OrganismName = OrganismName.toLowerCase(); - if(!m_RegisteredOrganisms.containsKey(OrganismName)){ + if (!m_RegisteredOrganisms.containsKey(OrganismName)) { m_RegisteredOrganisms.put(OrganismName, organism); organism.setCount(0); } } /** - * Creates a new instance of the organism with the id - * @param OrganismName Organism to get the instance of - * @return null if organism doesn't exist/wasn't registered, otherwise an object of AbstractOrganism type + * Creates a new instance of the organism with the id. + * + * @param OrganismName organism to get the instance of + * @return null if organism doesn't exist/wasn't registered, otherwise an object of AbstractOrganism type */ public AbstractOrganism createOrganism (String OrganismName){ OrganismName = OrganismName.toLowerCase(); - if(!m_RegisteredOrganisms.containsKey(OrganismName)){ + if (!m_RegisteredOrganisms.containsKey(OrganismName)) { return null; } return m_RegisteredOrganisms.get(OrganismName).getNewObjectFromExistingObject(); } - } diff --git a/src/main/java/team/rocket/Entities/Rabbit.java b/src/main/java/team/rocket/Entities/Rabbit.java index 6329a8b..544501e 100644 --- a/src/main/java/team/rocket/Entities/Rabbit.java +++ b/src/main/java/team/rocket/Entities/Rabbit.java @@ -6,44 +6,55 @@ import java.util.Random; /** + * A rabbit is an animal that provides food to foxes and eats plants. * + * @version 0.6.0 * @since 0.1.0 - * @version 0.4.0 */ public class Rabbit extends AbstractAnimal{ - private static final char icon = 'R'; - private static int count = 0; - private boolean hasMoved; - private boolean hasBred; - private int hunger; - private static int deathFood = 0; - private static int nutrition = 20; - private static final int vision = 4; - - public Rabbit(){ + private static final char icon = 'R'; // The icon representation of a rabbit + private static int count = 0; // The total number of existing rabbits + private boolean hasMoved; // True if this rabbit has moved this time step, false otherwise + private boolean hasBred; // True if this rabbit has bred today, false otherwise + private int hunger; // The hunger value of this rabbit + private static int deathFood = 0; // This rabbit will die if its hunger falls below this value + private static int nutrition = 20; // The hunger value that a rabbit rewards when eaten + private static final int vision = 4; // The number of tiles away a rabbit can see + + /** + * A default constructor for rabbits. + */ + public Rabbit() { count++; hasMoved = true; hasBred = true; - hunger = 100; //100 is full, 0 is empty + hunger = 100; // 100 is full, 0 is empty } /** - * @return team.rocket.Entities.Rabbit's icon as a character + * Returns the icon representation of a rabbit. + * + * @return rabbit's icon as a character */ - public static char toIcon(){ + public static char toIcon() { return icon; } /** - * gets the icon from an instance + * Gets the icon from an instance. + * * @return the icon of the organism */ - public char instancedToIcon(){return icon;} + public char instancedToIcon() { + return icon; + } /** + * Returns the total number of existing rabbits. + * * @return current Rabbit count */ - public static int getCount(){ + public static int getCount() { return count; } @@ -53,40 +64,54 @@ public void setCount(int i) { } /** + * Returns the hunger value that a rabbit rewards when eaten + * * @return Rabbit nutrition */ - public int getNutrition(){ + public int getNutrition() { return nutrition; } /** - * decreases Rabbit's hunger meter + * Decreases rabbit's hunger meter. */ - public void reduceHunger(){ - hunger-=10; + public void reduceHunger() { + hunger -= 10; } /** - * @return Rabbit's current hunger + * Returns this rabbit's hunger value. + * + * @return rabbit's current hunger */ public int getHunger() { return hunger; } + /** + * Decrements the count of rabbits. + */ @Override public void reduceCount() { count--; } + /** + * Returns a new rabbit using an existing rabbit + * + * @return a new rabbit + */ @Override public AbstractOrganism getNewObjectFromExistingObject() { return new Rabbit(); } /** - * Creates new Rabbit + * Creates a new rabbit */ - public void reproduce(){} //not yet implemented + public void reproduce() { + + } //not yet implemented /** * Resets hasMoved to false, meant to be used to reset movement each day @@ -96,17 +121,18 @@ public void resetMove(){ } /** - * Takes array of a team.rocket.Entities.Rabbit's neighbors, randomly chooses an available space, and returns corresponding direction + * Takes array of a rabbit's neighbors, randomly chooses an available space, and returns corresponding direction + * * @param neighbors array of animals in adjacent tiles, 0-3 representing UP, DOWN, LEFT, or RIGHT respectively - * @return randomly determined direction based on available spaces + * @return randomly determined direction based on available spaces */ public Direction availableMovementSpace(AbstractOrganism[] neighbors){ int i = 0; //tracks iterations of for loop int freeSpaceCount = 0; //stores number of free adjacent spaces Direction[] freeSpaces = new Direction[4]; //stores available movement directions - for(i = 0; i < 4; i++){ - if(neighbors[i] == null || neighbors[i].instancedToIcon() == 'G' || neighbors[i].instancedToIcon() == 'C'){ + for (i = 0; i < 4; i++) { + if(neighbors[i] == null || neighbors[i].instancedToIcon() == 'G' || neighbors[i].instancedToIcon() == 'C') { switch (i) { //identifies which direction is being evaluated case 0 -> { freeSpaces[freeSpaceCount] = Direction.UP; //stores open direction in freeSpaces @@ -128,22 +154,22 @@ public Direction availableMovementSpace(AbstractOrganism[] neighbors){ } } - if(freeSpaceCount==0){ //returns null in case of no free spaces + if (freeSpaceCount==0) { //returns null in case of no free spaces return null; } - if(freeSpaceCount==1){ + if (freeSpaceCount == 1) { return freeSpaces[0]; - } - else{ + } else { return freeSpaces[new Random().nextInt(freeSpaceCount)]; //randomly picks and returns a free space } } /** * Moves Rabbit in grid based on current position, available movement space, and past movement - * @param map map of simulation - * @param y - y position of Rabbit in grid - * @param x - x position of Rabbit in grid + * + * @param map map of simulation + * @param y y position of this rabbit in grid + * @param x x position of this rabbit in grid */ public void move(Map map, int y, int x) { if (hasMoved) { @@ -185,10 +211,21 @@ public void move(Map map, int y, int x) { hasMoved = true; } + /** + * Returns whether this rabbit is starving + * @return true if this rabbit is starving, false otherwise + */ public boolean isStarving() { return hunger <= deathFood; } + /** + * Causes this rabbit to eat the organism in the given row and column of the given map. + * + * @param map the map that this fox is in + * @param row the row of the organism that this fox will eat + * @param column the column of the organism that this fox will eat + */ public void eat(Map map, int row, int column) { if (map.getGrid()[row][column] != null) { AbstractOrganism org = map.getGrid()[row][column]; @@ -200,6 +237,14 @@ public void eat(Map map, int row, int column) { } } + /** + * Finds the contents of the four squares up, down, left, and right from this rabbit in the given map. + * + * @param map the map that this rabbit is in + * @param y the y value of this rabbit + * @param x the x value of this rabbit + * @return the contents of the four squares around this rabbit + */ public AbstractOrganism[] findNeighbors(Map map, int y, int x) { AbstractOrganism[] neighbors = new AbstractOrganism[4]; if (y == 0) { @@ -233,6 +278,11 @@ public AbstractOrganism[] findNeighbors(Map map, int y, int x) { return neighbors; } + /** + * Returns the number of tiles away a rabbit can see + * + * @return the vision value of a rabbit + */ public static int getVision() { return vision; } diff --git a/src/main/java/team/rocket/Map.java b/src/main/java/team/rocket/Map.java index ecd3cc6..b4d8f1e 100644 --- a/src/main/java/team/rocket/Map.java +++ b/src/main/java/team/rocket/Map.java @@ -5,38 +5,36 @@ import team.rocket.Enums.Direction; /** - * A team.rocket.Map contains the information about the arrangement of a set of simulated organisms. + * A Map contains the information about the arrangement of a set of simulated organisms. * - * @author Dale Morris - * @version 0.2.0 - * @since 0.6.0 + * @version 0.6.0 + * @since 0.2.0 */ public class Map { private AbstractOrganism[][] grid; // The 2D array containing all the organisms on the map private int width; // The width of the map private int height; // The height of the map - public static final int DEFAULT_WIDTH = 5; // The default value for the width of the grid - public static final int DEFAULT_HEIGHT = 5; // The default value for the height of the grid - - private long numberOfOrganisms; + private long numberOfOrganisms; // The total number of organisms in this map + public static final int DEFAULT_WIDTH = 5; // The default value for the width of the map + public static final int DEFAULT_HEIGHT = 5; // The default value for the height of the map /** - * A constructor for the team.rocket.Map class that sets the grid to an empty grid of default size and sets the - * width and height to their default values + * A constructor for the map class that sets the grid to an empty grid of default size and sets the + * width and height to their default values. */ public Map() { grid = new AbstractOrganism[DEFAULT_HEIGHT][DEFAULT_WIDTH]; width = DEFAULT_WIDTH; height = DEFAULT_HEIGHT; - this.numberOfOrganisms = 0; + numberOfOrganisms = 0; } /** - * A constructor for the team.rocket.Map class that sets the grid to an empty 2D array of AbstractOrganisms with a + * A constructor for the Map class that sets the grid to an empty 2D array of AbstractOrganisms with a * given width and height. * - * @param width The desired width (number of columns) of the grid - * @param height The desired height (number of rows) of the grid + * @param width The desired width (number of columns) of the grid + * @param height The desired height (number of rows) of the grid */ public Map(int width, int height) { if (width == 0 || height == 0) { @@ -44,142 +42,145 @@ public Map(int width, int height) { } else { grid = new AbstractOrganism[height][width]; } + this.width = width; this.height = height; - this.numberOfOrganisms = 0; - + numberOfOrganisms = 0; } /** - * A constructor for the team.rocket.Map class that sets the grid to the given grid and sets the width and height + * A constructor for the Map class that sets the grid to the given grid and sets the width and height * to the width and height of the grid. * - * @param grid A 2D array of AbstractOrganisms for the map to set its grid to + * @param grid A 2D array of AbstractOrganisms for the map to set its grid to */ public Map(AbstractOrganism[][] grid) { this.grid = grid; width = grid[0].length; height = grid.length; - for(int w = 0; w < width; w++){ + for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++){ - if(grid[h][w] != null) numberOfOrganisms++; + if (grid[h][w] != null) { + numberOfOrganisms++; + } } } } /** - * Returns the grid of the team.rocket.Map. + * Returns the grid of this map. * - * @return A 2D array containing the simulated AbstractOrganisms + * @return A 2D array containing the simulated AbstractOrganisms */ public AbstractOrganism[][] getGrid() { return grid; } /** - * Sets the grid of the team.rocket.Map to the given grid and changes the width and height values if applicable. + * Sets the grid of the map to the given grid and changes the width and height values if applicable. * - * @param grid The desired grid of the team.rocket.Map. + * @param grid The desired grid of the map. */ public void setGrid(AbstractOrganism[][] grid) { this.grid = grid; width = grid[0].length; height = grid.length; - for(int w = 0; w < width; w++){ + for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++){ - if(grid[h][w] != null) numberOfOrganisms++; + if (grid[h][w] != null) { + numberOfOrganisms++; + } } } } /** - * Returns the width of the grid of the team.rocket.Map. + * Returns the width of the map. * - * @return an int representing the width of the grid of the team.rocket.Map + * @return an int representing the width of the grid of the map */ public int getWidth() { return width; } /** - * Returns the height of the grid of the team.rocket.Map. + * Returns the height of the grid of the map. * - * @return an int representing the height of the grid of the team.rocket.Map + * @return an int representing the height of the grid of the map */ public int getHeight() { return height; } /** - * Gets the organism at the specified location + * Gets the organism at the specified location. * - * @param row the row that the organism is in - * @param column the column that the organism is in - * @return AbstractOrganism or null if the location is empty + * @param row the row that the organism is in + * @param column the column that the organism is in + * @return AbstractOrganism or null if the location is empty */ public AbstractOrganism getOrganism(int row, int column) { return grid[row][column]; } /** - * Adds an organism to the specified location + * Adds an organism to the specified location. * - * @param organism the organism that is to be added to the map - * @param row the row that the organism will be in - * @param column the column that the organism will be in + * @param organism the organism that is to be added to the map + * @param row the row that the organism will be in + * @param column the column that the organism will be in */ public void addOrganism(AbstractOrganism organism, int row, int column) { - //If no organism is in the space then numberOfOrganisms increases - if(grid[row][column] == null){ + if (grid[row][column] == null) { this.numberOfOrganisms++; } + grid[row][column] = organism; } /** - * Removes the organism at the specified location + * Removes the organism at the specified location. * - * @param row the row of the organism that is to be removed from the map - * @param column the column of the organism that is to be removed from the map + * @param row the row of the organism that is to be removed from the map + * @param column the column of the organism that is to be removed from the map */ public void removeOrganism(int row, int column) { - //If there's an organism in the space then numberOfOrganisms decreases - if(grid[row][column] != null){ + if (grid[row][column] != null) { this.numberOfOrganisms--; } - grid[row][column] = null; + grid[row][column] = null; } /** - * Returns a boolean telling whether the map is empty + * Returns a boolean telling whether the map is empty. * * @return true if the map is empty, false otherwise */ public boolean isEmpty() { - return this.numberOfOrganisms==0; + return numberOfOrganisms == 0; } /** - * Returns a boolean telling whether the map is full + * Returns a boolean telling whether the map is full. * * @return true if the map is full, false otherwise */ public boolean isFull() { - return this.numberOfOrganisms == (long) width * height; + return numberOfOrganisms == (long) width * height; } /** - * Gets the neighbors of the position (x,y) - * if a neighbor is a wall then that entry isn't included. - * @param x the x coordinate of the position to be checked - * @param y the y coordinate of the position to be checked - * @return a HashMap with entries of the neighbors with the Directions as keys + * Gets the neighbors of the position (x,y). If a neighbor is a wall then that entry isn't included. + * + * @param x the x coordinate of the position to be checked + * @param y the y coordinate of the position to be checked + * @return a HashMap with entries of the neighbors with the Directions as keys */ - public HashMap getNeighbors(int x, int y){ - if(x < 0 || x >= width){ + public HashMap getNeighbors(int x, int y) { + if (x < 0 || x >= width) { throw new IllegalArgumentException("x is outside bounds [0,%d].".formatted(width)); } else if (y < 0 || y >= height){ throw new IllegalArgumentException("y is outside bounds [0,%d].".formatted(height)); @@ -187,16 +188,16 @@ public HashMap getNeighbors(int x, int y){ HashMap hashMap = new HashMap<>(); - if(y-1 >= 0){ + if (y-1 >= 0) { hashMap.put(Direction.UP, grid[y-1][x]); } - if (y+1 < height){ + if (y+1 < height) { hashMap.put(Direction.DOWN, grid[y+1][x]); } - if (x-1 >= 0){ + if (x-1 >= 0) { hashMap.put(Direction.LEFT, grid[y][x-1]); } - if (x+1 < width){ + if (x+1 < width) { hashMap.put(Direction.RIGHT, grid[y][x+1]); } @@ -204,14 +205,14 @@ public HashMap getNeighbors(int x, int y){ } /** - * Gets the neighbors of the position (x,y) as characters - * If a neighbor is a wall, then that entry isn't included. + * Gets the neighbors of the position (x,y) as characters. If a neighbor is a wall, then that entry isn't included. + * * @param x the x coordinate of the position to be checked * @param y the y coordinate of the position to be checked * @return a HashMap of with entries of the neighbors with directions as keys */ - public HashMap getNeighborsAsCharacter(int x, int y){ - if(x < 0 || x >= width){ + public HashMap getNeighborsAsCharacter(int x, int y) { + if (x < 0 || x >= width) { throw new IllegalArgumentException("x is outside bounds [0,%d].".formatted(width)); } else if (y < 0 || y >= height){ throw new IllegalArgumentException("y is outside bounds [0,%d].".formatted(height)); @@ -220,24 +221,32 @@ public HashMap getNeighborsAsCharacter(int x, int y){ HashMap returnArrayList = new HashMap<>(); char icon; - if(y-1 >= 0){ + if (y-1 >= 0) { icon = ' '; - if(grid[y-1][x]!=null) icon = grid[y-1][x].instancedToIcon(); + if (grid[y-1][x]!=null) { + icon = grid[y-1][x].instancedToIcon(); + } returnArrayList.put(Direction.UP, icon); } - if (y+1 < height){ + if (y+1 < height) { icon = ' '; - if(grid[y+1][x]!=null) icon = grid[y+1][x].instancedToIcon(); + if (grid[y+1][x]!=null) { + icon = grid[y+1][x].instancedToIcon(); + } returnArrayList.put(Direction.DOWN, icon); } - if (x-1 >= 0){ + if (x-1 >= 0) { icon = ' '; - if(grid[y][x-1]!=null) icon = grid[y][x-1].instancedToIcon(); + if (grid[y][x-1]!=null) { + icon = grid[y][x-1].instancedToIcon(); + } returnArrayList.put(Direction.LEFT, icon); } - if (x+1 < width){ + if (x+1 < width) { icon = ' '; - if(grid[y][x+1]!=null) icon = grid[y][x+1].instancedToIcon(); + if (grid[y][x+1]!=null) { + icon = grid[y][x+1].instancedToIcon(); + } returnArrayList.put(Direction.RIGHT, icon); } @@ -245,7 +254,8 @@ public HashMap getNeighborsAsCharacter(int x, int y){ } /** - * Gets the number of entities held by the map + * Gets the number of entities held by the map. + * * @return a long representing the number of entities held by the map */ public long getNumberOfOrganisms() { @@ -253,16 +263,23 @@ public long getNumberOfOrganisms() { } /** - * Moves an organism at (currentCol, currentRow) to (newCol, newRow) and removes the organism at that location if one is there + * Moves an organism at (currentCol, currentRow) to (newCol, newRow) and removes the organism at that location if + * one is there. + * * @param currentRow the current x position of the organism * @param currentCol the current y position of the organism * @param newRow the x destination of the organism * @param newCol the y destination of the organism * @throws UnsupportedOperationException when the value at (currentCol, currentRow) is null */ - public void moveOrganism(int currentRow, int currentCol, int newRow, int newCol){ - if(grid[currentRow][currentCol] == null) throw new UnsupportedOperationException("Tried to move a null value to a new position."); - if(grid[newRow][newCol]!=null) numberOfOrganisms--; + public void moveOrganism(int currentRow, int currentCol, int newRow, int newCol) { + if (grid[currentRow][currentCol] == null) { + throw new UnsupportedOperationException("Tried to move a null value to a new position."); + } + if (grid[newRow][newCol] != null) { + numberOfOrganisms--; + } + grid[newRow][newCol] = grid[currentRow][currentCol]; grid[currentRow][currentCol] = null; } diff --git a/src/main/java/team/rocket/Simulation.java b/src/main/java/team/rocket/Simulation.java index 69b0a7a..b4b17dc 100644 --- a/src/main/java/team/rocket/Simulation.java +++ b/src/main/java/team/rocket/Simulation.java @@ -11,18 +11,11 @@ import team.rocket.util.TimeManager; import java.util.Arrays; -/* -import team.rocket.IO.Terminal.FlagHandler; -import team.rocket.IO.Terminal.GridSizeFlagHandler; -import team.rocket.IO.Terminal.InitialOrganismCountFlagHandler; -import team.rocket.IO.Terminal.TerminalFlagRequest; - */ /** - * team.rocket.Simulation is the class that controls the backend of the simulation. It contains a grid of animals. It also runs + * 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. * - * @author Dale Morris, Jon Roberts * @version 0.6.0 * @since 0.1.0 */ @@ -36,15 +29,11 @@ public class Simulation implements Runnable { private int daysPerRun; // The number of days that make up each run of the simulation private int timeStepsPerDay; // The number of time steps that make up each day private int millisecondsPerTimeStep; // The number of real-world milliseconds that make up each time step - // private FlagHandler flagHandler = new GridSizeFlagHandler(); private boolean mapIsFull = false; private int breedChance = 25; //the % chance that two animals will breed - private boolean printOutput = true; - - //Holds all of the useful one-off offsets - private static final int[][] offsetArray = { + private static final int[][] offsetArray = { //Holds all of the useful one-off offsets {1, 1}, {1, 0}, {1, -1}, @@ -56,10 +45,10 @@ public class Simulation implements Runnable { }; /** - * Returns a new team.rocket.Simulation object with the given constraints. + * Returns a new Simulation object with the given constraints. * - * @param mapWidth the number of columns of the simulated grid - * @param mapHeight the number of rows of the simulated grid + * @param mapWidth the number of columns of the simulated grid + * @param mapHeight the number of rows of the simulated grid */ public Simulation(int mapWidth, int mapHeight) { map = new Map(mapWidth, mapHeight); @@ -67,8 +56,7 @@ public Simulation(int mapWidth, int mapHeight) { /** - * Returns a new team.rocket.Simulation object with default constraints. - * Contains one rabbit in the corner by default + * Returns a new Simulation object with default constraints. Contains one rabbit in the corner by default. */ public Simulation() { map = new Map(); @@ -79,6 +67,7 @@ public Simulation() { /** * Creates a simulation from a map + * * @param m the map to create a simulation from */ public Simulation(Map m){ @@ -86,7 +75,7 @@ public Simulation(Map m){ } /** - * Simulates how the environment changes over time based on initial conditions and interactions among animals. + * Simulates how the environment changes over time based on initial conditions and interactions among organisms. */ @Override public void run() { @@ -97,21 +86,22 @@ public void run() { for (currentTimeStep = 1; currentTimeStep <= timeStepsPerDay; currentTimeStep++) { // Iterates through each time step in the current day moveAnimal(); - } // End of current day + } - currentTimeStep--; //For loop increments past the stopping step, this fixes that error + currentTimeStep--; // For loop increments past the stopping step, this fixes that error breed(); if (mapIsFull) { return; } - //print out map - if(printOutput) UI.outputGrid(currentDay, map); + + if (printOutput) { + UI.outputGrid(currentDay, map); + } long currentTime = TimeManager.getCurrentTime(); - //Only sleep if computation time didn't take long enough - if(currentTime < startTime + millisecondsPerDay){ + if (currentTime < startTime + millisecondsPerDay) { // Only sleep if computation time didn't take long enough try { Thread.sleep(startTime + millisecondsPerDay-currentTime); @@ -127,8 +117,8 @@ public void run() { } /** - * Simulates breeding among the animals and creates a new entity when breeding occurs - * There is a chance that the animals breed based on the breedChance variable + * Simulates breeding among the animals and creates a new entity when breeding occurs. There is a chance that the + * animals breed based on the breedChance variable. */ private void breed() { int maxDistance = 4; // Maximum distance from parent for new animal to spawn @@ -189,12 +179,13 @@ private void breed() { } /** - * finds the closest empty tile to position y, x in the grid - * only searchs within 1 tile orthogonally and diagonally - * @param map the map to search - * @param y the center y position - * @param x the center x position - * @return an array with the y position then the x position + * Finds the closest empty tile to position y, x in the grid. Only searches within 1 tile orthogonally and + * diagonally. + * + * @param map the map to search + * @param y the center y position + * @param x the center x position + * @return an array with the y position then the x position */ private int[] findClosestEmptyTile(Map map, int y, int x, int maxDistance) { int[] closestEmptyTile = null; @@ -215,7 +206,7 @@ private int[] findClosestEmptyTile(Map map, int y, int x, int maxDistance) { } /** - * Handles animal movement as the days progress + * Handles animal movement as the days progress. */ private void moveAnimal() { for (int i = 0; i < map.getHeight(); i++) { // Iterates through each row of the grid @@ -230,6 +221,7 @@ private void moveAnimal() { } } } + for (int i = 0; i < map.getHeight(); i++) { // Iterates through each row of the grid for (int j = 0; j < map.getWidth(); j++) { // Iterates through each column of the grid if (map.getOrganism(i, j) instanceof AbstractAnimal animal) { // Check if the object is an instance of AbstractAnimal @@ -240,18 +232,18 @@ private void moveAnimal() { } /** - * Determines the movement positions of animals up or dowm and left or right + * Determines the movement positions of animals up or down and left or right. * - * @param animal The animal being moved - * @param neighbors The surrounding animals + * @param animal the animal being moved + * @param neighbors the surrounding animals * @param y the vertical movement of the grid * @param x the horizontal movement of the grid */ private void moveDirection(AbstractAnimal animal, AbstractOrganism[] neighbors, int y, int x) { Direction direction = animal.availableMovementSpace(neighbors); - if(direction!=null){ - switch(direction) { + if (direction != null) { + switch (direction) { case UP -> { map.removeOrganism(y, x); map.addOrganism(animal, y - 1, x); @@ -273,31 +265,64 @@ private void moveDirection(AbstractAnimal animal, AbstractOrganism[] neighbors, } } + /** + * Sets the number of days in each run of the simulation to the given number. + * + * @param daysPerRun the desired number of days in each run of the simulation + */ public void setDaysPerRun(int daysPerRun) { this.daysPerRun = daysPerRun; } + /** + * Sets the number of time steps in each day to the given number. + * + * @param timeStepsPerDay the desired number of time steps in each day + */ public void setTimeStepsPerDay(int timeStepsPerDay) { this.timeStepsPerDay = timeStepsPerDay; } + /** + * Sets the time in milliseconds that each time step lasts to the given number. + * + * @param millisecondsPerTimeStep the desired number of milliseconds in each time step + */ public void setMillisecondsPerTimeStep(int millisecondsPerTimeStep) { this.millisecondsPerTimeStep = millisecondsPerTimeStep; } + /** + * Returns the current day of the simulation. + * + * @return the current day of the simulation + */ public int getCurrentDay() { return currentDay; } + /** + * Returns the current time step of the simulation. + * + * @return the current time step of the simulation + */ public int getCurrentTimeStep() { return currentTimeStep; } - public Map getMap(){return map; } + /** + * Returns the map of the simulation. + * + * @return the map of the simulation + */ + public Map getMap() { + return map; + } /** - * Sets whether the simulation should print it's output - * @param printOutput if true it will send the signals to the UI, otherwise it won't + * Sets whether the simulation should print it's output. + * + * @param printOutput if true it will send the signals to the UI, otherwise it won't */ public void setPrintOutput(boolean printOutput) { this.printOutput = printOutput;