Java - 最后创建的对象覆盖前一个对象

标签 java object overwrite

我正在开发一个地下城风格的游戏项目,并使用 2D 数组来创建房间的布局。我正在使用 map 生成类和点类来创建 map 并跟踪正在访问哪个房间。在生成时选择一个起点(通常只是 0,0 是 2D 数组的中心)和 boss 点(创建但未填充)。 map 生成本身工作正常,但由于某种原因,我的起点被老板点覆盖。我尝试过将起点默认为 (0,0),但没有成功。我尝试强制 boss 点也从 (0,0) 开始,这导致了空指针异常。我不确定为什么会发生这种情况,希望得到一些帮助。

map 生成类

/**********************************************
 * created by Intellij idea.
 * User: Kyle Castillo
 * Date: 2/14/2020
 * Time: 10:41 AM
 * Contact: kylea.castillo@calbaptist.edu
 ***********************************************/
import java.util.Random;
import java.util.Vector;

public class MapGeneration {

    private static Vector<Vector<Integer>> map;
    private Point startPnt;
    private Point bossPoint;
    private static Integer size;

    /************************************************************
     * Constructor class for Map Generation
     * The Size will always be an X by X square based on size
     * Starting Point is specified by the starting (x,y)
     * Rooms are designated by a numerical value
     * - A 0, indicating an empty space
     * - A 1, indicating a filled space with a room
     * - An 8, indicating the array boundary.
     ************************************************************/

    MapGeneration(int size, int startX, int startY) {

        startPnt = new Point(startX,startY);
        bossPoint = new Point();
        MapGeneration.size = size + 2;         //the additional two is to account for boundaries
        map = new Vector<>();

        //Check to prevent the creation of an array where the starting x and y are outside of the array bounds.
        if (startX == 0 || startX == size - 1 || startX == size)
            throw new IllegalArgumentException("Error, the starting X value " + startX + " is not allowed!");
        if (startY == 0 || startY == size - 1 || startY == size)
            throw new IllegalArgumentException("Error, the starting Y value " + startY + " is not allowed!");

        //Creation of the starting room layout.
        for (int row = 0; row < size; row++) {
            Vector<Integer> tmp = new Vector<>();
            for (int col = 0; col < size; col++) {
                //The first row, the first value of each row, the last value of each row, and the last row must be 8
                //to prevent the generation later from going out of the 2D array boundary.
                if (row == 0 || col == 0 || row == size - 1 || col == size - 1 ){
                    tmp.add(8);

                    //If the row and col match the starting value then this is the first room.
                } else if (row == startY && col == startX){
                   tmp.add(1);
                } else {
                    //Empty space that can be filled with a room.
                    tmp.add(0);
                }
            }
            map.add(tmp);
        }
    }

    /*********************************************************************************
     * The generate map populates the map based on the desired number of rooms.
     * The number of rooms cannot exceed the maximum space available within the map.
     * If the number of rooms fits then the map is generated from the starting point.
     *********************************************************************************/

    public void generateMap(int numRooms) {
        //Checking to make sure that the number of rooms does not exceed the total number of empty spaces.
        if (numRooms > ((map.size() - 2) * (map.get(0).size() - 2))) {
            throw new IllegalArgumentException("Error, room amount exceeds map space!");
        }

        int x = startPnt.getX();
        int y = startPnt.getY();
        Point crntPnt = new Point(x,y);
        bossPoint = crntPnt;

        //Based a random number 0-3 a direction is chosen to create a new room.
        Random randDirection = new Random();
        int compass;
        while (numRooms != 0) {
            compass = randDirection.nextInt(4);
            switch (compass) {
                case 0: //Compass Index is North, indicates a shift in ROW + 1
                    int nextPos = map.get(crntPnt.getY() + 1).get(crntPnt.getX());
                    if(nextPos == 8){
                        //do nothing, its the map boundary
                    }
                    //As long as the next spot isn't already filled, create a room
                    else if (nextPos != 1) {
                        map.get(crntPnt.getY() + 1).set(crntPnt.getX(),1);
                        crntPnt.setY(crntPnt.getY() + 1);
                        //If the current point is further from the start point then make the boss point the current point
                        if (bossPoint.distance(startPnt) < crntPnt.distance(startPnt))
                            bossPoint = crntPnt;
                        numRooms--;
                        //If the next position is 1 move the current position but do not fill the spot or
                        //decrease the number of rooms left to make.
                    } else if (nextPos == 1) {
                        crntPnt.setY(crntPnt.getY() + 1);
                    }
                    break;

                /****************************************
                 * The rest of the cases function the exact
                 * same way as the first.
                 ****************************************/

                case 1: //Compass Index is East, indicates a shift in COL + 1
                    nextPos = map.get(crntPnt.getY()).get(crntPnt.getX() + 1);
                    if(nextPos == 8){
                        //do nothing
                    }
                    if (nextPos != 1) {
                        map.get(crntPnt.getY()).set(crntPnt.getX() + 1,1);
                        crntPnt.setX(crntPnt.getX() + 1);
                        if (bossPoint.distance(startPnt) > crntPnt.distance(startPnt))
                            bossPoint = crntPnt;
                        numRooms--;
                    } else if (nextPos == 1) {
                        crntPnt.setX(crntPnt.getX() + 1);
                    }
                    break;
                case 2: //Compass Index is South, indicates a shift in ROW - 1
                    nextPos = map.get(crntPnt.getY() - 1).get(crntPnt.getX());
                    if(nextPos == 8){
                        //do nothing
                    }
                    if (nextPos != 1) {
                        map.get(crntPnt.getY() - 1).set(crntPnt.getX(),1);
                        crntPnt.setY(crntPnt.getY() - 1);
                        if (bossPoint.distance(startPnt) > crntPnt.distance(startPnt))
                            bossPoint = crntPnt;
                        numRooms--;
                    } else if (nextPos == 1) {
                        crntPnt.setY(crntPnt.getY() - 1);
                    }
                    break;
                case 3: //Compass Index is West, indicates a shift in COL - 1
                    nextPos = map.get(crntPnt.getY()).get(crntPnt.getX() - 1);
                    if(nextPos == 8){
                        //do nothing
                    }
                    if (nextPos != 1) {
                        map.get(crntPnt.getY()).set(crntPnt.getX() - 1,1);
                        crntPnt.setX(crntPnt.getX() - 1);
                        if (bossPoint.distance(startPnt) > crntPnt.distance(startPnt))
                            bossPoint = crntPnt;

                        numRooms--;
                    } else if (nextPos == 1) {
                        crntPnt.setX(crntPnt.getX() - 1);
                    }
                    break;
            }
        }
        map.get(bossPoint.getY()).set(bossPoint.getX(),2);
    }

