mundo basado en texto java moviendo elementos alrededor

I have a project that i cannot for the life of me figure out the moving pattern, whether some object is in the same place as another, or how to interact with each project, here is the first class:

public class AnimalKingdom {

public static final int WORLD_ROWS = 4; 
public static final int WORLD_COLUMNS = 4;

public static final int ROUNDS = 10;

public static void main(String[] args) {
    Animal[] animals = new Animal[10];
    animals[0] = new Worm(WORLD_ROWS, WORLD_COLUMNS);        
    animals[1] = new Worm(WORLD_ROWS, WORLD_COLUMNS);
    animals[2] = new Worm(WORLD_ROWS, WORLD_COLUMNS);        
    animals[3] = new Worm(WORLD_ROWS, WORLD_COLUMNS);        
    animals[4] = new Bird(WORLD_ROWS, WORLD_COLUMNS);        
    animals[5] = new Bird(WORLD_ROWS, WORLD_COLUMNS);
    animals[6] = new Bird(WORLD_ROWS, WORLD_COLUMNS);
    animals[7] = new Bird(WORLD_ROWS, WORLD_COLUMNS);                    
    animals[8] = new Wolf(WORLD_ROWS, WORLD_COLUMNS);
    animals[9] = new Wolf(WORLD_ROWS, WORLD_COLUMNS);   

    for (int i = 1; i < ROUNDS; i++) {

public static void showWorld(Animal[] animals) {
    System.out.println("The World");
      /*  The world is made of rows and columns.
          Each location must be big enough to list all the animals
            (in case they all show up at that spot at once).  So we print
            the single character string for each animal, then later add in enough
            blanks to ensure that the lines between locations are neatly 
            drawn. */
    for (int r = 0; r < WORLD_ROWS; r++) {
        for (int c = 0; c < WORLD_COLUMNS; c++) {

            int localAnimals = 0;
            for (int a = 0; a < animals.length; a++) {
                if (animals[a] != null) {  // as animals die, nulls will be left in the array
                    int ar = animals[a].getRow();
                    int ac = animals[a].getCol();
                    if (r == ar && c == ac) {  // this animal is local to this location
                        System.out.print(animals[a]);  // draw the animal
                 // create enough blanks to fill out the location
            for (int i = 0; i < animals.length-localAnimals; i++) {
                System.out.print(" ");

public static void doEating(Animal[] animals) {
    // This needs to be filled in    

public static void doMoving(Animal[] animals) {    
    // This needs to be filled in


And here is the second part of my coding:

import java.util.Random;

public class Animal {

private Random rand = new Random();
private int worldWidth;
private int worldHeight;
private int row;
private int col;

public Animal(int worldHeight, int worldWidth) {
    this.worldHeight = worldHeight;    
    this.worldWidth = worldWidth;  
    row = rand.nextInt(worldHeight);   
    col = rand.nextInt(worldWidth);            

public boolean willEat(Animal anim) {
    return false;

public void move() {

public int getRow() {
    return row;

public int getCol() {
    return col;

public void setRow(int r) {
    row = r;

public void setCol(int c) {
    col = c;

public String toString() {
    return "";

public int getWorldWidth(){
    return worldWidth;

public int getWorldHeight(){
    return worldHeight;

public boolean isInSamePlaceAs(Animal other) {
    return false;   // code needs to be replaced


Each subclass is named Worm, Bird, and Wolf. Each subclass toString is represented in the form of one char. 'B' for Bird, '.' for Worm, and 'W' for Wolf. The worms can move left and right, remembering the direction they are going in and reverse if they hit a wall or end/beginning of array. The Bird moves diagonally in the world. The Wolf is allowed to move in any direction.

I just need help with starting to make movements in the doMoving(), help identify isInSamePlaceAs() and help doEating(). Birds eat worms, Wolves eat Birds, and worms do nothing.

preguntado el 04 de julio de 12 a las 04:07

can you show your problem on abstract level ? -

Is it correct that you have four worms, four birds and two wolves? Where do you set the initial positions of all the animals? The idea of representing the world as an array is OK, but you only need one array where each cell contains a Cell object which itself has containers for the various animal types. Then implementing eating and moving is a question of iterating over the single 2d array, looking in each non-empty cell and determining what eats what and then adjusting cell contents by discarding eaten animals. You seem to have loops and iteration, so you should be all set. -

This problem is the given situation. I've slightly figured out that overriding the subclass of animals move() initiates the movement. The array is set in a single array with a point system established. The world is created with each animal randomly generated. i am just trying to figure out how to check that they are both on the same square. I haven't learned iteration yet. -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.