Question
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'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} -------------------------------------------------
-------------------------------------------------------------------
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started