Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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
}

Java, Programming

  • Category:- Java
  • Reference No.:- M91611582
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Java

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

Assessment instructionsin this assessment you will design

Assessment Instructions In this assessment, you will design and code a simple Java application that defines a class, instantiate the class into a number of objects, and prints out the attributes of these objects in a spe ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As