The goal for this programming project is to create a simple


The goal for this programming project is to create a simple 2D predator-prey simulation. In this simulation the prey are ants and the predators are doodlebugs. These critters live in a world composed of a 20x20 grid of cells. Only one critter may occupy a cell at a time. The grid is enclosed, so a critter is not allowed to move off the edges of the world. Time is simulated in time steps. Each critter performs some action every time step.
The ants behave according to the following model:

Move. Every time step, randomly try to move up, down, left or right. If the neighboring cell in the selected direction is occupied or would move the ant off the grid, then the ant stays in the current cell.
Breed. If an ant survives for three time steps, then at the end of the time step (i.e. after moving) the ant will breed. This is simulated by creating a new ant in an adjacent (up, down, left, or right) cell that is empty. If there is no empty cell available then no breeding occurs. Once an offspring is produced an ant cannot produce an offspring until three more time steps have elapsed.
The doodlebugs behave according to the following model:
Move. Every time step, if there is an adjacent ant (up, down, left, or right) then the doodlebug will move to that cell and eat the ant. Otherwise the doodlebug moves according to the same rules as the ant. Note that a doodlebug cannot eat other doodlebugs.
Breed. If a doodlebug survives for eight time steps, then at the end of the time step it will spawn off a new doodlebug in the same manner as the ant.
Starve. If a doodlebug has not eaten an ant within the last three time steps, then at the end of the third time step it will starve and die. The doodlebug should then be removed from the grid of cells.
During one turn, all the doodlebugs should move before the ants.
Write a program to implement this simulation and draw the world using ASCII characters of "o" for an ant and "X" for a doodlebug. Create a class named Organism that encapsulates basic data common to both ants and doodlebugs. This class should have an overridden method named move that is defined in the derived classes of Ant and Doodlebug. You may need additional data structures to keep track of which critters have moved.

Initialize the world with 5 doodlebugs and 100 ants. After each time step prompt the user to press enter to move to the next time step. You should see a cyclical pattern between the population of predators and prey, although random perturbations may lead to the elimination of one or both species.

In this program you should implement classes Ant and DoodleBug with the following constructors and methods:

class Ant extends Organism {
public Ant() { ... }
public Ant(World world, int x, int y) { ... }
public boolean starve() { ... }
public void move() { ... }
public int getType() { ... }
public void breed() { ... }
}

class DoodleBug extends Organism {
public Doodlebug() { ... }
public Doodlebug(World world, int x, int y) { ... }
public void move() { ... }
public int getType() { ... }
public void breed() { ... }
public boolean starve() { ... }
}

/**
* This program simulates a 2D world with predators and prey.
* The predators (doodlebugs) and prey (ants) inherit from the
* Organism class that keeps track of basic information about each
* critter (time ticks since last bred, position in the world).
*


* The 2D world is implemented as a separate class, World,
* that contains a 2D array of pointers to type Organism.
*


* Normally the classes would be defined in separate files, but
* they are all included here for ease of use with CodeMate.
*/
import java.util.Scanner;

public class Simulation {
public static final int WORLDSIZE = 20;
public static final int INITIALANTS = 100;
public static final int INITIALBUGS = 5;
public static final int DOODLEBUG = 1;
public static final int ANT = 2;
public static final int ANTBREED = 3;
public static final int DOODLEBREED = 8;
public static final int DOODLESTARVE = 3;

/**
* Main driver method
*/
public static void main(String[] args) {
World w = new World();

// Randomly create 100 ants
int antcount = 0;
while (antcount < INITIALANTS) {
int x = (int) (Math.random() * WORLDSIZE);
int y = (int) (Math.random() * WORLDSIZE);

// Only put ant in empty spot
if (w.getAt(x, y) == null) {
antcount++;
Ant a1 = new Ant(w, x, y);
}
}

// Randomly create 5 doodlebugs
int doodlecount = 0;
while (doodlecount < INITIALBUGS) {
int x = (int) (Math.random() * WORLDSIZE);
int y = (int) (Math.random() * WORLDSIZE);

// Only put doodlebug in empty spot
if (w.getAt(x,y) == null) {
doodlecount++;
Doodlebug d1 = new Doodlebug(w, x, y);
}
}

Scanner keyboard = new Scanner(System.in);
String s = "";

// Run simulation forever, until user cancels
while (s.length() == 0) {
w.display();
w.simulateOneStep();
System.out.println();
System.out.println(
"Press enter for next step, any key (and enter) to end");
s = keyboard.nextLine();
}
}

}

