Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The code below creates 3 3x3 boxes of . and adds an ant (A) in a random location. You can run the code and realize

The code below creates 3 3x3 boxes of "." and adds an ant ("A") in a random location. You can run the code and realize what I mean.

I want to be able to, in Java, 1) make sure the space is null and generate a random direction in the grid array, 2) let Ant move to a random direction using the move method and 3) finish the move method in Doodlebug with the same behavior.

For example, to go up in the grid, it would say grid [ant.x-1] [ant.y]

down: grid [ant.x+1] [ant.y]

left: grid [ant.x] [ant.y-1]

right: grid [ant.x] [ant.y+1]

Also, I wasn't sure how to post the code, it came out in a weird table, I apologize.

-------------------------------------------------------------

abstract public class Critter {
protected World myWorld; // know in which world the critter lives
protected int x, y; // Position in the world
abstract public void move();
public Critter(World world, int rIndex, int cIndex){
myWorld = world;
x = rIndex;
y = cIndex;
}
public abstract void display();
}

-----------------------------------------------------------------------------

import java.util.Random;
public class Ant extends Critter {
public Ant(World world, int rIndex, int cIndex) {
super(world, rIndex, cIndex);
}
public void move(){
//System.out.println("ant move");
//todo:
// Check whether the cell below is available
//
// If the cell below is available
//(the cell is not out of boundary and is empty),
// put the ant to the next cell
// Else: the ant does not move
int gridsize = this.myWorld.getGridSize();
int newx = this.x+1;
if(newx >= gridsize)
{ return;
}
Critter cell = this.myWorld.getCell(this.x+1,this.y);
if( cell == null)
this.myWorld.setCell(this.x+1, this.y, this);
this.myWorld.setCell(this.x, this.y, null);
}
public void display()
{
System.out.print('A');
}
//if idex is (i, j) -- to get to the cell below it (i +1, j)
}

---------------------------------------------------------------------

public class Doodlebug extends Critter {
public Doodlebug(World world, int rIndex, int cIndex) {
super(world, rIndex, cIndex);
}
public void move(){
//System.out.println("doodlebug move");
}
@Override
public void display()
{
System.out.print('D');
}

}

-------------------------------------------------

public class Main {
public static void main(String[] argv){
World world = new World(3);
world.AssignCritters(4, 1);
world.Display();
System.out.println(" ");
world.oneStepSimulation();
world.Display();
}
}

-------------------------------------------------------------------

import java.util.Random;
public class World {
private Critter[][] grid; // Array of organisms for each getCell
public Critter getCell(int i, int j){
return grid[i][j];
}
public void setCell(int x, int y, Critter c) {
this.grid[x][y] = c;
}
public int getGridSize(){
return grid.length;}
public World(int gridsize){
grid = new Critter[gridsize][gridsize];
for(int i = 0; i < gridsize; i++){
for(int j = 0; j < gridsize; j++){
grid[i][j] = null;
}
}
}
public void Display(){
for (int i=0; i < grid.length; i++)
{
for (int m = 0; m < grid.length; m++)
{
if (grid [i] [m] == null)
System.out.print(" .");
else
{ grid [i] [m].display();
}
}
System.out.print(" ");
}
}
public static void main(String[] argv){
World world = new World(5);
world.AssignCritters(10, 15);
world.Display();
}
public void oneStepSimulation(){
for (int i = 0; i < grid.length; i++){
for (int j = 0; j < grid.length; j++){
if (grid[i][j]!=null){
grid[i][j].move();
}
}
}
}
public void AssignCritters(int antNumber, int critterNumber)
{
Random r = new Random();
int rIndex = r.nextInt(grid.length);
int cIndex = r.nextInt(grid[rIndex].length);
for (int i=0; i < antNumber; i++)
if ( grid [rIndex] [cIndex] == null) {
grid[rIndex] [cIndex] = new Ant(World.this, rIndex, cIndex); }
// else { Ant.move();
//}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago