View Single Post
Old 09-3-2015, 06:45 AM   #16
Xayphon
sausage
FFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
Xayphon's Avatar
 
Join Date: Nov 2008
Location: Germany
Posts: 1,630
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Quote:
Originally Posted by sickufully View Post
Wow thanks man, that was a huge help. The whole array part of this is slowly starting to click into place in my head now and I think I have a better understanding of how this array works (providing that your non-existent c++ knowledge is correct but since you sound like you know what you're taking about, I'm assuming it is correct :P).
I'm just assuming that arrays work the same way in Java as they do in C++, since Java took a lot of influence from C and C++. Looking at the code, it did seem to me that they do sort of, so that's why I think I can help you with that part at least.

Quote:
Originally Posted by sickufully View Post
So basically what you're saying is that the Array called ENTITY[MAP_HEIGHT][MAP_WIDTH] is empty
Yes, the Array is completely useless until you actually fill it up like it's done in the for loop. Both MAP_HEIGHT and MAP_WIDTH, which are integers, only define the size of the Array that you mustn't step over. And obviously you only can define the size of an Array with a number.

Quote:
Originally Posted by sickufully View Post
So, when initialising the tilemap array with data elements, each time it moves to the next box, consecutive numbers are entered. This is what I mean:
______________________________________________
|__0__|__1__|__2__|__3__|__4__|__5__|__6__|__7__|
|__8__|__9__|__etc__|__etc__|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|

The only way I came to this conclusion is by these lines and the increment signs (row++ & col++).
Code:
for (unsigned int row = 0; row < MAP_HEIGHT; row++)

		for (unsigned int col = 0; col < MAP_WIDTH; col++)
I'm not sure how close I am to figuring this out but this seemed like a logical answer.
Not exactly. The values "row" and "col" are used to point to certain boxes (or: Indexes within the Array), as you've visualized them so nicely. "tileMap[row][col] = EMPTY" literally means nothing but "put EMPTY at index [row][col]", meaning that, if EMPTY really is simply 0, the whole array is filled with 0's.

The for-loop iterations are something along the lines of

row: 0
col: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, ...

row: 1
col: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, ...

row: 2
col: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, ...

row: 3
col: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, ...

and so on. After each iteration the Array is thus filled as follows:

tileMap[0][0] = EMPTY tileMap[1][0] = EMPTY ...
tileMap[0][1] = EMPTY tileMap[1][1] = EMPTY ...
tileMap[0][2] = EMPTY tileMap[1][2] = EMPTY ...
tileMap[0][3] = EMPTY tileMap[1][3] = EMPTY ...
tileMap[0][4] = EMPTY tileMap[1][4] = EMPTY ...
...

until the Array is finally completely filled with EMPTY.

Unfortunately, the following part confuses me a little in terms of what datatype the Array is in the end... but since that's what the Array is being filled with, the whole Array is probably, as FoJaR said, an array of entity_labels. The entities being different kinds of Tiles that will be used in your Tile Map. I missed that in my last post, sorry for that.

Quote:
Originally Posted by sickufully View Post
From what I gather, EMPTY is a value that equals 0.
Code:
enum entity_labels	{
	EMPTY = 0,
	WALL
};
If you're a bit irritated by the fact that the whole array is being filled with 0's, try imagining an actual Tile Map like in Terraria. Every 0 in the Array is an empty wall, so, something like sky or w/e. That empty wall needs to exist though in order to work with something like building. You want to be able to place a block anywhere on the screen, so you must be able to access the Array and replace that specific Array index the player wants to place a block on with, say, an Earth Tile (or WALL, as it is in your code, if you wish). That Earth Tile has a value or is labeled as entity "1". You then replace that 0 with a 1 and when you render that array to graphics, you will have a lonely Earth Tile sitting on your screen.

Visualized:

The Array after the for-loop (literally a little empty Tile Map):
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|

User clicks on a box on the screen:
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__X__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|

Convert clicking position to Array position and change the value at Array index tileMap[1][2] to 1
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__1__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|

And when you render it next time, you're going to have an Earth Tile where the 1 is (really simply spoken).

I know this goes way beyond your assignment, but I think it's an interesting example that shows how Arrays can be used while also showing the basics of using one. Who knows, maybe you get to program a Terraria clone like me last semester, haha...
Xayphon is offline   Reply With Quote