如何在2D数组中更改对象的位置

时间:2014-03-13 21:50:46

标签: java inheritance

好的我正在制作2D地图数组。它里面有狐狸和野兔。他们可以四处走动而死。我将狐狸和野兔存放在一个arraylist中,以记录剩下的数量。我的问题是如何在阵列上切换我的狐狸或野兔的位置。每转一圈,狐狸和野兔必须移动一个位置,与之前的位置相邻 到目前为止,我的代码是:

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

1 个答案:

答案 0 :(得分:1)

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

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

这将填充狐狸以前用动物占据的位置(这就是你初始化你的地图的方式,所以我认为那是你想要的,当那个地方既没有狐狸也没有野兔),它“移动”Fox(实际上,创建一个新的,因为你的动物知道它们的坐标,因此Fox或Hare对象只对地图上的一个点有效)到新位置。

动物包含它们当前的坐标似乎很奇怪。您可能想在设计中重新考虑,但很难说。

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