I'm trying to have a program that maps terrain in java. Just now it's flagging an error in the main program:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Map.run(Map.java:35) at Terrain.run(Terrain.java:31) at Terrain.main(Terrain.java:10)
I've just started using enum and it's only started t flag up when I implemented some into my code. Here is the main class:
import java.util.Scanner;
public class Terrain {
public static void main(String[] args) {
Terrain t = new Terrain();
t.init();
t.run();//Flags here
}
private Map map;
public void init()
{
map = new Map();
Scanner in = new Scanner(System.in);
System.out.println("Please enter a height");
map.setHeight(in.nextInt());
System.out.println("Please enter a width");
map.setWidth(in.nextInt());
System.out.println("Please enter a hedge size");
map.setHedgeSize(in.nextInt());
System.out.println("Please enter an entrance size");
map.setEntranceSize((in.nextInt()/2));
in.close();
}
public void run()
{
map.run();//And flags here
}
}
And heres the map class:
public class Map {
private int height;
private int width;
private int area;
private int passCount;
private int hedgeSize;
private int entranceSize;
private int percentageOfPassable;
TerrainType [][] terrain;
TerrainType tile;
public Map()
{
height=0;
width=0;
area=0;
passCount=0;
hedgeSize=0;
entranceSize=0;
percentageOfPassable=0;
terrain = new TerrainType[width][height];
}
public void run()
{
for (int y=0; y<height; y++)
{
String mapDisplay = new String();
for (int x=0; x<width; x++)
{
area++;
terrain[x][y]=TerrainType.GRASS;
if (x<hedgeSize||x>((width-1)-hedgeSize))
{
terrain[x][y]=TerrainType.HEDGE;
}
if (y<hedgeSize||y>((height-1)-hedgeSize))
{
terrain[x][y]=TerrainType.HEDGE;
}//Sets border with regard to hedge size
if (y>((height-1)-hedgeSize))
{
if (x>=((width/2)-entranceSize)&&x<((width/2)+entranceSize))
{
terrain[x][y]=TerrainType.GRASS;
}
}//Entrance size+position with regard to hedge size
if(terrain[x][y].getTerrainPassable())
{
passCount++;
terrain[x][y]=TerrainType.GRASS;
}
mapDisplay+=terrain[x][y].getChar();
}
System.out.println(mapDisplay);
}//End nested for loop to set initial values
percentageOfPassable=passCount*100/area;
System.out.println(percentageOfPassable+"%");
}
public void setHeight(int height)
{
this.height=height;
}
public void setWidth(int width)
{
this.width=width;
}
public void setEntranceSize(int entranceSize)
{
this.entranceSize=entranceSize;
}
public void setHedgeSize(int hedgeSize)
{
this.hedgeSize=hedgeSize;
}
}
And finally my enum:
import java.awt.Color;
public enum TerrainType
{
GRASS(',', true, Color.GREEN), ROCK('#', false, Color.GRAY),
HEDGE('H', false, Color.YELLOW), WATER('W', false, Color.BLUE);
private char terrainChar;
private boolean terrainPassable;
private Color colour;
private TerrainType(char terrainChar, boolean terrainPassable, Color colour) {
this.terrainChar = terrainChar;
this.terrainPassable = terrainPassable;
this.colour = colour;
}
public char getChar() {
return terrainChar;
}
public boolean getTerrainPassable() {
return terrainPassable;
}
public Color getColour() {
return colour;
}
}
It is due to your terrain is initialized as TerrainType[0][0] in the constructor which results in a empty array and wasn't reinitialized later during run() after the user input their desired values hence it stays as an empty array.
height = 0;
width = 0;
terrain = new TerrainType[width][height];
You can move it to your run() method on your Map.java instead so that i would generate a array with the size specified by the user.
public void run() {
terrain = new TerrainType[width][height];
...
}