I'm a beginner in Java and I'm trying to make my own very simple game "BomberMan".
Here are classes I've created:
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;
}
}
The result of code above looks like:
[ ] [ ] [ ] [ ] [ ]
[B] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ]
B
is our Bomberman, and [ ]
- empty cells of 2D array.
Abstract class for figures:
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);
}
The Bomberman I'm trying to make a step:
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");
}
}
Class to get Integer[]
coordinate to make a step:
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;
}
}
And a class where I'm running my code:
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++;
}
}
}
The problem is that I'm having NPE here (Bomberman's move method):
gameField.getGameField()[x][y] = null;
On that coordinate we should have our Bomberman. I make this null and then create a new figure on a new coordinate. Each figure has x and y coordinate equals to our 2D array (gamefield) first and second coordinate. But in Run I say:
if (gameField.getGameField()[i][j] != null)
(That means we must have a figure there which can move).
So that cell should not be null and should not throw a NPE. What is wrong?