JAVA The goal for this programming project is to create a simple 2D predator–pre
ID: 3907487 • Letter: J
Question
JAVA
The goal for this programming project is to create a simple 2D predator–prey
simulation. In this simulation, the prey is ants, and the predators are doodlebugs.
These critters live in a world composed of a 20 20 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 grid. 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
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 third 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, no breeding occurs. Once an offspring is produced, the
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 cell (up, down, left, or right)
occupied by an ant, 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 pat-
tern between the population of predators and prey, although random perturbations
may lead to the elimination of one or both species.
Explanation / Answer
**************************************
Organism.java
public class Organism {
int breedThreshold;
int timeSinceBreed;
char symbol; //O for Ants, X for Doodlebugs
int rowPosition;
int colPosition;
boolean topFlag;
boolean rightFlag;
boolean bottomFlag;
boolean leftFlag;
public Organism() {
this.setRowPosition(-1);
this.setColPosition(-1);
this.timeSinceBreed = 0;
this.topFlag = true;
this.bottomFlag = true;
this.leftFlag = true;
this.rightFlag = true;
}
public Organism(int row, int col) {
this.setRowPosition(row);
this.setColPosition(col);
this.timeSinceBreed = 0;
}
public Organism(Organism o) {
this.breedThreshold = o.breedThreshold;
this.timeSinceBreed = o.timeSinceBreed;
this.symbol = o.symbol;
this.setRowPosition(o.getRowPosition());
this.setColPosition(o.getColPosition());
}
public char getSymbol() {
return this.symbol;
}
public int getRowPosition() {
return this.rowPosition;
}
public int getColPosition() {
return this.colPosition;
}
public void setRowPosition(int row) {
this.topFlag = false;
this.bottomFlag = false;
this.rowPosition = row;
if (row <= 0) {
this.topFlag = true;
} else if(row >= 19) {
this.bottomFlag = true;
}
}
public void setColPosition(int col) {
this.leftFlag = false;
this.rightFlag = false;
this.colPosition = col;
if (col <= 0) {
this.leftFlag = true;
} else if(col >= 19) {
this.rightFlag = true;
}
}
}
**************************************
Doodlebug.java
public class Doodlebug extends Organism {
final static int STARVE_THRESHOLD = 3;
int timeSinceEat;
public Doodlebug() {
super();
this.symbol = 'X';
this.breedThreshold = 8;
this.timeSinceEat = 0;
}
public Doodlebug(int row, int col) {
super(row, col);
this.symbol = 'X';
this.breedThreshold = 8;
this.timeSinceEat = 0;
}
public Doodlebug(Doodlebug d) {
super(d);
this.symbol = d.symbol;
this.breedThreshold = d.breedThreshold;
this.timeSinceEat = d.timeSinceEat;
}
public void moveBug(int direction) {
if (direction < 1 || direction > 4)
{
System.out.println("Error: Improper moveBug parameter.");
System.exit(0);
}
switch (direction) {
case 1: Board.field[this.getRowPosition() - 1][this.getColPosition()] = null; //Remove Ant
Board.field[this.getRowPosition() - 1][this.getColPosition()] = this; //Copy over DB
Board.field[this.getRowPosition()][this.getColPosition()] = null; //Remove old DB
this.setRowPosition(this.getRowPosition() - 1);
break;
case 2: Board.field[this.getRowPosition()][this.getColPosition() + 1] = null; //Remove Ant
Board.field[this.getRowPosition()][this.getColPosition() + 1] = this; //Copy over DB
Board.field[this.getRowPosition()][this.getColPosition()] = null; //Remove old DB
this.setColPosition(this.getColPosition() + 1);
break;
case 3: Board.field[this.getRowPosition() + 1][this.getColPosition()] = null; //Remove Ant
Board.field[this.getRowPosition() + 1][this.getColPosition()] = this; //Copy over DB
Board.field[this.getRowPosition()][this.getColPosition()] = null; //Remove old DB
this.setRowPosition(this.getRowPosition() + 1);
break;
case 4: Board.field[this.getRowPosition()][this.getColPosition() - 1] = null; //Remove Ant
Board.field[this.getRowPosition()][this.getColPosition() - 1] = this; //Copy over DB
Board.field[this.getRowPosition()][this.getColPosition()] = null; //Remove old DB
this.setColPosition(this.getColPosition() - 1);
break;
}
}
public String toString() {
return "Doodlebug at " + this.getRowPosition() + ", " + this.getColPosition() + " " +
"topFlag " + this.topFlag + " " +
"rightFlag " + this.rightFlag + " " +
"bottomFlag " + this.bottomFlag + " " +
"leftFlag " + this.leftFlag + " " +
"timeSinceEat: " + this.timeSinceEat + " " +
"timeSinceBreed: " + this.timeSinceBreed;
}
}
**************************************
Ant.java
public class Ant extends Organism {
public Ant() {
super();
this.symbol = 'O';
this.breedThreshold = 3;
}
public Ant(int row, int col) {
super(row, col);
this.symbol = 'O';
this.breedThreshold = 3;
}
public Ant (Ant a) {
super(a);
}
public void moveBug(int direction) {
if (direction < 1 || direction > 4) {
System.err.println("Error: Improper moveBug parameter.");
System.exit(0);
}
switch (direction) {
case 1:
Board.field[this.getRowPosition() - 1][this.getColPosition()] = this; //Copy over Ant
Board.field[this.getRowPosition()][this.getColPosition()] = null; //Remove old Ant
this.setRowPosition(this.getRowPosition() - 1);
break;
case 2:
Board.field[this.getRowPosition()][this.getColPosition() + 1] = this; //Copy over Ant
Board.field[this.getRowPosition()][this.getColPosition()] = null; //Remove old Ant
this.setColPosition(this.getColPosition() + 1);
break;
case 3:
Board.field[this.getRowPosition() + 1][this.getColPosition()] = this; //Copy over Ant
Board.field[this.getRowPosition()][this.getColPosition()] = null; //Remove old Ant
this.setRowPosition(this.getRowPosition() + 1);
break;
case 4:
Board.field[this.getRowPosition()][this.getColPosition() - 1] = this; //Copy over Ant
Board.field[this.getRowPosition()][this.getColPosition()] = null; //Remove old Ant
this.setColPosition(this.getColPosition() - 1);
break;
}
}
}
**************************************
Board.java
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class Board {
static Organism[][] field = new Organism[20][20];
static int numberOfAnts;
static int numberOfDoodlebugs;
Random randomNumGen = new Random();
//Used for scanning efficiently through the field
static int rowScan = 0;
static int colScan = 0;
public Board() {
numberOfAnts = 100;
numberOfDoodlebugs = 5;
initializeBoard(numberOfAnts, numberOfDoodlebugs);
}
public Board(int ants, int doodlebugs) {
numberOfAnts = ants;
numberOfDoodlebugs = doodlebugs;
initializeBoard(numberOfAnts, numberOfDoodlebugs);
}
public void initializeBoard(int numAnt, int numDoodlebugs) {
//Place Ants in field
for (int i = 0; i < numAnt; i++) {
placeBug(new Ant(), numAnt);
}
//Place Doodlebugs in field
for (int i = 0; i < numDoodlebugs; i++) {
placeBug(new Doodlebug(), numDoodlebugs);
}
}
public boolean isOccupied(int row, int col) {
//Check for null value in given field index
if (Board.field[row][col] != null) {
return true;
} else {
return false;
}
}
public void placeBug(Organism bug, int numBug) {
//Declare and initialize random row and column values
int row = randomNumGen.nextInt(20);
int col = randomNumGen.nextInt(20);
//Cycle through field until an empty cell is found
while (this.isOccupied(row, col)) {
row = randomNumGen.nextInt(20);
col = randomNumGen.nextInt(20);
}
//Place a new Ant in open cell
if(bug instanceof Ant) {
field[row][col] = new Ant(row, col);
}
//Place a new Doodlebug in open cell
else if(bug instanceof Doodlebug) {
field[row][col]= new Doodlebug(row, col);
}
//Error catching
else {
System.out.println("Error in placeBug method! Did not place a bug in an open cell.");
System.exit(0);
}
}
public static Organism getBug(int row, int col) {
return field[row][col];
}
public static Organism getBug(int[] position) {
return field[position[0]][position[1]];
}
public boolean antAdjacent(Doodlebug db) {
//Top left corner check
if (db.topFlag && db.leftFlag) {
if ( (getBug(db.getRowPosition(), db.getColPosition() + 1) instanceof Ant) ||
(getBug(db.getRowPosition() + 1, db.getColPosition()) instanceof Ant) ) {
return true;
} else {
return false;
}
}
//Bottom right corner check
else if (db.bottomFlag && db.rightFlag) {
if ( (getBug(db.getRowPosition() - 1, db.getColPosition()) instanceof Ant) ||
(getBug(db.getRowPosition(), db.getColPosition() - 1) instanceof Ant) ) {
return true;
} else {
return false;
}
}
//Top right corner check
else if (db.topFlag && db.rightFlag) {
if ( (getBug(db.getRowPosition() + 1, db.getColPosition()) instanceof Ant) ||
(getBug(db.getRowPosition(), db.getColPosition() - 1) instanceof Ant) ) {
return true;
} else {
return false;
}
}
//Bottom left corner check
else if (db.bottomFlag && db.leftFlag) {
if ( (getBug(db.getRowPosition() - 1, db.getColPosition()) instanceof Ant) ||
(getBug(db.getRowPosition(), db.getColPosition() + 1) instanceof Ant) ) {
return true;
} else {
return false;
}
}
//Top row check
else if (db.topFlag) {
if ( (getBug(db.getRowPosition(), db.getColPosition() + 1) instanceof Ant) ||
(getBug(db.getRowPosition() + 1, db.getColPosition()) instanceof Ant) ||
(getBug(db.getRowPosition(), db.getColPosition() - 1) instanceof Ant) ) {
return true;
} else {
return false;
}
}
//Right column check
else if (db.rightFlag) {
if ( (getBug(db.getRowPosition() - 1, db.getColPosition()) instanceof Ant) ||
(getBug(db.getRowPosition() + 1, db.getColPosition()) instanceof Ant) ||
(getBug(db.getRowPosition(), db.getColPosition() - 1) instanceof Ant) ) {
return true;
} else {
return false;
}
}
//Bottom row check
else if (db.bottomFlag) {
if ( (getBug(db.getRowPosition() - 1, db.getColPosition()) instanceof Ant) ||
(getBug(db.getRowPosition(), db.getColPosition() + 1) instanceof Ant) ||
(getBug(db.getRowPosition(), db.getColPosition() - 1) instanceof Ant) ) {
return true;
} else {
return false;
}
}
//Left column check
else if (db.leftFlag) {
if ( (getBug(db.getRowPosition() - 1, db.getColPosition()) instanceof Ant) ||
(getBug(db.getRowPosition(), db.getColPosition() + 1) instanceof Ant) ||
(getBug(db.getRowPosition() + 1, db.getColPosition()) instanceof Ant) ) {
return true;
} else {
return false;
}
}
//Check four positions for Ant
else if ((getBug(db.getRowPosition() - 1, db.getColPosition()) instanceof Ant) ||
(getBug(db.getRowPosition(), db.getColPosition() + 1) instanceof Ant) ||
(getBug(db.getRowPosition() + 1, db.getColPosition()) instanceof Ant) ||
(getBug(db.getRowPosition(), db.getColPosition() - 1) instanceof Ant) ) {
return true;
} else {
return false;
}
}
public boolean emptyCellAdjacent(Organism bug) {
//Top left corner check
if (bug.topFlag && bug.leftFlag) {
if ( (getBug(bug.getRowPosition(), bug.getColPosition() + 1) == null) ||
(getBug(bug.getRowPosition() + 1, bug.getColPosition()) == null) ) {
return true;
} else {
return false;
}
}
//Bottom right corner check
else if (bug.bottomFlag && bug.rightFlag) {
if ((getBug(bug.getRowPosition() - 1, bug.getColPosition()) == null) ||
(getBug(bug.getRowPosition(), bug.getColPosition() - 1) == null) ) {
return true;
}
else {
return false;
}
}
//Top right corner check
else if (bug.topFlag && bug.rightFlag) {
if ((getBug(bug.getRowPosition() + 1, bug.getColPosition()) == null) ||
(getBug(bug.getRowPosition(), bug.getColPosition() - 1) == null) ) {
return true;
} else {
return false;
}
}
//Bottom left corner check
else if (bug.bottomFlag && bug.leftFlag)
{
if ((getBug(bug.getRowPosition() - 1, bug.getColPosition()) == null) ||
(getBug(bug.getRowPosition(), bug.getColPosition() + 1) == null) ) {
return true;
} else {
return false;
}
}
//Top row check
else if (bug.topFlag) {
if (
(getBug(bug.getRowPosition(), bug.getColPosition() + 1) == null) ||
(getBug(bug.getRowPosition() + 1, bug.getColPosition()) == null) ||
(getBug(bug.getRowPosition(), bug.getColPosition() - 1) == null) ) {
return true;
} else {
return false;
}
}
//Right column check
else if (bug.rightFlag)
{
if (
(getBug(bug.getRowPosition() - 1, bug.getColPosition()) == null) ||
(getBug(bug.getRowPosition() + 1, bug.getColPosition()) == null) ||
(getBug(bug.getRowPosition(), bug.getColPosition() - 1) == null) ) {
return true;
} else {
return false;
}
}
//Bottom row check
else if (bug.bottomFlag) {
if (
(getBug(bug.getRowPosition() - 1, bug.getColPosition()) == null) ||
(getBug(bug.getRowPosition(), bug.getColPosition() + 1) == null) ||
(getBug(bug.getRowPosition(), bug.getColPosition() - 1) == null) ) {
return true;
} else {
return false;
}
}
//Left column check
else if (bug.leftFlag) {
if (
(getBug(bug.getRowPosition() - 1, bug.getColPosition()) == null) ||
(getBug(bug.getRowPosition(), bug.getColPosition() + 1) == null) ||
(getBug(bug.getRowPosition() + 1, bug.getColPosition()) == null) ) {
return true;
} else {
return false;
}
}
//Check four positions for Ant
else if (
(getBug(bug.getRowPosition() - 1, bug.getColPosition()) == null) ||
(getBug(bug.getRowPosition(), bug.getColPosition() + 1) == null) ||
(getBug(bug.getRowPosition() + 1, bug.getColPosition()) == null) ||
(getBug(bug.getRowPosition(), bug.getColPosition() - 1) == null) ) {
return true;
} else {
return false;
}
}
public void printBoard() {
//Top row border
System.out.println(" 01234567890123456789 ");
for(int i = 0; i < field.length; i++) {
//Left edge border
System.out.printf("%2d|", i);
for(int j = 0; j < field[i].length; j++) {
//Print whitespace if empty cell
if (field[i][j] == null) {
System.out.print("-");
}
//Print symbol for occupying bug
else {
System.out.print(field[i][j].symbol);
}
}
//Right edge border
System.out.print("|");
//Prevent printing last empty line
if (i < field.length - 1) {
System.out.println();
}
}
//Bottom row border
System.out.println(" ");
System.out.println("Number of Ants: " + numberOfAnts);
System.out.println("Number of Doodlebugs: " + numberOfDoodlebugs);
}
public void removeBug(int row, int col) {
if (field[row][col] == null) {
System.out.println("Error: removeBug tried to remove a bug, but there was none.");
System.exit(0);
}
if(field[row][col] instanceof Ant) {
numberOfAnts--;
}
if(field[row][col] instanceof Doodlebug) {
numberOfDoodlebugs--;
}
field[row][col] = null;
}
public static Integer[] randomDirections() {
Integer[] directionArray = new Integer[] {1, 2, 3, 4};
Collections.shuffle(Arrays.asList(directionArray));
return directionArray;
}
public static int[] antScanner() {
for (int j = colScan; j < 20; j++) {
//Finish partially completed row
if (field[rowScan][j] instanceof Ant) {
//Check for last column
if (colScan >= 19) {
colScan = 0;
rowScan++;
return new int[] {rowScan - 1, 19};
} else {
colScan = j + 1;
return new int[] {rowScan, j};
}
}
}
rowScan++;
colScan = 0;
for (int i = rowScan; i < 20; i++) {
for (int j = colScan; j < 20; j++) {
if (field[i][j] instanceof Ant) {
if (j >= 19) {
colScan = 0;
rowScan++;
return new int[] {i, 19};
} else {
colScan = j + 1;
return new int[] {i, j};
}
} else if (j >= 19) {
colScan = 0;
rowScan++;
}
}
}
rowScan = 0;
colScan = 0;
return new int[] {-1, -1};
}
public static int[] doodlebugScanner() {
for (int j = colScan; j < 20; j++) {
//Finish partially completed row
if (field[rowScan][j] instanceof Doodlebug) {
//Check for last column
if (colScan >= 19) {
colScan = 0;
rowScan++;
return new int[] {rowScan - 1, 19};
} else {
colScan = j + 1;
return new int[] {rowScan, j};
}
}
}
rowScan++;
colScan = 0;
for (int i = rowScan; i < 20; i++) {
for (int j = colScan; j < 20; j++) {
if (field[i][j] instanceof Doodlebug) {
if (j >= 19) {
colScan = 0;
rowScan++;
return new int[] {i, 19};
} else {
colScan = j + 1;
return new int[] {i, j};
}
}
}
colScan = 0;
rowScan++;
}
rowScan = 0;
colScan = 0;
return new int[] {-1, -1};
}
public static void moveBug(Organism bug, int direction)
{
int rowPos = bug.getRowPosition();
int colPos = bug.getColPosition();
if (bug.getSymbol() == 'O') {
switch (direction)
{
case 1:
field[rowPos - 1][colPos] = new Ant((Ant) bug);
getBug(rowPos - 1, colPos).setRowPosition(rowPos - 1);
field[rowPos][colPos] = null;
rowPos--;
break;
case 2:
field[rowPos][colPos + 1] = new Ant((Ant) bug);
getBug(rowPos, colPos + 1).setColPosition(colPos + 1);
field[rowPos][colPos] = null;
colPos++;
break;
case 3:
field[rowPos + 1][colPos] = new Ant((Ant) bug);
getBug(rowPos + 1, colPos).setRowPosition(rowPos + 1);
field[rowPos][colPos] = null;
rowPos++;
break;
case 4:
field[rowPos][colPos - 1] = new Ant((Ant) bug);
getBug(rowPos, colPos - 1).setColPosition(colPos - 1);
field[rowPos][colPos] = null;
colPos--;
break;
}
} else if(bug.getSymbol() == 'X') {
switch (direction)
{
case 1:
field[rowPos - 1][colPos] = new Doodlebug((Doodlebug) bug);
getBug(rowPos - 1, colPos).setRowPosition(rowPos - 1);
field[rowPos][colPos] = null;
rowPos--;
break;
case 2:
field[rowPos][colPos + 1] = new Doodlebug((Doodlebug) bug);
getBug(rowPos, colPos + 1).setColPosition(colPos + 1);
field[rowPos][colPos] = null;
colPos++;
break;
case 3:
field[rowPos + 1][colPos] = new Doodlebug((Doodlebug) bug);
getBug(rowPos + 1, colPos).setRowPosition(rowPos + 1);
field[rowPos][colPos] = null;
rowPos++;
break;
case 4:
field[rowPos][colPos - 1] = new Doodlebug((Doodlebug) bug);
getBug(rowPos, colPos - 1).setColPosition(colPos - 1);
field[rowPos][colPos] = null;
colPos--;
break;
}
bug.timeSinceBreed++; //Increment counter for breeding
}
}
public void breed(Organism bug) {
//Check breed threshold and open adjacent cell
if ((bug.timeSinceBreed == bug.breedThreshold) && this.emptyCellAdjacent(bug)) {
int row = bug.getRowPosition();
int col = bug.getColPosition();
Integer[] randomDirections = Board.randomDirections();
boolean breedComplete = false;
//Check for type of bug, ant or doodlebug
if (bug instanceof Ant) {
Ant ant = (Ant) Board.getBug(row, col);
for (int i = 0; i < 4 && (breedComplete == false); i++) {
switch (randomDirections[i]) {
case 1:
if (ant.topFlag)
break;
if (Board.getBug(row - 1, col) == null) {
field[row - 1][col] = new Ant(row - 1, col);
breedComplete = true;
}
break;
case 2:
if (ant.rightFlag)
break;
if (Board.getBug(row, col + 1) == null) {
field[row][col + 1] = new Ant(row, col + 1);
breedComplete = true;
}
break;
case 3:
if (ant.bottomFlag)
break;
if (Board.getBug(row + 1, col) == null) {
field[row + 1][col] = new Ant(row + 1, col);
breedComplete = true;
}
break;
case 4:
if (ant.leftFlag)
break;
if (Board.getBug(row, col - 1) == null) {
field[row][col - 1] = new Ant(row, col - 1);
breedComplete = true;
}
break;
}
}
bug.timeSinceBreed = 0; //Reset breeding counter
numberOfAnts++;
}
else if (bug instanceof Doodlebug) {
Doodlebug db = (Doodlebug) Board.getBug(row, col);
for (int i = 0; i < 4 && (breedComplete == false); i++) {
switch (randomDirections[i]) {
case 1:
if (db.topFlag)
break;
if (Board.getBug(row - 1, col) == null) {
field[row - 1][col] = new Doodlebug(row - 1, col);
breedComplete = true;
}
break;
case 2:
if (db.rightFlag)
break;
if (Board.getBug(row, col + 1) == null) {
field[row][col + 1] = new Doodlebug(row, col + 1);
breedComplete = true;
}
break;
case 3:
if (db.bottomFlag)
break;
if (Board.getBug(row + 1, col) == null) {
field[row + 1][col] = new Doodlebug(row + 1, col);
breedComplete = true;
}
break;
case 4:
if (db.leftFlag)
break;
if (Board.getBug(row, col - 1) == null) {
field[row][col - 1] = new Doodlebug(row, col - 1);
breedComplete = true;
}
break;
}
}
bug.timeSinceBreed = 0; //Reset breeding counter
numberOfDoodlebugs++;
} else { //Catch error
System.out.println("Error in breed!");
System.exit(0);
}
}
}
public void starve(Doodlebug db) {
if (db.timeSinceEat >= Doodlebug.STARVE_THRESHOLD) {
this.removeBug(db.getRowPosition(), db.getColPosition());
}
}
}
**************************************
Simulator.java
import java.util.Arrays;
import java.util.Scanner;
public class Simulator {
public static void enterToContinue()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Press enter to continue: ");
keyboard.nextLine();
}
public static void eatAnt(int[] coordinates)
{
//Get Doodlebug from coordinates
Doodlebug db = (Doodlebug) Board.getBug(coordinates[0], coordinates[1]);
Integer[] randomDirections = Board.randomDirections(); //Random direction list
boolean moveComplete = false;
//Check four random directions for Ant, and eat first found
for (int i = 0; i < 4 && (moveComplete == false); i++)
{
switch (randomDirections[i])
{
case 1:
if (db.topFlag)
break;
if (Board.getBug(coordinates[0] - 1, coordinates[1]) instanceof Ant)
{
Board.field[coordinates[0] - 1][coordinates[1]] = null;
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 1);
moveComplete = true;
Board.numberOfAnts--;
}
break;
case 2:
if (db.rightFlag)
break;
if (Board.getBug(coordinates[0], coordinates[1] + 1) instanceof Ant)
{
Board.field[coordinates[0]][coordinates[1] + 1] = null;
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 2);
moveComplete = true;
Board.numberOfAnts--;
}
break;
case 3:
if (db.bottomFlag)
break;
if (Board.getBug(coordinates[0] + 1, coordinates[1]) instanceof Ant)
{
Board.field[coordinates[0] + 1][coordinates[1]] = null;
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 3);
moveComplete = true;
Board.numberOfAnts--;
}
break;
case 4:
if (db.leftFlag)
break;
if (Board.getBug(coordinates[0], coordinates[1] - 1) instanceof Ant)
{
Board.field[coordinates[0]][coordinates[1] - 1] = null;
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 4);
moveComplete = true;
Board.numberOfAnts--;
}
break;
}
}
}
public static void moveDoodlebug(int[] coordinates)
{
int row = coordinates[0];
int col = coordinates[1];
Doodlebug db = (Doodlebug) Board.getBug(row, col);
Integer[] randomDirections = Board.randomDirections();
boolean moveComplete = false;
for (int i = 0; i < 4 && (moveComplete == false); i++)
{
switch (randomDirections[i])
{
case 1:
if (db.topFlag)
break;
if (Board.getBug(coordinates[0] - 1, coordinates[1]) == null)
{
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 1);
moveComplete = true;
}
break;
case 2:
if (db.rightFlag)
break;
if (Board.getBug(coordinates[0], coordinates[1] + 1) == null)
{
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 2);
moveComplete = true;
}
break;
case 3:
if (db.bottomFlag)
break;
if (Board.getBug(coordinates[0] + 1, coordinates[1]) == null)
{
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 3);
moveComplete = true;
}
break;
case 4:
if (db.leftFlag)
break;
if (Board.getBug(coordinates[0], coordinates[1] - 1) == null)
{
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 4);
moveComplete = true;
}
break;
}
}
}
public static void moveAnt(int[] coordinates)
{
int row = coordinates[0];
int col = coordinates[1];
Ant ant = (Ant) Board.getBug(row, col);
Integer[] randomDirections = Board.randomDirections();
boolean moveComplete = false;
for (int i = 0; i < 4 && (moveComplete == false); i++)
{
switch (randomDirections[i])
{
case 1:
if (ant.topFlag)
break;
if (Board.getBug(coordinates[0] - 1, coordinates[1]) == null)
{
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 1);
moveComplete = true;
}
break;
case 2:
if (ant.rightFlag)
break;
if (Board.getBug(coordinates[0], coordinates[1] + 1) == null)
{
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 2);
moveComplete = true;
}
break;
case 3:
if (ant.bottomFlag)
break;
if (Board.getBug(coordinates[0] + 1, coordinates[1]) == null)
{
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 3);
moveComplete = true;
}
break;
case 4:
if (ant.leftFlag)
break;
if (Board.getBug(coordinates[0], coordinates[1] - 1) == null)
{
Board.moveBug(Board.getBug(coordinates[0], coordinates[1]), 4);
moveComplete = true;
}
break;
}
}
}
public static int[][] antPositions()
{
int[][] positions = new int[Board.numberOfAnts][2];
for (int i = 0; i < Board.numberOfAnts; i++)
{
positions[i] = Board.antScanner();
}
Board.rowScan = 0;
Board.colScan = 0;
return positions;
}
public static int[][] doodlebugPositions()
{
int[][] positions = new int[Board.numberOfDoodlebugs][2];
for (int i = 0; i < Board.numberOfDoodlebugs; i++)
{
positions[i] = Board.doodlebugScanner();
}
Board.rowScan = 0;
Board.colScan = 0;
return positions;
}
public static void printPositions()
{
System.out.println("Doodlebugs");
int [][] dbPositions = new int[Board.numberOfDoodlebugs][2];
dbPositions = doodlebugPositions();
for (int i = 0; i < Board.numberOfDoodlebugs; i++)
{
System.out.println(Arrays.toString(dbPositions[i]));
}
System.out.println("Ants");
int[][] positions = new int[Board.numberOfAnts][2];
positions = antPositions();
for (int i = 0; i < Board.numberOfAnts; i++)
{
System.out.println(Arrays.toString(positions[i]));
}
}
public static void turnSequence(Board testBoard)
{
int[][] dbPositions = new int[Board.numberOfDoodlebugs][2];
dbPositions = doodlebugPositions();
int[] position = new int[2];
for (int i = 0; i < Board.numberOfDoodlebugs; i++)
{
//Get Doodlebug Coordinates
position = dbPositions[i];
//position = testBoard.doodlebugScanner();
//Error checking
if (position[0] < 0 || position[1] < 0)
{
System.out.println("-1 from doodlebugScanner"); //debug
System.exit(0);
}
else if (position[0] > 19 || position[1] > 19)
{
System.out.println("20 from doodlebugScanner"); //debug
System.exit(0);
}
//Eat Ant if necessary
if (testBoard.antAdjacent((Doodlebug)Board.getBug(position)))
{
Doodlebug db = (Doodlebug)Board.getBug(position);
db.timeSinceEat = 0;
db.timeSinceBreed++;
eatAnt(position);
}
else
{
//Else move doodlebug
Doodlebug db = (Doodlebug)Board.getBug(position);
db.timeSinceEat++;
db.timeSinceBreed++;
moveDoodlebug(position);
}
}
int[][] antPositions = new int[Board.numberOfAnts][2];
antPositions = antPositions();
//Loop through all the ants
for (int i = 0; i < Board.numberOfAnts; i++)
{
//Get Ant position
position = antPositions[i];
if (position[0] < 0 || position[1] < 0)
{
break;
}
//Move Ant
Ant ant = (Ant)Board.getBug(position);
ant.timeSinceBreed++;
moveAnt(position);
}
Board.rowScan = 0;
Board.colScan = 0;
//For all the doodlebugs...
dbPositions = doodlebugPositions();
int tempNumberOfDoodlebugs = Board.numberOfDoodlebugs;
for (int i = 0; i < tempNumberOfDoodlebugs; i++)
{
//Get Doodlebug Coordinates
position = dbPositions[i];
int row = position[0];
int col = position[1];
if (row < 0 || col < 0)
{
break;
}
testBoard.breed(Board.getBug(position));
}
antPositions = antPositions();
int tempNumberOfAnts = Board.numberOfAnts;
for (int i = 0; i < tempNumberOfAnts; i++)
{
//Get Ant Coordinates
position = antPositions[i];
int row = position[0];
int col = position[1];
if (row < 0 || col < 0)
{
break;
}
testBoard.breed(Board.getBug(row, col));
}
dbPositions = doodlebugPositions();
tempNumberOfDoodlebugs = Board.numberOfDoodlebugs;
for (int i = 0; i < tempNumberOfDoodlebugs; i++)
{
testBoard.starve((Doodlebug)Board.getBug(dbPositions[i][0], dbPositions[i][1]));
}
}
public static void main(String[] args)
{
Board testBoard = new Board();
testBoard.printBoard();
while (Board.numberOfAnts != 0 || Board.numberOfDoodlebugs != 0)
{
turnSequence(testBoard);
testBoard.printBoard();
enterToContinue();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.