    @Override
    public String toString() {
        int sizeTemp = map.get(0).size();
        for (int row = 0; row < sizeTemp; row++) {
            System.out.print("[ ");
            for (int col = 0; col < sizeTemp; col++) {
                int type = map.get(row).get(col);
                System.out.print(type + " ");
            }
            System.out.print("]\n");
        }
        return "";
    }

    public static void main(String[] args) {
        MapGeneration map = new MapGeneration(11, 5, 5);
        System.out.println("Empty Map:");
        System.out.println(map);
        System.out.println("Starting point prior to map generation: " + map.startPnt);
        map.generateMap(10);
        System.out.println(map);
        System.out.println("The starting room is at " + map.startPnt);
        System.out.println("The boss room is at " + map.bossPoint);
        System.out.println("The distance to the boss room is: " + (int) map.startPnt.distance(map.bossPoint));
    }
}

点等级

/**********************************************
 * created by Intellij idea.
 * User: Kyle Castillo
 * Date: 3/4/2020
 * Time: 9:04 AM
 * Contact: kylea.castillo@calbaptist.edu
 ***********************************************/

public class Point {
    private static int x;
    private static int y;

    Point(){
        //default constructor
    }

    Point(int x, int y){
        this.x = x;
        this.y = y;
    }

    public static int getX(){
        return x;
    }

    public static int getY(){
        return y;
    }

    public void setX(int x){
        this.x = x;
    }

    public void setY(int y){
        this.y = y;
    }

    public static double distance(Point b){
        int bX = b.getX();
        int bY = b.getY();
        return Math.sqrt((Math.pow((bX - getX()),2.0) + Math.pow( bY - getY(), 2.0)));
    }

    @Override
    public String toString(){
        return "(" + getX() + ", " + getY() + ")";
    }
}

最佳答案

欢迎来到堆栈溢出。您在代码中的错误是,xy 变量在Point 类中标记为静态。那么它们将是类变量,而不是实例变量。即使您创建了 Point crntPnt = new Point( x, y ) 变量的新实例,xy 也不属于该特定实例。

只需如下更改 Point 类,并确保也更改 getter 和 setter

public class Point {
  private  int x;
  private  int y;


  public  int getX(){
    return x;
  }

  public  int getY(){
    return y;
  }

  public void setX(int x){
    this.x = x;
  }

  public void setY(int y){
    this.y = y;
  }
}

作为旁注,行 map.get( crntPnt.getY() - 1 ).set( crntPnt.getX(), 1 ) 将抛出 ArrayIndexOutOfBounds如果 crntPnt.getY() 值为 0,则会出现异常。因此您可能也想处理该问题。

关于Java - 最后创建的对象覆盖前一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61299098/

相关文章:

java - 连接错误 Firefox (V39) 和 Selenium Webdriver (2.47.1)

console - 返回 REPL 中行的开头

java - 客户端服务器应用程序java

Java 方法在一种情况下可以正确地看到继承,但在另一种情况下却不能,罪魁祸首是什么?

php - 如何在 PHP 中向对象添加新的键值对?

javascript - 如何将一个变量分配给对象内的另一个变量 vanilla JavaScript

c++ - 创建 C++ 对象

c# - 覆盖文件只能工作一次

Javascript/jQuery : nesting loops, 防止覆盖对象属性

java - 无法使用 BubbleTextView