2014-03-13 60 views
2

好的我制作了2D地图阵列。它里面有狐狸和野兔。他们可以四处走动并死亡。我将狐狸和兔子存放在一个数组列表中,以便跟踪剩下的数量。我的问题是我如何能够改变狐狸或兔子在阵列上的位置。每一次狐狸和野兔都必须移动一个点与他们以前的地点相邻。
继承人到目前为止我的代码:如何更改2D阵列中物体的位置

import java.util.ArrayList; 
import java.util.Random; 
import java.util.Scanner; 

public class Map { 

     protected int numberOfRows; // number of rows in my map 
     protected int numberOfColumns; // number of columns in my map 
     protected int randomRow; 
     protected int randomColumn; 
     protected ArrayList<Fox> fox = new ArrayList<Fox>(); 
     protected ArrayList<Hare> hare = new ArrayList<Hare>(); 

     Animal map [][]; // creates an instance of my map 

     public Map(int rows, int columns) { // creates a constructor 
      numberOfRows = rows; 
      numberOfColumns = columns; 
      map= new Animal[rows][columns]; // puts my number of rows and columns in array 
      for(int i = 0; i < rows; i++) { 
       for(int k = 0; k < columns; k++) { 
        map[i][k] = new Animal(); // feels my map with null 
       } 
      } 
      generateFoxAndHares(); 
     } 

     public void print() { 
      // nested for loop to print my map 
      for(int i =0; i < numberOfRows; i++) { 
       for(int j = 0; j < numberOfColumns; j++) { 
        System.out.print(map[i][j].getDisplayChar()); 
       } 
       System.out.println(""); 
      } 

     } 


     public void generateFoxAndHares() { 
      int ranRow; // random row for hare or fox to go 
      int ranCol; // random column for hare or fox to go 
      Scanner myScanner = new Scanner(System.in); // take user input 
      int counter = 1; // counts how many times my while loop runs 

      Random r = new Random(); 

      for(int i = 0; i < 15; i++) { // puts foxs on my map 
       ranRow = r.nextInt(this.numberOfRows); 
       ranCol = r.nextInt(this.numberOfColumns); 
       fox.add(new Fox(ranRow, ranCol)); 
       map[ranRow][ranCol] = fox.get(i); 

      } 

      for(int i = 0; i < 15; i++) { // puts hares on map 
       ranRow = r.nextInt(this.numberOfRows); 
       ranCol = r.nextInt(this.numberOfColumns); 
       hare.add(new Hare(ranRow, ranCol)); 
       map[ranRow][ranCol] = hare.get(i); 
      } 

      print(); 
      System.out.println("Day: 0, Foxs: " + fox.size() + ", Hares: " + hare.size()); 


      while(hare.size() > 0 || fox.size() > 0) { // keeps running 
       System.out.println("Press any button to continue"); 
       String textString; 
       textString = myScanner.nextLine(); 
       print(); 
       System.out.println("Day: " + counter + ", Foxs: " + fox.size() + ", Hares: " + hare.size()); 
       counter++; 

      } 
     } 
} 

public class Animal { 
    protected char displayChar; 
    protected int xCord; 
    protected int yCord; 

    public char getDisplayChar() { 
     return displayChar; 
    } 

    public char getSymbol() { 

      return getDisplayChar(); 
    } 

    public int getxCord() { 
     return xCord; 
    } 

    public int getyCord() { 
     return yCord; 
    } 

} 

public class Fox extends Animal{ 
    public Fox(int x, int y) { 
     this.xCord = x; 
     this.yCord = y; 
     this.displayChar = 'F'; 
    } 
} 

public class Hare extends Animal{ 

    public Hare(int x, int y) { 
     this.xCord = x; 
     this.yCord = y; 
     this.displayChar = 'H'; 
    } 
} 

import java.util.ArrayList; 

public class FoxAndHaresMain { 

    public static void main(String[] args) { 
     Map map1 = new Map(20,20); 
     map1.generateFoxAndHares(); 

    } 
} 

我的继承人输出:

FF  H H  
    F H   F  
     HF   
F H    F 
    F H  F 
      F  F 
F  HH H   
     H   H 
HF  HH  F  
H  H  F 
Day: 0, Hares: 15, Foxes: 15 
+0

看起来你的动物是不可变的,所以你应该根据他们的新位置重新创建它们。你需要找到一个你想让他们移动的算法。 (在4个可能的方向上是随机的?边界怎么样?) –

+0

为什么动物存储坐标? (只是问,似乎他们不是真的需要)。假设它们不在被占领的领域上有效吗? (一只狐狸在被野兔占领的田野上移动是有道理的......其他的......不是那么多) – Marco13

+0

等等,你为什么用(ranRow,ranCol)制作狐狸和野兔?在地图上?在地图上的坐标总是只有(0,0)的坐标。 –

回答

1

假设你知道,有在位置(3,7),狐狸,你想将其移动到(3 ,8)。你可以这样做:

map[3][7] = new Animal(); 
map[3][8] = new Fox(3, 8); 

这将填补与动物先前由狐狸占据的位置(这是你如何初始化地图,所以我认为这是你想要什么时,既不是狐狸,也没有一只野兔在该位置存在),并且它“移动”狐狸(实际上,创建一个新的,因为你的动物知道他们的坐标,因此狐狸或兔子对象只对地图上的一个点有效)到新的位置。

这似乎很奇怪,动物包含他们当前的坐标。这可能是您想要在设计中重新考虑的事情,但很难说清楚。

我刚刚注意到你的fox1hare1对象是在动物放置在地图上的循环中创建的,实际上从来没有使用过。他们只会被垃圾收集,而ArrayLists坐标为(0,0)的坐标是唯一放置在地图上的坐标。这几乎肯定是你当前代码中的一个错误。动物不需要坐标,或者在地图上放置错误的物体。

+0

好吧,我拿出了两个方法,它们生成坐标(0,0)与现在采用(ranRow,ranCol)并将它们放在地图上的方法。我如何获得坐标,以便我可以在下一回合更新它们? – este9

+1

你可以通过调用它们的'getxCord()'和'getyCord()'方法来获得它们的坐标,但是你不能更新它们,因为没有坐标的设置器。这些对象是不可变的。你必须添加setters来使它们变为可变的,或者你必须用新的坐标来创建新的对象,或者你必须改变你的设计,使动物不知道它们的坐标。 –