2017-04-10 52 views
-3

我是一个Java初学者,我正在尝试制作自己非常简单的游戏“BomberMan”。Npe同时在游戏领域移动一个数字(java)

这里是我创建的类:

public class GameField { 

    private Figure[][] gameField; 

    public Figure[][] getGameField() { 
     return gameField; 
    } 

    public void createField(int size) { 
     this.gameField = new Figure[size][size]; 
     setBomberMan(); 
    } 

    private void setBomberMan() { 
     int cellX = new Random().nextInt(gameField.length); 
     int cellY = new Random().nextInt(gameField.length); 
     Bomberman hero = new Bomberman(cellX, cellY, "Bomberman"); 
     this.gameField[cellX][cellY] = hero; 
    } 
} 

的代码,结果上面的样子:

[ ] [ ] [ ] [ ] [ ] 
[B] [ ] [ ] [ ] [ ] 
[ ] [ ] [ ] [ ] [ ] 
[ ] [ ] [ ] [ ] [ ] 
[ ] [ ] [ ] [ ] [ ] 

B是我们的炸弹,并[ ] - 二维数组的空单元格。

的数字抽象类:

abstract class Figure { 

    private int x; 
    private int y; 
    private String name; 
    protected GameField gameField; 

    protected Figure(int x, int y, String name) { 
     this.x = x; 
     this.y = y; 
     this.name = name; 
    } 

    public int getX() { 
     return x; 
    } 

    public int getY() { 
     return y; 
    } 

    public String getFigureName() { 
     return name; 
    } 

    abstract void move(int x, int y); 
} 

的炸弹人,我想做一个步骤:

public class Bomberman extends Figure { 

    protected Bomberman(int x, int y, String name) { 
     super(x, y, name); 
    } 

    @Override 
    void move(int x, int y) { 
     StepMaker step = new StepMaker(); 
     Integer[] newPosition = step.getDestination(x, y, null); 


     //The problem is in the line below: 
     gameField.getGameField()[x][y] = null; 


     int a = newPosition[0]; 
     int b = newPosition[1]; 
     gameField.getGameField()[a][b] = new Bomberman(a, b, "Bomberman"); 
    } 
} 

类获取Integer[]协调做出步:

public class StepMaker { 

    private final EnumMap<Step, Integer[]> steps = new EnumMap<>(Step.class); 

    private void fillEnumMap() { 
     this.steps.put(Step.UP, new Integer[]{-1, 0}); 
     this.steps.put(Step.DOWN, new Integer[]{1, 0}); 
     this.steps.put(Step.LEFT, new Integer[]{0, -1}); 
     this.steps.put(Step.RIGHT, new Integer[]{0, 1}); 
    } 

    public Integer[] getDestination(int x, int y, Step step) { 
     fillEnumMap(); 
     int destX, destY; 
     Integer[] way; 
     if (step != null) { 
      way = this.steps.get(step); 
      destX = x + way[0]; 
      destY = y + way[1]; 
     } else { 
      way = this.steps.get(choose()); 
      destX = x + way[0]; 
      destY = y + way[1]; 
     } 
     return new Integer[]{destX, destY}; 
    } 

    private Step choose() { 
     return Step.values()[ThreadLocalRandom.current().nextInt(Step.values().length)]; 
    } 

    private enum Step { 
     /** 
     * Possible ways to make step. 
     */ 
     UP, DOWN, LEFT, RIGHT; 
    } 
} 

和我正在运行我的代码的课程:

public class Run { 
    public static void main(String[] args) { 
     GameField gameField = new GameField(); 
     gameField.createField(5); 

     int p = 0; 
     while (p != 1) { 
      for (int i = 0; i < gameField.getGameField().length; i++) { 
       for (int j = 0; j < gameField.getGameField()[i].length; j++) { 
        if (gameField.getGameField()[i][j] != null) { 
         gameField.getGameField()[i][j].move(i, j); 
        } 
       } 
      } 
      gameField.printField(gameField.getGameField()); 
      p++; 
     } 
    } 
} 

的问题是,我在这里NPE(炸弹的举动法):

gameField.getGameField()[x][y] = null; 

在该坐标,我们应该有我们的炸弹。我将它设为null,然后在新坐标上创建一个新图形。每个图形的x和y坐标等于我们的二维数组(游戏场)的第一和第二坐标。但在运行我说:

if (gameField.getGameField()[i][j] != null) 

(这意味着我们必须有一个可以移动的数字)。

因此,单元格不应该为空,不应该抛出NPE。哪里不对?

+3

您的'main'方法中的'GameField'对象与'Figure'中的对象不是同一个对象,第二个对象不会被初始化并且是'null'。 – Berger

回答

2

看来gameField未设置为您的BomberMan。有一个额外的ARG

protected Figure(int x, int y, String name, Figure[][] gameField) { 
    this.x = x; 
    this.y = y; 
    this.name = name; 
    this.gameField = gameField; 
} 

,然后创建一个新的炸弹人:您可以扩展BomberMan的和Figure的构造函数(添加gameField作为ARG)这样的:

protected Bomberman(int x, int y, String name, Figure[][] gameField) { 
    super(x, y, name, gameField); 
} 

而且

Bomberman hero = new Bomberman(cellX, cellY, "Bomberman", gameFieldValue);