SlideShare a Scribd company logo
1 of 19
Download to read offline
you will use cellular automata to create a 2D predator–prey simulation in your program. The
preys are ants and the predators are doodlebugs. These critters live in a 20 * 20 grid of cells.
Only one critter may occupy a cell at a time. The grid is enclosed and no critter may move off
the grid. Time is simulated in steps. Each critter performs some action every time step. The ants
behave according to the following model: Move: For every time step, the ants 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 (not been eaten by doodlebugs), 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 randomly. If there is no empty cell available, no breeding occurs.
Once an offspring is produced, an ant cannot produce an offspring again until it has survived
three more time steps.
The doodlebugs behave according to the following model:
Move: For every time step, the doodlebug will firstly try to move to an adjacent cell containing
an ant and eat the ant (you can decide if there are several ants in the adjacent cells, how the
doodlebug will choose to move). If there are no ants in adjacent cells, 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, 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 three time steps, at the end of the third time
step it will starve and die. The doodlebug should then be removed from the grid of cells.
Initialize the world with 5 doodlebugs and 100 ants. You will randomly place them. 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. You will prompt the user to
enter the number of time steps to run.
Create a class named Critter that contains data and functions common to ants and doodlebugs.
This class should have a virtual function named move that is defined in the derived classes of
Ant and Doodlebug. Each class will be in its own source file.
For each time step, do the following in your program: after moves, when breeding, eating, and
starving are resolved, display the resulting grid. Draw the world using ASCII characters of “O”
for an ant, “X” for a doodlebug and “E” for an empty space (the characters should be arranged to
look like a grid). The doodlebugs will move before the ants in each time step. When you reach
the time steps entered by the user, ask them to enter another number and start to run the
simulation again or to exit the program. You must maintain the state of the current grid while
creating the next display. You will use a dynamic array to represent the grid. Each array element
will be a pointer to a Critter. Get your program running and tested. For debugging your program,
you should save the random placement until you have everything else working properly. In
general, “random” is bad for testing and debugging.
Solution
World.java
import java.util.*;
import java.awt.*;
public class World{ private static Random r= new Random(); private int doodlebugs; private int
ants; private static final int SIZE = 20; private Critter[][] oGrid; World(int d,int a){ oGrid = new
Critter[SIZE][SIZE]; doodlebugs = d; ants = a; for(int i = 0; i < d; i++){ boolean isOccupied =
true; while(isOccupied){ int potX = r.nextInt(SIZE); int potY = r.nextInt(SIZE);
if(oGrid[potX][potY] == null) { isOccupied = false; oGrid[potX][potY] = new
Doodlebug(this,potX,potY); } } } for(int i = 0; i < a; i++){ boolean isOccupied = true;
while(isOccupied){ int potX = r.nextInt(SIZE); int potY = r.nextInt(SIZE); if(oGrid[potX][potY]
== null) { isOccupied = false; oGrid[potX][potY] = new Ant(this,potX,potY); } } } } public int
getdoodlebugs(){ return doodlebugs; } public int getants(){ return ants; } public char[][]
getCGrid(){ char[][] grid = new char[SIZE][SIZE]; for(int i = 0; i < SIZE; i++){ for(int j = 0;j <
SIZE; j++){ if(oGrid[j][i]!=null) if(oGrid[j][i].getClass().getName().equals("Ant")) Grid[j][i] =
'o'; else if(oGrid[j][i].getClass().getName().equals("Doodlebug")) grid[j][i] = 'x'; else
if(oGrid[j][i].getClass().getName().equals("Empty"))
grid[j][i] = 'e';
else
grid[j][i] = ' ';
}
}
return grid;
}
public void increasedoodlebugs(){
doodlebugs++;
}
public void decreasedoodlebugs(){
doodlebugs--;
}
public void increaseants(){
ants++;
}
public void decreaseants(){
ants--;
}
public void moveAll(){
for(int i = 0; i < SIZE; i++){
for(int j = 0;j < SIZE; j++){
if(oGrid[j][i] !=null)
oGrid[j][i].move();
}
}
for(int i = 0; i < SIZE; i++){
for(int j = 0;j < SIZE; j++){
if(oGrid[j][i] !=null)
oGrid[j][i].moved=false;
}
}
}
public void breedAll(){
for(int i = 0; i < SIZE; i++){
for(int j = 0;j < SIZE; j++){
if(oGrid[j][i]!=null)
oGrid[j][i].breed();
}
}
}
public void starveAll(){
for(int i = 0; i < SIZE; i++){
for(int j = 0;j < SIZE; j++){
if(oGrid[j][i]!=null)
oGrid[j][i].starve();
}
}
}
public boolean isEmpty(int x,int y){
boolean tbr = false;
if(oGrid[x][y]==null)
tbr = true;
return tbr;
}
public boolean isAnt(int x,int y){
boolean tbr = false;
if(oGrid[x][y] instanceof Ant)
tbr = true;
return tbr;
}
public void setCell(int x, int y, Critter O){
oGrid[x][y] = O;
}
}
Ant.java
import java.awt.*;
import java.io.*;
import java.util.*;
public class Ant extends Critter{
private static Random r = new Random();
private int untilBreed;
public Ant(World world,int _x, int _y){
super(world,_x, _y );
untilBreed = 3;
moved = false;//defaults to TRUE so that 'child' ants dont move
}
public void move(){
if(!moved){
int[] potMoves = new int[4];
int i = 1;
potMoves[0] = r.nextInt(4);
do{
do{
potMoves[i] = r.nextInt(4);
}
while(potMoves[i]!=potMoves[i-1]);
i++;
}
while(i<4);
i = 0;
boolean empty = false;
int oldx = x;
int oldy = y;
while(i<4 && !empty){
switch(potMoves[i]){
case 0:
if(x!=0){
if(world.isEmpty(x-1,y)){
empty = true;
world.setCell(x-1,y,this);
world.setCell(oldx,y,null);
moved = true;
untilBreed--;
x=x-1;
}
}
else if(world.isEmpty(19,y)){
empty = true; untilBreed--; world.setCell(19,y,this); world.setCell(oldx,y,null); moved = =19;
}
break;
case 1:
if(x!=19){
if(world.isEmpty(x+1,y)){
untilBreed--;
world.setCell(x+1,y,this);
world.setCell(oldx,y,null);
moved = true;
empty = true;
x=x+1;
}
}
else if(world.isEmpty(0,y)){
world.setCell(0,y,this);
world.setCell(oldx,y,null);
moved = true;
empty = true;
x=0;
untilBreed--;
}
break;
case 2:
if(y!=0){
if(world.isEmpty(x,y-1)){
world.setCell(x,y-1,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=y-1;
untilBreed--;
}
}
else if(world.isEmpty(x,19)){
world.setCell(x,19,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=19;
untilBreed--;
}
break;
case 3:
if(y!=19){
if(world.isEmpty(x,y+1)){
world.setCell(x,y+1,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=y+1;
untilBreed--;
}
}
else if(world.isEmpty(x,0)){
world.setCell(x,0,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=0;
untilBreed--;
}
break;
}
i++;
}
}
}
public void breed(){
if(untilBreed==0){
int[] potMoves = new int[4];
int i = 1;
potMoves[0] = r.nextInt(4);
do{
do{
potMoves[i] = r.nextInt(4);
}
while(potMoves[i]!=potMoves[i-1]);
i++;
}
while(i<4);
i = 0;
boolean empty = false;
int oldx = x;
int oldy = y;
while(i<4 && !empty){
switch(potMoves[i]){
case 0:
if(x!=0){
if(world.isEmpty(x-1,y)){
world.setCell(x-1,y,new Ant(world, x-1,y));
untilBreed = 3;
empty = true;
}
}
else if(world.isEmpty(19,y)){
world.setCell(19,y,new Ant(world, 19,y));
untilBreed = 3;
empty = true;
}
break;
case 1:
if(x!=19){
if(world.isEmpty(x+1,y)){
world.setCell(x+1,y,new Ant(world, x+1,y));
untilBreed=3;
empty = true;
}
}
else if(world.isEmpty(0,y)){
world.setCell(0,y,new Ant(world, 0,y));
untilBreed=3;
empty = true;
}
break;
case 2:
if(y!=0){
if(world.isEmpty(x,y-1)){
world.setCell(x,y-1,new Ant(world, x,y-1));
untilBreed=3;
empty = true;
}
}
else if(world.isEmpty(x,19)){
world.setCell(x,19,new Ant(world, x,19));
untilBreed=3;
empty = true;
}
break;
case 3:
if(y!=19){
if(world.isEmpty(x,y+1)){
world.setCell(x,y+1,new Ant(world, x,y+1));
untilBreed=3;
empty = true;
}
}
else if(world.isEmpty(x,0)){
world.setCell(x,0,new Ant(world, x,0));
untilBreed=3;
empty = true;
}
break;
}
i++;
}
}
}
}
Grid.java
import javax.swing.*;
import java.awt.*;
public class Grid extends JPanel {
private int rows;
private int cols;
private JLabel[][] grid;
public Grid(int r, int c) {
rows = r;
cols = c;
grid = new JLabel[rows][cols];
setLayout (new GridLayout(rows, cols, 0, 0));
setBackground( new Color( 200,200,200 ) );
for (int i = 0; i < rows; i++ ) {
for (int j = 0; j < cols; j++ ) {
grid[i][j] = new JLabel();
grid[i][j].setBorder( BorderFactory.createLineBorder(Color.BLACK,1) );
grid[i][j].setFont( new Font("Courier", Font.BOLD, 12));
grid[i][j].setForeground(Color.WHITE);
grid[i][j].setHorizontalAlignment(SwingConstants.CENTER);
add( grid[i][j] );
}
}
setPreferredSize( new Dimension(500,500) );
}
public void setGrid(int i, int j, char value) {
if (value == '.')
value = ' ';
grid[i][j].setText( "" + value );
switch (value) {
case 'o':
grid[i][j].setBackground( Color.MAGENTA );
grid[i][j].setOpaque(true);
break;
case 'x':
grid[i][j].setBackground( Color.BLUE );
grid[i][j].setOpaque(true);
break;
case 'S':
grid[i][j].setBackground( Color.RED );
grid[i][j].setOpaque(true);
break;
default:
grid[i][j].setOpaque(false);
break;
}
}
}
DisplayFrame.Java
import javax.swing.*;
import java.util.*;
public class DisplayFrame extends JFrame {
private Grid grid;
public DisplayFrame(String title, int x, int y) {
setTitle( title );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
grid = new Grid(y, x);
add( grid );
pack();
setVisible( true );
}
public void setGrid( char [][] data ) {
for (int i = 0; i < data.length; i++ ) {
for (int j = 0; j < data[i].length; j++ ) {
grid.setGrid( i, j, data[i][j] );
}
}
}
}
Prog.java
Import java.util.*;
import java.awt.*;
import java.io.*;
import java.lang.*;
public class Prog{
public static Scanner s = new Scanner(System.in);
public static void main(String[] args){
World world = new World(5,100);
DisplayFrame disp = new DisplayFrame("Pred/Prey", 20, 20 );
disp.setGrid( world.getCGrid() );
String input = "1";
int timesteps = 1;
boolean playing = true;
while(playing){
System.out.println("Options: [ENTER] Last timestep.");
System.out.println(" [Q] Quit Simulation.");
System.out.println(" [n] Enter another number.");
input = s.nextLine();
if(input.equals("Q")||input.equals("q")){
playing = false;
continue;
}
else if(input.equals("")){
timestep(timesteps,world);
}
else if(isNum(input)){
timesteps = Integer.parseInt(input);
timestep(timesteps,world);
}
else{
System.out.println("Try again!");
continue;
}
disp.setGrid( world.getCGrid() );
}
}
public static void timestep(int timesteps, World w){
for(int i = timesteps; i > 0; i--){
w.moveAll();
w.breedAll();
w.starveAll();
}
}
public static boolean isNum(String s){
try{
Integer.parseInt(s);
} catch(NumberFormatException nfe) {
return false;
}
return true;
}
}
Doodlebug.java
import java.awt.*;
import java.io.*;
import java.util.*;
public class Doodlebug extends Critter{
private static Random r = new Random();
private int untilBreed;
private int untilStarve;
public Doodlebug( World world,int _x, int _y){
super(world, _x, _y );
untilBreed = 8;
untilStarve = 3;
moved = true;
}
public void move(){
if(!moved){
int[] potMoves = new int[4];
int i = 1;
potMoves[0] = r.nextInt(4);
do{
do{
potMoves[i] = r.nextInt(4);
}
while(potMoves[i]!=potMoves[i-1]);
i++;
}
while(i<4);
i = 0;
boolean empty = false;
int oldx = x;
int oldy = y;
while(i<4 && !empty){
switch(potMoves[i]){
case 0:
if(x!=0){
if(world.isEmpty(x-1,y)){
empty = true;
world.setCell(x-1,y,this);
world.setCell(oldx,y,null);
moved = true;
untilBreed--;
untilStarve--;
x=x-1;
}
}
else if(world.isEmpty(19,y)){
empty = true;
untilBreed--;
untilStarve--;
world.setCell(19,y,this);
world.setCell(oldx,y,null);
moved = true;
x=19;
}
break;
case 1:
if(x!=19){
if(world.isEmpty(x+1,y)){
untilBreed--;
untilStarve--;
world.setCell(x+1,y,this);
world.setCell(oldx,y,null);
moved = true;
empty = true;
x=x+1;
}
}
else if(world.isEmpty(0,y)){
world.setCell(0,y,this);
world.setCell(oldx,y,null);
moved = true;
empty = true;
x=0;
untilBreed--;
untilStarve--;
}
break;
case 2:
if(y!=0){
if(world.isEmpty(x,y-1)){
world.setCell(x,y-1,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=y-1;
untilBreed--;
untilStarve--;
}
}
else if(world.isEmpty(x,19)){
world.setCell(x,19,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=19;
untilBreed--;
untilStarve--;
}
break;
case 3:
if(y!=19){
if(world.isEmpty(x,y+1)){
world.setCell(x,y+1,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=y+1;
untilBreed--;
untilStarve--;
}
}
else if(world.isEmpty(x,0)){
world.setCell(x,0,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=0;
untilBreed--;
untilStarve--;
}
break;
}
i++;
}
i= 0;
while(i<4 && !empty){
switch(potMoves[i]){
case 0:
if(x!=0){
if(world.isAnt(x-1,y)){
empty = true;
world.setCell(x-1,y,this);
world.setCell(oldx,y,null);
moved = true;
untilBreed--;
untilStarve=3;
x=x-1;
}
}
else if(world.isAnt(19,y)){
empty = true;
untilBreed--;
untilStarve=3;
world.setCell(19,y,this);
world.setCell(oldx,y,null);
moved = true;
x=19;
}
break;
case 1:
if(x!=19){
if(world.isAnt(x+1,y)){
untilBreed--;
untilStarve=3;
world.setCell(x+1,y,this);
world.setCell(oldx,y,null);
moved = true;
empty = true;
x=x+1;
}
}
else if(world.isAnt(0,y)){
world.setCell(0,y,this);
world.setCell(oldx,y,null);
moved = true;
empty = true;
x=0;
untilBreed--;
untilStarve=3;
}
break;
case 2:
if(y!=0){
if(world.isAnt(x,y-1)){
world.setCell(x,y-1,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=y-1;
untilBreed--;
untilStarve=3;
}
}
else if(world.isAnt(x,19)){
world.setCell(x,19,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=19;
untilBreed--;
untilStarve=3;
}
break;
case 3:
if(y!=19){
if(world.isAnt(x,y+1)){
world.setCell(x,y+1,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=y+1;
untilBreed--;
untilStarve=3;
}
}
else if(world.isAnt(x,0)){
world.setCell(x,y+1,this);
world.setCell(x,oldy,null);
moved = true;
empty = true;
y=0;
untilBreed--;
untilStarve=3;
}
break;
}
i++;
}
}
}

More Related Content

Similar to you will use cellular automata to create a 2D predator–prey simulati.pdf

42: Rise of the dependent types
42: Rise of the dependent types42: Rise of the dependent types
42: Rise of the dependent typesGeorge Leontiev
 
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdf
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdfTriangleU210.javapublic class TriangleU210 {    Declaring inst.pdf
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdfanuradhasilks
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Cody Engel
 
In a galaxy far, far away - A procedural generation tale
In a galaxy far, far away - A procedural generation taleIn a galaxy far, far away - A procedural generation tale
In a galaxy far, far away - A procedural generation taleShay Davidson
 
#includeiostream #includetime.h #includecstdio #include.pdf
#includeiostream #includetime.h #includecstdio #include.pdf#includeiostream #includetime.h #includecstdio #include.pdf
#includeiostream #includetime.h #includecstdio #include.pdfARORACOCKERY2111
 
Autonomous agents with deep reinforcement learning - Oredev 2018
Autonomous agents with deep reinforcement learning - Oredev 2018Autonomous agents with deep reinforcement learning - Oredev 2018
Autonomous agents with deep reinforcement learning - Oredev 2018Ali Kheyrollahi
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05cKaz Yoshikawa
 
import java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfimport java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfanwarsadath111
 
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfneed help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfarcotstarsports
 
Find the shortest route through a maze using linking and linked list.pdf
Find the shortest route through a maze using linking and linked list.pdfFind the shortest route through a maze using linking and linked list.pdf
Find the shortest route through a maze using linking and linked list.pdfsanuoptical
 
Java Question help needed In the program Fill the Add statements.pdf
Java Question  help needed In the program Fill the Add statements.pdfJava Question  help needed In the program Fill the Add statements.pdf
Java Question help needed In the program Fill the Add statements.pdfkamdinrossihoungma74
 
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdfganisyedtrd
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner codeMite Mitreski
 
Hi there I am having difficulty in finalizing my Tetris game , below.pdf
Hi there I am having difficulty in finalizing my Tetris game , below.pdfHi there I am having difficulty in finalizing my Tetris game , below.pdf
Hi there I am having difficulty in finalizing my Tetris game , below.pdffonecomp
 
I am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdfI am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdffortmdu
 

Similar to you will use cellular automata to create a 2D predator–prey simulati.pdf (20)

42: Rise of the dependent types
42: Rise of the dependent types42: Rise of the dependent types
42: Rise of the dependent types
 
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdf
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdfTriangleU210.javapublic class TriangleU210 {    Declaring inst.pdf
TriangleU210.javapublic class TriangleU210 {    Declaring inst.pdf
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)
 
In a galaxy far, far away - A procedural generation tale
In a galaxy far, far away - A procedural generation taleIn a galaxy far, far away - A procedural generation tale
In a galaxy far, far away - A procedural generation tale
 
#includeiostream #includetime.h #includecstdio #include.pdf
#includeiostream #includetime.h #includecstdio #include.pdf#includeiostream #includetime.h #includecstdio #include.pdf
#includeiostream #includetime.h #includecstdio #include.pdf
 
tetris
tetristetris
tetris
 
Autonomous agents with deep reinforcement learning - Oredev 2018
Autonomous agents with deep reinforcement learning - Oredev 2018Autonomous agents with deep reinforcement learning - Oredev 2018
Autonomous agents with deep reinforcement learning - Oredev 2018
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
import java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfimport java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdf
 
Lezione03
Lezione03Lezione03
Lezione03
 
Lezione03
Lezione03Lezione03
Lezione03
 
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfneed help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdf
 
Find the shortest route through a maze using linking and linked list.pdf
Find the shortest route through a maze using linking and linked list.pdfFind the shortest route through a maze using linking and linked list.pdf
Find the shortest route through a maze using linking and linked list.pdf
 
Java Question help needed In the program Fill the Add statements.pdf
Java Question  help needed In the program Fill the Add statements.pdfJava Question  help needed In the program Fill the Add statements.pdf
Java Question help needed In the program Fill the Add statements.pdf
 
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner code
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
libGDX: Tiled Maps
libGDX: Tiled MapslibGDX: Tiled Maps
libGDX: Tiled Maps
 
Hi there I am having difficulty in finalizing my Tetris game , below.pdf
Hi there I am having difficulty in finalizing my Tetris game , below.pdfHi there I am having difficulty in finalizing my Tetris game , below.pdf
Hi there I am having difficulty in finalizing my Tetris game , below.pdf
 
I am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdfI am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdf
 

More from FashionBoutiquedelhi

Questions1) How does multimedia transmission differ than messag.pdf
Questions1) How does multimedia transmission differ than messag.pdfQuestions1) How does multimedia transmission differ than messag.pdf
Questions1) How does multimedia transmission differ than messag.pdfFashionBoutiquedelhi
 
Question 1 of 10 Map sapling learning Based on the chart to the right.pdf
Question 1 of 10 Map sapling learning Based on the chart to the right.pdfQuestion 1 of 10 Map sapling learning Based on the chart to the right.pdf
Question 1 of 10 Map sapling learning Based on the chart to the right.pdfFashionBoutiquedelhi
 
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdfplease send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdfFashionBoutiquedelhi
 
On January 1, 2002, Germany officially adopted the euro as its curre.pdf
On January 1, 2002, Germany officially adopted the euro as its curre.pdfOn January 1, 2002, Germany officially adopted the euro as its curre.pdf
On January 1, 2002, Germany officially adopted the euro as its curre.pdfFashionBoutiquedelhi
 
Networking problem help Consider the following TCP session between h.pdf
Networking problem help Consider the following TCP session between h.pdfNetworking problem help Consider the following TCP session between h.pdf
Networking problem help Consider the following TCP session between h.pdfFashionBoutiquedelhi
 
Lipid bilayers can only be synthesized by biological organisms. This .pdf
Lipid bilayers can only be synthesized by biological organisms. This .pdfLipid bilayers can only be synthesized by biological organisms. This .pdf
Lipid bilayers can only be synthesized by biological organisms. This .pdfFashionBoutiquedelhi
 
Is a neutral trait (meaning it is neither deleterious nor advantageo.pdf
Is a neutral trait (meaning it is neither deleterious nor advantageo.pdfIs a neutral trait (meaning it is neither deleterious nor advantageo.pdf
Is a neutral trait (meaning it is neither deleterious nor advantageo.pdfFashionBoutiquedelhi
 
Let T R^n rightarrow R^m be a linear transformation. Given a subspa.pdf
Let T  R^n rightarrow R^m be a linear transformation. Given a subspa.pdfLet T  R^n rightarrow R^m be a linear transformation. Given a subspa.pdf
Let T R^n rightarrow R^m be a linear transformation. Given a subspa.pdfFashionBoutiquedelhi
 
Just a general question on some vocab words I cannot piece together .pdf
Just a general question on some vocab words I cannot piece together .pdfJust a general question on some vocab words I cannot piece together .pdf
Just a general question on some vocab words I cannot piece together .pdfFashionBoutiquedelhi
 
In order to be useful to managers, management accounting reports shou.pdf
In order to be useful to managers, management accounting reports shou.pdfIn order to be useful to managers, management accounting reports shou.pdf
In order to be useful to managers, management accounting reports shou.pdfFashionBoutiquedelhi
 
Im also confused on question 9 and 10 On which point(s) would Charle.pdf
Im also confused on question 9 and 10 On which point(s) would Charle.pdfIm also confused on question 9 and 10 On which point(s) would Charle.pdf
Im also confused on question 9 and 10 On which point(s) would Charle.pdfFashionBoutiquedelhi
 
In about six billion years, our suns luminosity (a.k.a. power outpu.pdf
In about six billion years, our suns luminosity (a.k.a. power outpu.pdfIn about six billion years, our suns luminosity (a.k.a. power outpu.pdf
In about six billion years, our suns luminosity (a.k.a. power outpu.pdfFashionBoutiquedelhi
 
I have to write a java program . So basically I have to take my in.pdf
I have to write a java program . So basically I have to take my in.pdfI have to write a java program . So basically I have to take my in.pdf
I have to write a java program . So basically I have to take my in.pdfFashionBoutiquedelhi
 
Fill in the blanks.Intensity of orangebrownred color of reduced .pdf
Fill in the blanks.Intensity of orangebrownred color of reduced .pdfFill in the blanks.Intensity of orangebrownred color of reduced .pdf
Fill in the blanks.Intensity of orangebrownred color of reduced .pdfFashionBoutiquedelhi
 
Explain how cellular respiration (O2 consumption and CO2 production).pdf
Explain how cellular respiration (O2 consumption and CO2 production).pdfExplain how cellular respiration (O2 consumption and CO2 production).pdf
Explain how cellular respiration (O2 consumption and CO2 production).pdfFashionBoutiquedelhi
 
Entrepreneurship isA. one of the basic rights of the private enter.pdf
Entrepreneurship isA. one of the basic rights of the private enter.pdfEntrepreneurship isA. one of the basic rights of the private enter.pdf
Entrepreneurship isA. one of the basic rights of the private enter.pdfFashionBoutiquedelhi
 
Discrete Random Variables we are working with commonly used discre.pdf
Discrete Random Variables we are working with commonly used discre.pdfDiscrete Random Variables we are working with commonly used discre.pdf
Discrete Random Variables we are working with commonly used discre.pdfFashionBoutiquedelhi
 
Determine the open intervals over which the function is increasing an.pdf
Determine the open intervals over which the function is increasing an.pdfDetermine the open intervals over which the function is increasing an.pdf
Determine the open intervals over which the function is increasing an.pdfFashionBoutiquedelhi
 
Discuss and define Isotropy. Anisotropy and Orthotropy with regards t.pdf
Discuss and define Isotropy. Anisotropy and Orthotropy with regards t.pdfDiscuss and define Isotropy. Anisotropy and Orthotropy with regards t.pdf
Discuss and define Isotropy. Anisotropy and Orthotropy with regards t.pdfFashionBoutiquedelhi
 
Consider the circuits in figure 2.32. If we assume the transistors ar.pdf
Consider the circuits in figure 2.32. If we assume the transistors ar.pdfConsider the circuits in figure 2.32. If we assume the transistors ar.pdf
Consider the circuits in figure 2.32. If we assume the transistors ar.pdfFashionBoutiquedelhi
 

More from FashionBoutiquedelhi (20)

Questions1) How does multimedia transmission differ than messag.pdf
Questions1) How does multimedia transmission differ than messag.pdfQuestions1) How does multimedia transmission differ than messag.pdf
Questions1) How does multimedia transmission differ than messag.pdf
 
Question 1 of 10 Map sapling learning Based on the chart to the right.pdf
Question 1 of 10 Map sapling learning Based on the chart to the right.pdfQuestion 1 of 10 Map sapling learning Based on the chart to the right.pdf
Question 1 of 10 Map sapling learning Based on the chart to the right.pdf
 
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdfplease send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
 
On January 1, 2002, Germany officially adopted the euro as its curre.pdf
On January 1, 2002, Germany officially adopted the euro as its curre.pdfOn January 1, 2002, Germany officially adopted the euro as its curre.pdf
On January 1, 2002, Germany officially adopted the euro as its curre.pdf
 
Networking problem help Consider the following TCP session between h.pdf
Networking problem help Consider the following TCP session between h.pdfNetworking problem help Consider the following TCP session between h.pdf
Networking problem help Consider the following TCP session between h.pdf
 
Lipid bilayers can only be synthesized by biological organisms. This .pdf
Lipid bilayers can only be synthesized by biological organisms. This .pdfLipid bilayers can only be synthesized by biological organisms. This .pdf
Lipid bilayers can only be synthesized by biological organisms. This .pdf
 
Is a neutral trait (meaning it is neither deleterious nor advantageo.pdf
Is a neutral trait (meaning it is neither deleterious nor advantageo.pdfIs a neutral trait (meaning it is neither deleterious nor advantageo.pdf
Is a neutral trait (meaning it is neither deleterious nor advantageo.pdf
 
Let T R^n rightarrow R^m be a linear transformation. Given a subspa.pdf
Let T  R^n rightarrow R^m be a linear transformation. Given a subspa.pdfLet T  R^n rightarrow R^m be a linear transformation. Given a subspa.pdf
Let T R^n rightarrow R^m be a linear transformation. Given a subspa.pdf
 
Just a general question on some vocab words I cannot piece together .pdf
Just a general question on some vocab words I cannot piece together .pdfJust a general question on some vocab words I cannot piece together .pdf
Just a general question on some vocab words I cannot piece together .pdf
 
In order to be useful to managers, management accounting reports shou.pdf
In order to be useful to managers, management accounting reports shou.pdfIn order to be useful to managers, management accounting reports shou.pdf
In order to be useful to managers, management accounting reports shou.pdf
 
Im also confused on question 9 and 10 On which point(s) would Charle.pdf
Im also confused on question 9 and 10 On which point(s) would Charle.pdfIm also confused on question 9 and 10 On which point(s) would Charle.pdf
Im also confused on question 9 and 10 On which point(s) would Charle.pdf
 
In about six billion years, our suns luminosity (a.k.a. power outpu.pdf
In about six billion years, our suns luminosity (a.k.a. power outpu.pdfIn about six billion years, our suns luminosity (a.k.a. power outpu.pdf
In about six billion years, our suns luminosity (a.k.a. power outpu.pdf
 
I have to write a java program . So basically I have to take my in.pdf
I have to write a java program . So basically I have to take my in.pdfI have to write a java program . So basically I have to take my in.pdf
I have to write a java program . So basically I have to take my in.pdf
 
Fill in the blanks.Intensity of orangebrownred color of reduced .pdf
Fill in the blanks.Intensity of orangebrownred color of reduced .pdfFill in the blanks.Intensity of orangebrownred color of reduced .pdf
Fill in the blanks.Intensity of orangebrownred color of reduced .pdf
 
Explain how cellular respiration (O2 consumption and CO2 production).pdf
Explain how cellular respiration (O2 consumption and CO2 production).pdfExplain how cellular respiration (O2 consumption and CO2 production).pdf
Explain how cellular respiration (O2 consumption and CO2 production).pdf
 
Entrepreneurship isA. one of the basic rights of the private enter.pdf
Entrepreneurship isA. one of the basic rights of the private enter.pdfEntrepreneurship isA. one of the basic rights of the private enter.pdf
Entrepreneurship isA. one of the basic rights of the private enter.pdf
 
Discrete Random Variables we are working with commonly used discre.pdf
Discrete Random Variables we are working with commonly used discre.pdfDiscrete Random Variables we are working with commonly used discre.pdf
Discrete Random Variables we are working with commonly used discre.pdf
 
Determine the open intervals over which the function is increasing an.pdf
Determine the open intervals over which the function is increasing an.pdfDetermine the open intervals over which the function is increasing an.pdf
Determine the open intervals over which the function is increasing an.pdf
 
Discuss and define Isotropy. Anisotropy and Orthotropy with regards t.pdf
Discuss and define Isotropy. Anisotropy and Orthotropy with regards t.pdfDiscuss and define Isotropy. Anisotropy and Orthotropy with regards t.pdf
Discuss and define Isotropy. Anisotropy and Orthotropy with regards t.pdf
 
Consider the circuits in figure 2.32. If we assume the transistors ar.pdf
Consider the circuits in figure 2.32. If we assume the transistors ar.pdfConsider the circuits in figure 2.32. If we assume the transistors ar.pdf
Consider the circuits in figure 2.32. If we assume the transistors ar.pdf
 

Recently uploaded

PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsISCOPE Publication
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxMohamed Rizk Khodair
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 

Recently uploaded (20)

PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 

you will use cellular automata to create a 2D predator–prey simulati.pdf

  • 1. you will use cellular automata to create a 2D predator–prey simulation in your program. The preys are ants and the predators are doodlebugs. These critters live in a 20 * 20 grid of cells. Only one critter may occupy a cell at a time. The grid is enclosed and no critter may move off the grid. Time is simulated in steps. Each critter performs some action every time step. The ants behave according to the following model: Move: For every time step, the ants 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 (not been eaten by doodlebugs), 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 randomly. If there is no empty cell available, no breeding occurs. Once an offspring is produced, an ant cannot produce an offspring again until it has survived three more time steps. The doodlebugs behave according to the following model: Move: For every time step, the doodlebug will firstly try to move to an adjacent cell containing an ant and eat the ant (you can decide if there are several ants in the adjacent cells, how the doodlebug will choose to move). If there are no ants in adjacent cells, 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, 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 three time steps, at the end of the third time step it will starve and die. The doodlebug should then be removed from the grid of cells. Initialize the world with 5 doodlebugs and 100 ants. You will randomly place them. 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. You will prompt the user to enter the number of time steps to run. Create a class named Critter that contains data and functions common to ants and doodlebugs. This class should have a virtual function named move that is defined in the derived classes of Ant and Doodlebug. Each class will be in its own source file. For each time step, do the following in your program: after moves, when breeding, eating, and starving are resolved, display the resulting grid. Draw the world using ASCII characters of “O” for an ant, “X” for a doodlebug and “E” for an empty space (the characters should be arranged to look like a grid). The doodlebugs will move before the ants in each time step. When you reach the time steps entered by the user, ask them to enter another number and start to run the simulation again or to exit the program. You must maintain the state of the current grid while creating the next display. You will use a dynamic array to represent the grid. Each array element
  • 2. will be a pointer to a Critter. Get your program running and tested. For debugging your program, you should save the random placement until you have everything else working properly. In general, “random” is bad for testing and debugging. Solution World.java import java.util.*; import java.awt.*; public class World{ private static Random r= new Random(); private int doodlebugs; private int ants; private static final int SIZE = 20; private Critter[][] oGrid; World(int d,int a){ oGrid = new Critter[SIZE][SIZE]; doodlebugs = d; ants = a; for(int i = 0; i < d; i++){ boolean isOccupied = true; while(isOccupied){ int potX = r.nextInt(SIZE); int potY = r.nextInt(SIZE); if(oGrid[potX][potY] == null) { isOccupied = false; oGrid[potX][potY] = new Doodlebug(this,potX,potY); } } } for(int i = 0; i < a; i++){ boolean isOccupied = true; while(isOccupied){ int potX = r.nextInt(SIZE); int potY = r.nextInt(SIZE); if(oGrid[potX][potY] == null) { isOccupied = false; oGrid[potX][potY] = new Ant(this,potX,potY); } } } } public int getdoodlebugs(){ return doodlebugs; } public int getants(){ return ants; } public char[][] getCGrid(){ char[][] grid = new char[SIZE][SIZE]; for(int i = 0; i < SIZE; i++){ for(int j = 0;j < SIZE; j++){ if(oGrid[j][i]!=null) if(oGrid[j][i].getClass().getName().equals("Ant")) Grid[j][i] = 'o'; else if(oGrid[j][i].getClass().getName().equals("Doodlebug")) grid[j][i] = 'x'; else if(oGrid[j][i].getClass().getName().equals("Empty")) grid[j][i] = 'e'; else grid[j][i] = ' '; } } return grid; } public void increasedoodlebugs(){ doodlebugs++; } public void decreasedoodlebugs(){ doodlebugs--; } public void increaseants(){
  • 3. ants++; } public void decreaseants(){ ants--; } public void moveAll(){ for(int i = 0; i < SIZE; i++){ for(int j = 0;j < SIZE; j++){ if(oGrid[j][i] !=null) oGrid[j][i].move(); } } for(int i = 0; i < SIZE; i++){ for(int j = 0;j < SIZE; j++){ if(oGrid[j][i] !=null) oGrid[j][i].moved=false; } } } public void breedAll(){ for(int i = 0; i < SIZE; i++){ for(int j = 0;j < SIZE; j++){ if(oGrid[j][i]!=null) oGrid[j][i].breed(); } } } public void starveAll(){ for(int i = 0; i < SIZE; i++){ for(int j = 0;j < SIZE; j++){ if(oGrid[j][i]!=null) oGrid[j][i].starve(); } } } public boolean isEmpty(int x,int y){
  • 4. boolean tbr = false; if(oGrid[x][y]==null) tbr = true; return tbr; } public boolean isAnt(int x,int y){ boolean tbr = false; if(oGrid[x][y] instanceof Ant) tbr = true; return tbr; } public void setCell(int x, int y, Critter O){ oGrid[x][y] = O; } } Ant.java import java.awt.*; import java.io.*; import java.util.*; public class Ant extends Critter{ private static Random r = new Random(); private int untilBreed; public Ant(World world,int _x, int _y){ super(world,_x, _y ); untilBreed = 3; moved = false;//defaults to TRUE so that 'child' ants dont move } public void move(){ if(!moved){ int[] potMoves = new int[4]; int i = 1; potMoves[0] = r.nextInt(4); do{ do{ potMoves[i] = r.nextInt(4); }
  • 5. while(potMoves[i]!=potMoves[i-1]); i++; } while(i<4); i = 0; boolean empty = false; int oldx = x; int oldy = y; while(i<4 && !empty){ switch(potMoves[i]){ case 0: if(x!=0){ if(world.isEmpty(x-1,y)){ empty = true; world.setCell(x-1,y,this); world.setCell(oldx,y,null); moved = true; untilBreed--; x=x-1; } } else if(world.isEmpty(19,y)){ empty = true; untilBreed--; world.setCell(19,y,this); world.setCell(oldx,y,null); moved = =19; } break; case 1: if(x!=19){ if(world.isEmpty(x+1,y)){ untilBreed--; world.setCell(x+1,y,this); world.setCell(oldx,y,null); moved = true; empty = true; x=x+1; } }
  • 6. else if(world.isEmpty(0,y)){ world.setCell(0,y,this); world.setCell(oldx,y,null); moved = true; empty = true; x=0; untilBreed--; } break; case 2: if(y!=0){ if(world.isEmpty(x,y-1)){ world.setCell(x,y-1,this); world.setCell(x,oldy,null); moved = true; empty = true; y=y-1; untilBreed--; } } else if(world.isEmpty(x,19)){ world.setCell(x,19,this); world.setCell(x,oldy,null); moved = true; empty = true; y=19; untilBreed--; } break; case 3: if(y!=19){ if(world.isEmpty(x,y+1)){ world.setCell(x,y+1,this); world.setCell(x,oldy,null); moved = true; empty = true;
  • 7. y=y+1; untilBreed--; } } else if(world.isEmpty(x,0)){ world.setCell(x,0,this); world.setCell(x,oldy,null); moved = true; empty = true; y=0; untilBreed--; } break; } i++; } } } public void breed(){ if(untilBreed==0){ int[] potMoves = new int[4]; int i = 1; potMoves[0] = r.nextInt(4); do{ do{ potMoves[i] = r.nextInt(4); } while(potMoves[i]!=potMoves[i-1]); i++; } while(i<4); i = 0; boolean empty = false; int oldx = x; int oldy = y;
  • 8. while(i<4 && !empty){ switch(potMoves[i]){ case 0: if(x!=0){ if(world.isEmpty(x-1,y)){ world.setCell(x-1,y,new Ant(world, x-1,y)); untilBreed = 3; empty = true; } } else if(world.isEmpty(19,y)){ world.setCell(19,y,new Ant(world, 19,y)); untilBreed = 3; empty = true; } break; case 1: if(x!=19){ if(world.isEmpty(x+1,y)){ world.setCell(x+1,y,new Ant(world, x+1,y)); untilBreed=3; empty = true; } } else if(world.isEmpty(0,y)){ world.setCell(0,y,new Ant(world, 0,y)); untilBreed=3; empty = true; } break; case 2: if(y!=0){ if(world.isEmpty(x,y-1)){ world.setCell(x,y-1,new Ant(world, x,y-1)); untilBreed=3; empty = true;
  • 9. } } else if(world.isEmpty(x,19)){ world.setCell(x,19,new Ant(world, x,19)); untilBreed=3; empty = true; } break; case 3: if(y!=19){ if(world.isEmpty(x,y+1)){ world.setCell(x,y+1,new Ant(world, x,y+1)); untilBreed=3; empty = true; } } else if(world.isEmpty(x,0)){ world.setCell(x,0,new Ant(world, x,0)); untilBreed=3; empty = true; } break; } i++; } } } } Grid.java import javax.swing.*; import java.awt.*; public class Grid extends JPanel { private int rows; private int cols; private JLabel[][] grid; public Grid(int r, int c) {
  • 10. rows = r; cols = c; grid = new JLabel[rows][cols]; setLayout (new GridLayout(rows, cols, 0, 0)); setBackground( new Color( 200,200,200 ) ); for (int i = 0; i < rows; i++ ) { for (int j = 0; j < cols; j++ ) { grid[i][j] = new JLabel(); grid[i][j].setBorder( BorderFactory.createLineBorder(Color.BLACK,1) ); grid[i][j].setFont( new Font("Courier", Font.BOLD, 12)); grid[i][j].setForeground(Color.WHITE); grid[i][j].setHorizontalAlignment(SwingConstants.CENTER); add( grid[i][j] ); } } setPreferredSize( new Dimension(500,500) ); } public void setGrid(int i, int j, char value) { if (value == '.') value = ' '; grid[i][j].setText( "" + value ); switch (value) { case 'o': grid[i][j].setBackground( Color.MAGENTA ); grid[i][j].setOpaque(true); break; case 'x': grid[i][j].setBackground( Color.BLUE ); grid[i][j].setOpaque(true); break; case 'S': grid[i][j].setBackground( Color.RED ); grid[i][j].setOpaque(true); break; default: grid[i][j].setOpaque(false);
  • 11. break; } } } DisplayFrame.Java import javax.swing.*; import java.util.*; public class DisplayFrame extends JFrame { private Grid grid; public DisplayFrame(String title, int x, int y) { setTitle( title ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); grid = new Grid(y, x); add( grid ); pack(); setVisible( true ); } public void setGrid( char [][] data ) { for (int i = 0; i < data.length; i++ ) { for (int j = 0; j < data[i].length; j++ ) { grid.setGrid( i, j, data[i][j] ); } } } } Prog.java Import java.util.*; import java.awt.*; import java.io.*; import java.lang.*; public class Prog{ public static Scanner s = new Scanner(System.in); public static void main(String[] args){ World world = new World(5,100); DisplayFrame disp = new DisplayFrame("Pred/Prey", 20, 20 ); disp.setGrid( world.getCGrid() );
  • 12. String input = "1"; int timesteps = 1; boolean playing = true; while(playing){ System.out.println("Options: [ENTER] Last timestep."); System.out.println(" [Q] Quit Simulation."); System.out.println(" [n] Enter another number."); input = s.nextLine(); if(input.equals("Q")||input.equals("q")){ playing = false; continue; } else if(input.equals("")){ timestep(timesteps,world); } else if(isNum(input)){ timesteps = Integer.parseInt(input); timestep(timesteps,world); } else{ System.out.println("Try again!"); continue; } disp.setGrid( world.getCGrid() ); } } public static void timestep(int timesteps, World w){ for(int i = timesteps; i > 0; i--){ w.moveAll(); w.breedAll(); w.starveAll(); } } public static boolean isNum(String s){ try{ Integer.parseInt(s);
  • 13. } catch(NumberFormatException nfe) { return false; } return true; } } Doodlebug.java import java.awt.*; import java.io.*; import java.util.*; public class Doodlebug extends Critter{ private static Random r = new Random(); private int untilBreed; private int untilStarve; public Doodlebug( World world,int _x, int _y){ super(world, _x, _y ); untilBreed = 8; untilStarve = 3; moved = true; } public void move(){ if(!moved){ int[] potMoves = new int[4]; int i = 1; potMoves[0] = r.nextInt(4); do{ do{ potMoves[i] = r.nextInt(4); } while(potMoves[i]!=potMoves[i-1]); i++; } while(i<4); i = 0; boolean empty = false; int oldx = x;
  • 14. int oldy = y; while(i<4 && !empty){ switch(potMoves[i]){ case 0: if(x!=0){ if(world.isEmpty(x-1,y)){ empty = true; world.setCell(x-1,y,this); world.setCell(oldx,y,null); moved = true; untilBreed--; untilStarve--; x=x-1; } } else if(world.isEmpty(19,y)){ empty = true; untilBreed--; untilStarve--; world.setCell(19,y,this); world.setCell(oldx,y,null); moved = true; x=19; } break; case 1: if(x!=19){ if(world.isEmpty(x+1,y)){ untilBreed--; untilStarve--; world.setCell(x+1,y,this); world.setCell(oldx,y,null); moved = true; empty = true; x=x+1; }
  • 15. } else if(world.isEmpty(0,y)){ world.setCell(0,y,this); world.setCell(oldx,y,null); moved = true; empty = true; x=0; untilBreed--; untilStarve--; } break; case 2: if(y!=0){ if(world.isEmpty(x,y-1)){ world.setCell(x,y-1,this); world.setCell(x,oldy,null); moved = true; empty = true; y=y-1; untilBreed--; untilStarve--; } } else if(world.isEmpty(x,19)){ world.setCell(x,19,this); world.setCell(x,oldy,null); moved = true; empty = true; y=19; untilBreed--; untilStarve--; } break; case 3: if(y!=19){ if(world.isEmpty(x,y+1)){
  • 16. world.setCell(x,y+1,this); world.setCell(x,oldy,null); moved = true; empty = true; y=y+1; untilBreed--; untilStarve--; } } else if(world.isEmpty(x,0)){ world.setCell(x,0,this); world.setCell(x,oldy,null); moved = true; empty = true; y=0; untilBreed--; untilStarve--; } break; } i++; } i= 0; while(i<4 && !empty){ switch(potMoves[i]){ case 0: if(x!=0){ if(world.isAnt(x-1,y)){ empty = true; world.setCell(x-1,y,this); world.setCell(oldx,y,null); moved = true; untilBreed--; untilStarve=3; x=x-1; }
  • 17. } else if(world.isAnt(19,y)){ empty = true; untilBreed--; untilStarve=3; world.setCell(19,y,this); world.setCell(oldx,y,null); moved = true; x=19; } break; case 1: if(x!=19){ if(world.isAnt(x+1,y)){ untilBreed--; untilStarve=3; world.setCell(x+1,y,this); world.setCell(oldx,y,null); moved = true; empty = true; x=x+1; } } else if(world.isAnt(0,y)){ world.setCell(0,y,this); world.setCell(oldx,y,null); moved = true; empty = true; x=0; untilBreed--; untilStarve=3; } break; case 2: if(y!=0){ if(world.isAnt(x,y-1)){
  • 18. world.setCell(x,y-1,this); world.setCell(x,oldy,null); moved = true; empty = true; y=y-1; untilBreed--; untilStarve=3; } } else if(world.isAnt(x,19)){ world.setCell(x,19,this); world.setCell(x,oldy,null); moved = true; empty = true; y=19; untilBreed--; untilStarve=3; } break; case 3: if(y!=19){ if(world.isAnt(x,y+1)){ world.setCell(x,y+1,this); world.setCell(x,oldy,null); moved = true; empty = true; y=y+1; untilBreed--; untilStarve=3; } } else if(world.isAnt(x,0)){ world.setCell(x,y+1,this); world.setCell(x,oldy,null); moved = true;