1

我有一个项目,我一生都无法弄清楚移动模式,某个对象是否与另一个对象在同一个地方,或者如何与每个项目进行交互,这是第一类:

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++) {
        showWorld(animals);
        doEating(animals);
        doMoving(animals);
    }
} 

public static void showWorld(Animal[] animals) {
    System.out.println();
    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
                        localAnimals++;
                        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(" ");
            }
            System.out.print("|");
        }
        System.out.println();        
    }
    System.out.println();
}

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

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


}

这是我编码的第二部分:

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
 }

}

每个子类被命名为 Worm、Bird 和 Wolf。每个子类 toString 都以一个字符的形式表示。'B' 代表鸟,'。代表蠕虫,而“W”代表狼。蠕虫可以左右移动,记住它们前进的方向,如果它们撞到墙壁或阵列的结束/开始,就会反转。小鸟在世界上沿对角线移动。狼可以向任何方向移动。

我只需要帮助开始在 doMoving() 中进行动作,帮助识别 isInSamePlaceAs() 并帮助 doEating()。鸟吃虫,狼吃鸟,虫什么都不做。

4

0 回答 0