Jump to content

Best way to do this? [Java]


Recommended Posts

This is probably a really newbie question but what is the best way to do a map that has to contain two arrays.

 

I will try to clarify. I have a map that is X squares wide and X squares high (X = a variable passed to it which can change).

 

I was thinking that the easiest way would be something like an array or vector with X*X elements and then each element having an array of what the map tile holds but different map tiles can hold different amounts of data.

I have two vectors:

mapArray and mapSquare.

 

I add elements to mapSquare (a and b plus the number the loop is on) so a0 and b0 then a1,b1 etc.

The only problem is that every element in the primary array/vector is the exact same if I do .clear() on the vector.

If i don't do .clear() on the mapSquare then the mapSquare works but has the previous elements plus all the new ones so a0,b0 even when I want the mapSquare to contain only like a1,b1.

 

Oh dear I don't think anyone is going to be able to understand this :(.

 

I will try to clarify any bits that anyone is still confused on.

Link to comment
Share on other sites

I am guessing that I did confuse everyone :(

 

Here is what i have been playing with:

 

Object map[mapSize][];

for(int i=0;i<mapSize;i++) {

map[i][] = {"Element 1","Object 2","Item 3"};
}

 

But the code errors at the map[] ={...}; bit.

 

 

The error is "Syntax error, insert "}" to complete Block"

 

Hope that clarifies things now.

Link to comment
Share on other sites

I am wanting to create a game and this is the only thing that I have found hard.

 

The map is of an area of land. Each tile will hold information like what is on that tile such as whether is a building and if so what building or if it doesn't have a building on it then does it have building rights.

 

The first index would be the tile ID and then the second one would be the actual stuff there.

So like:

 

 

map[] = {true,none, false};

 

where the first value is if it is owned, the second is what is built there and the final one is the status of building rights.

 

 

Hope that makes sense. I have a horrible tendancy to waffle and make no sense.

Link to comment
Share on other sites

Instead of storing that info in an array, why not store it in an object?*  It just seems like a simpler way to look at it, and it would increase readability.  I mean, how would you access the info stored in a tile with the way you have it now?  Something like:

 

(map[1][5])[0]; // whatever that's supposed to return

 

Right?

 

With a tile object, you could have a method that makes it a lot easier to see what's going on:

 

map[1][5].GetId(); //return the id of the tile at coordinates 1-5

 

This would also allow you to plug in other functionality into each individual tile.  Are some tiles due for a buff/debuff?  Add an observer.  Do some give different bonuses?  Wrap a decorator around them.

 

The point is to not fight against Java's OO nature.  Arrays are a useful tool, but not the only one available to you.

 

Regarding your syntax error, I can't help you there.  I don't know Java.  My mostly-OO language of choice is C#, which is pretty similar to Java, from what I hear.

 

*Yes, I know that arrays are objects in Java.  I mean a proper object. ;-)

Link to comment
Share on other sites

hmm That is a good idea to have a tile class. It would definantly make it easier. I have problems with OOP sometimes as when I first started programming they taught us procedural and I am now trying to learn OOP style.

 

Thank you for the advice.

 

Doing it with a tile class also means there is no worrying about the syntax error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.