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

Hey everyone, I need help coding a few classes for a simple simulator. The code

ID: 3527183 • Letter: H

Question

Hey everyone, I need help coding a few classes for a simple simulator. The code for the simulator is given so I don't need to mess around with that, but what I need are the classes (or animals in this case) that populate the simulation. The simulator is a program that populates a grid with animals, each having it's own properties. The animals that we have to code are the Bear, Lion and Giant.

For the Bear, it needs to be displayed as a "/" on the grid and every step, it changes to a "" and then back again. They need to move towards walls (edge of the grid) and once they reach a wall, they move along it in a counter-clockwise fashion. Also, the Bear symbol needs to change between black and white, just as the symbol itself changes from "/" to "".

For the Lion, the symbol is an "L" and stays that way every step. When we run the simulator, 1/3 of the Lion population must be red, 1/3 must be green and 1/3 must be blue. They retain these colors for 3 steps after which they change to a random different one of the three. Also, once they reach walls, they turn and step away from them.

For the Giant, the symbol starts out as "fee" on the grid, and after six steps, it changes to "fie, then another six steps "foe" and another six "fum", then back to "fee" and so on. They also move towards walls but move in a clockwise direction once they reach the edge.


Here is the superclass that these animal classes inherit code from which we need to override and manipulate in order to have the animals do what we need them to do:


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;

}

}


Thanks a lot for all the help guys!!

Explanation / Answer

import java.awt.*; public abstract class Critter { public boolean eat() { return false; } public Attack fight(String opponent) { return Attack.FORFEIT; } public Color getColor() { return Color.BLACK; } public Direction getMove() { return Direction.CENTER; } public String toString() { return "?"; } private int x; private int y; private int width; private int height; private boolean alive = true; private boolean awake = true; private final String[] neighbors = {" ", " ", " ", " ", " "}; public static enum Direction { NORTH, SOUTH, EAST, WEST, CENTER }; public static enum Attack { ROAR, POUNCE, SCRATCH, FORFEIT }; public final int getHeight() { return height; } public final String getNeighbor(Direction direction) { return neighbors[direction.ordinal()]; } public final int getWidth() { return width; } public final int getX() { return x; } public final int getY() { return y; } public final boolean isAlive() { return alive; } public final boolean isAwake() { return awake; } public final void setAlive(boolean alive) { this.alive = alive; } public final void setAwake(boolean awake) { this.awake = awake; } public final void setHeight(int height) { this.height = height; } public final void setNeighbor(Direction direction, String value) { neighbors[direction.ordinal()] = value; } public final void setWidth(int width) { this.width = width; } public final void setX(int x) { this.x = x; } public final void setY(int y) { this.y = y; } public void win() {} public void lose() {} public void sleep() {} public void wakeup() {} public void reset() {} public void mate() {} public void mateEnd() {} }