Vierox
Vierox

Reputation: 13

What is better way to store multiple data values in one object?

This is a difficult one to title.

Essentially i have the following code:

for (int x=0; x<width; x++)
        {
            for (int y=0; y<height; y++)
            {
                if (world[x][y] == 2) //Find all Planets ('2') in world
                {
                    size[count] = randInt(1024, 512); //Random Size
                    angle[count] = randFloat(4, 0); //Random Angle
                    hue[count] = randInt(360, 0); //Random Hue
                    count++;
                }
            }
        }

world[][] is an integer multi-dimensional array that currently holds randomly placed values from 0 to 2.

0 being nothing, 1 being a star and 2 being a planet.

randInt and randFloat give random Integer or Float values between their (max, min)

Therefor this for loop goes through the entire world[][] array and if it finds a planet (2) it performs the following code.

The code within the for loop is supposed to store values for each planet found so that they are all unique with their different values (i.e. size, angle, hue). With these values, I would later paint all planets on the world and while painting them I would like to be able to access these values depending on what planet is being painted and then use those values accordingly to change the parameters of how the planet will render.

What I don't know how to do is have all of these values stored into the one planet (2) so that I can have the planet permanently keep the values that i have specified to it. What is the best way of doing this?

If more code is needed please let me know.

Upvotes: 1

Views: 1458

Answers (1)

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

You can create a Planet class & a Star class to approach this in a more of an object oriented manner. For the example, you can use a Map of ArrayLists of Planets:

Planet.java

public class Planet {
    private int size;
    private int angle;
    private int hue;

    public Planet(int size, int angle, int hue) {
        this.size = size;
        this.angle = angle;
        this.hue = hue;
    }

    // Getters & setters
}

Star.java

// I'm not sure whether you are considering Stars as Planets in your 
// approach, but this is just an example so that you would be able to use 
// polymorphism in your code
public class Star extends Planet {
    // methods & properties
}

YourMainClass.java

Map<Integer, List<Planet>> map = new HashMap<>();
map.put(1, new ArrayList<>());
map.put(2, new ArrayList<>());
map.put(3, new ArrayList<>());

for (int x=0; x<width; x++)
{
    for (int y=0; y<height; y++)
    {
        if (world[x][y] == 1) //Find all Stars in world
        {
            // Taking in consideration the fact that Stars are Planets
            map.get(1).add(new Star(/*args*/));
        }
        else if (world[x][y] == 2) //Find all Planets ('2') in world
        {
            map.get(2).add(new Planet(
                    randInt(1024, 512),
                    randFloat(4, 0),
                    randInt(360, 0)
            ));
        }
    }
}

Upvotes: 2

Related Questions