/**
* The World class stores data about the world by creating a
* WORLDSIZE by WORLDSIZE array of type Organism.
* NULL indicates an empty spot, otherwise a valid object
* indicates an ant or doodlebug. To determine which,
* invoke the virtual function getType of Organism that should return
* ANT if the class is of type ant, and DOODLEBUG otherwise.
*/
class World {
private Organism[][] grid = null;

public World() {
grid = new Organism[Simulation.WORLDSIZE][Simulation.WORLDSIZE];
}

/**
* Returns the entry stored in the grid array at x,y
*/
public Organism getAt(int x, int y) {
if ((x >= 0) && (x < Simulation.WORLDSIZE) &&
(y >= 0) && (y < Simulation.WORLDSIZE)) {
return grid[x][y];
}
return null;
}

/**
* Sets the entry at x,y to the value passed in.
*/
public void setAt(int x, int y, Organism org) {
if ((x >= 0) && (x < Simulation.WORLDSIZE) &&
(y >= 0) && (y < Simulation.WORLDSIZE)) {
grid[x][y] = org;
}
}

/**
* Displays the world in ASCII. Uses o for ant, X for doodlebug.
*/
public void display() {
System.out.println();
System.out.println();
for (int j = 0; j < Simulation.WORLDSIZE; j++) {
for (int i = 0; i < Simulation.WORLDSIZE; i++) {
if (grid[i][j] == null) {
System.out.print(".");
}
else if (grid[i][j].getType() == Simulation.ANT) {
System.out.print("o");
}
else {
System.out.print("X");
}
}
System.out.println();
}
}

/**
* This is the main routine that simulates one turn in the world.
* First, a flag for each organism is used to indicate if it has moved.
* This is because we iterate through the grid starting from the top
* looking for an organism to move . If one moves down, we don't want
* to move it again when we reach it.
*


* First move doodlebugs, then ants, and if they are still alive then
* we breed them.
*/
public void simulateOneStep() {
// First reset all organisms to not moved
for (int i = 0; i < Simulation.WORLDSIZE; i++) {
for (int j = 0; j < Simulation.WORLDSIZE; j++) {
if (grid[i][j] != null) {
grid[i][j].moved = false;
}
}
}

// Loop through cells in order and move if it's a Doodlebug
for (int i = 0; i < Simulation.WORLDSIZE; i++) {
for (int j = 0; j < Simulation.WORLDSIZE; j++) {
if ((grid[i][j] != null) &&
(grid[i][j].getType() == Simulation.DOODLEBUG)) {
if (! grid[i][j].moved) {
grid[i][j].moved = true; // Mark as moved
grid[i][j].move();
}
}
}
}

// Loop through cells in order and move if it's an Ant
for (int i = 0; i < Simulation.WORLDSIZE; i++) {
for (int j = 0; j < Simulation.WORLDSIZE; j++) {
if ((grid[i][j] != null) &&
(grid[i][j].getType() == Simulation.ANT)) {
if (! grid[i][j].moved) {
grid[i][j].moved = true; // Mark as moved
grid[i][j].move();
}
}
}
}

// Loop through cells in order and check if we should breed
for (int i = 0; i < Simulation.WORLDSIZE; i++) {
for (int j = 0; j < Simulation.WORLDSIZE; j++) {
// Kill off any doodlebugs that haven't eaten recently
if ((grid[i][j] != null) &&
(grid[i][j].getType() == Simulation.DOODLEBUG)) {
if (grid[i][j].starve()) {
grid[i][j] = null;
}
}
}
}

// Loop through cells in order and check if we should breed
for (int i = 0; i < Simulation.WORLDSIZE; i++) {
for (int j = 0; j < Simulation.WORLDSIZE; j++) {
// Only breed organisms that have moved, since
// breeding places new organisms on the map we
// don't want to try and breed those
if ((grid[i][j] != null) && (grid[i][j].moved)) {
grid[i][j].breed();
}
}
}
}

}

/**
* Definition for the Organism base class. Each organism has a reference
* back to the World object so it can move itself about in the world.
*/
abstract class Organism {
/**
* Position in the world
*/
protected int x;
protected int y;

/**
* Bool to indicate if moved this turn
*/
protected boolean moved;

/**
* Number of ticks since breeding
*/
protected int breedTicks;

protected World world;

public Organism() {
world = null;
moved = false;
breedTicks = 0;
x = 0;
y = 0;
}

public Organism(World world, int x, int y) {
this.world = world;
this.x = x;
this.y = y;
breedTicks = 0;
moved = false;
world.setAt(x, y, this);
}

public abstract void breed(); // Whether or not to breed
public abstract void move(); // Rules to move the critter
public abstract int getType(); // Return if ant or doodlebug
public abstract boolean starve(); // Determine if organism starves
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: The goal for this programming project is to create a simple
Reference No:- TGS01250020

Now Priced at $20 (50% Discount)

Recommended (98%)

Rated (4.3/5)