基于java文本的世界移动元素

时间:2012-07-04 03:22:49

标签: java arrays text

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

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以一个char的形式表示。 'B'代表伯德,'。'对于蠕虫,对于狼来说是'W'。蠕虫可以向左和向右移动,记住它们进入的方向,如果它们撞到墙壁或阵列的末端/开始则反向。鸟在世界上对角移动。允许狼向任何方向移动。

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

0 个答案:

没有答案