Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)
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.
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.
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.
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...
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.
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.
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.
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...




Comment