DCZ
DCZ

Reputation: 1744

Storing coordinates and characteristics of objects in a 2D grid in an ArrayList

I have a grid, made as as an Array of Arrays holding 1 character each (char [][]).

On that grid, i built houses, taking up one or multiple blocks of the grid. I would like to store the location (grid coordinates) and characteristics (price, upkeep) in some sort of list.

The house object is part of an enum containing multiple objects with these characteristics:

public enum Building {
    HOUSE(width, height, price, upkeep, symbol)
    FACTORY(width, height, price, upkeep, symbol)
}

- width and height are the number of tiles in the grid it takes up - symbol is the Character that is used in the grid to represent the specific building

I thought to do it this way: (pseudocode)

ArrayList<house> buildings = new ArrayList<House>

But how could i add the location of the house (the x and y coordinates in this arrayList? Should I use another datatype?

Upvotes: 0

Views: 371

Answers (1)

Kent
Kent

Reputation: 195229

My feeling your enum should be BuildingType if your two house objects could have different properties, like what you mentioned location.

Create a Building class, which has something like int x; int y; and a BuildingType (the enum) indicating which kind of the building it is.

If it is necessary you can have super class Building and two (or more?) sub types (House, Factory, Apartment etc) In this case you don't need the enum any longer. All is up to your requirement.

Upvotes: 1

Related Questions