Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey everyone. So this is a simulator that populates a walled grid with \"animals

ID: 3584499 • Letter: H

Question

Hey everyone. So this is a simulator that populates a walled grid with "animals." These animals move one step, or turn 90 degrees in a direction every turn. When two animals are next to each other, one is randomly picked to "infect" the other and turn it into one of its own. All of the code for the simulator is given, including the functions of the animals, given in a "Critter" class, which we need to utilize and override (if needed) using inheritance in our own animal classes. So the assignment calls for four animals to be coded by us, Bear, Lion, Giant, and one of our own (I chose Squirel). The functions of the first three are: Bear: the display image for the bear is a "/" which changes to a "" after 1 step, and then back to "/" the next step and so on. The bear class also takes on a boolean parameter that determines whether it will be generated as a "polar" bear or a black bear, meaning the initial "/" symbol will either be black or white in color, and then stay that color. Also, bears should move towards walls and once they reach a wall (edge of the grid) they move along the wall in a counter-clockwise direction. Lions: 1/3 are red, 1/3 are green, 1/3 are blue when generated. They retain these colors for the first three steps after which they switch to a different one, so for steps 0,1,2 they are the first generated color, then steps 3,4,5 they are another color and so on. Once they reach a wall, they turn an move away from it, and they shouldn't cluster around each other. Giant: the symbol for the giant is "fee" on the grid, which stays the same for the first six steps, then changes to "fie" for another six steps, then "foe" for six steps, and finally "fum" for six, then back to "fee." They move towards walls like bears but once they hit a wall, they move clockwise against it. For our own animal (Squirel), the issue is survivability. We can use inheritance to make it behave any way we want, but it will be pitted against other peoples animals and must infect and survive to win. I must add that once an animal has infected another animal, it adds +1 to a counter, which is used when it encounters another animal, after which the one with the lowest counter will get to infect the other first, if the counters are the same (0-0, 3-3, etc) then its a coin toss who gets to infect who. So basically, my issues are with the Bear and the Giant, and getting their code to work properly. Also, if anyone knows a good coding direction to go in for my Squirel's survivability, that would be much appreciated! So here is the parent class which we need to inherit from for our animals: import java.awt.*; public class Critter { public static enum Neighbor { WALL, EMPTY, SAME, OTHER }; public static enum Action { HOP, LEFT, RIGHT, INFECT }; public static enum Direction { NORTH, SOUTH, EAST, WEST }; // This method should be overriden (default action is turning left) public Action getMove(CritterInfo info) { return Action.LEFT; } // This method should be overriden (default color is black) public Color getColor() { return Color.BLACK; } // This method should be overriden (default display is "?") public String toString() { return "?"; } // This prevents critters from trying to redefine the definition of // object equality, which is important for the simulator to work properly. public final boolean equals(Object other) { return this == other; } }

Explanation / Answer

import java.util.Random; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.awt.Color; /** * A simple predator-prey simulator, based on a rectangular field * containing rabbits and foxes. * * @author David J. Barnes and Michael Kölling * @version 2011.07.31 */ public class Game { // Constants representing configuration information for the simulation. // The default width for the grid. private static final int DEFAULT_WIDTH = 12; // The default depth of the grid. private static final int DEFAULT_DEPTH = 12; // The probability that a grass will be created in any given grid position. private static final double GRASS_CREATION_PROBABILITY = 0.50; // The probability that a tree will be created in any given grid position. private static final double TREE_CREATION_PROBABILITY = 0.; // The probability that deer will be created in any given grid position. private static final double DEER_CREATION_PROBABILITY = 0 // List of vegetation on the board. private List vegetation; // List of animals on the board. private List // The current state of the field. private Board board; // The current step of the simulation. private int step; // A graphical view of the simulation. private GameViewer view; /** * Construct a simulation field with default size. */ public Game() { this(DEFAULT_DEPTH, DEFAULT_WIDTH); } /** * Create a simulation field with the given size. * @param depth Depth of the field. Must be greater than zero. * @param width Width of the field. Must be greater than zero. */ public Game(int depth, int width) { if(width