Java - последний созданный объект перезаписывает предыдущий - PullRequest
0 голосов
/ 19 апреля 2020

Я работаю над игровым проектом в стиле подземелья и использую 2D-массив для создания планировки комнат. Я использую класс генерации карты и класс точек для создания карты и отслеживания того, к какой комнате осуществляется доступ. Начальная точка выбирается при генерации (обычно просто 0,0, являющейся центром 2D-массива) и точка для босса (которая создана, но не заполнена). Само генерирование карты работает нормально, но по какой-то причине моя отправная точка перезаписывается точкой босса. Я попытался установить начальную точку по умолчанию (0,0), и это не сработало. Я попытался заставить точку босса также начинаться с (0,0), что привело к исключению нулевого указателя. Я не уверен, почему это происходит, и был бы признателен за помощь.

Класс MapGeneration

/**********************************************
 * 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));
    }
}

Класс Point

/**********************************************
 * 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() + ")";
    }
}

1 Ответ

0 голосов
/ 19 апреля 2020

Добро пожаловать в переполнение стека. Ваша ошибка в коде: x и y переменные помечены как stati c в Point классе. Тогда они будут переменными класса, а не переменными экземпляра. Даже если вы создаете новый экземпляр Point crntPnt = new Point( x, y ) переменных x, а y не принадлежит этому конкретному экземпляру.

Просто измените класс Point, как показано ниже, и не забудьте также изменить геттеры и сеттеры

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. Таким образом, вы можете также обработать это.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...