| 04-19-2003, 03:23 PM | #1 |
I need to know how to create a 2 dimensional region array variable. I know that JASS does not allow 2 dimensional arrays, but i've seen people create them anyway through arithmetic. If anyone could help me out on how to create a 2 dimensional array, i would be very grateful! If you need to know why i need one, read below: As you know, the default monopoly board has 40 spaces. Of the 40 spaces that may hold houses, there is a maximum of 4 houses. I need this 2D array to keep up with references to 4 regions on each of these properties. For example: HouseRegions[3,2] This would be the 3rd property, 2nd house placement space. The house placement spaces are not the houses themselves, but rather the region that the houses appear at on that particular property. Another example: HouseRegions[37,4] This would be the 37th property (parkplace), 4th house placement space. This would refer to the 4th region the 4th house on park place appears in. NOTE: keep in mind, i use the GUI to program my maps, i don't use the JASS script directly. I hope everyone understands! Thanks for reading! |
| 04-19-2003, 04:07 PM | #2 |
i think i remember Weadder saying that you do this Array [x*10+y] im pretty sure that makes every combo of y and x different :ggani: |
| 04-19-2003, 04:54 PM | #3 |
erm well sorry you misquoted me but whatever I'll do a full explanation now. Jass Arrays are 1d. Nothing we can really do about it. But if you know one dimension is fixed (I.e. Y dimension will never be greater then 5), then we can exploit this like you do with C arrays. I ofcourse do this in custom text which means I don't know how you'll do it out of it other then some multiplication. I create a function called Indexer like so. Let say I want to call coordinate 5,2 Code:
function indexer takes integer x,integer y returns integer return(x*5+y) end function Then in my code I'll call it like so My2dArray(indexer(5,2)) But what am I really doing? because you know that Y will never get past 5 you quantize the X coardinate to a multiple of 5 then add 2 to it. So You really are at the 27th entry in this Array, but its just for organazation sake. And it makes it much easier. N-D arrays are doable as well but I forget the formula. |
| 04-19-2003, 06:22 PM | #4 |
so, if i do this: First Property: Set Houses[1*40+1]=Region 1 Set Houses[1*40+2]=Region 2 Set Houses[1*40+3]=Region 3 Set Houses[1*40+4]=Region 4 Third Property: Set Houses[3*40+1]=Region 5 Set Houses[3*40+2]=Region 6 Set Houses[3*40+3]=Region 7 Set Houses[3*40+4]=Region 8 that will set the first 4 regions to Space 1, and the next 4 regions to space 3? Since there are 40 spaces on the board, the middle number will have to be 40 because i won't be going over 40. The way you explained it was a bit hard to grasp, so if you could give more examples and perhaps emphasize a bit, i would be grateful. Thanks! I think i'm starting to understand a bit. PS: Also, when i get up into [30*40+X] won't that take up a lot of RAM when the game is running? Because, that would equal index 120 for that array, and everything between 0 and 120 in the variable array are given junk values to fill in what i'm not using. |
| 04-19-2003, 06:43 PM | #5 |
War3 dynamically builds array as Bwood Explained. Arrays can go to anyplace between 0-99999 but can only have 1024 entries. So you can start at 9999 if you'd like. Also your a little off on your math... (see your property thingy)... Edit you'll probably want to start the first property at zero, just incase though... |
| 04-19-2003, 06:45 PM | #6 |
Since you are only working with a max of 4 regions per space, it is much more efficient to change it to: First Property: X = 0, Y = 0 through 3 Set Houses[X*4+Y]=Region 1 (y = 0) Set Houses[X*4+Y]=Region 2 (y = 1) Set Houses[X*4+Y]=Region 3 (y = 2) Set Houses[X*4+Y]=Region 4 (y = 3) Third Property: X = 2, Y = 0 - 3 Set Houses[X*4+Y]=Region 5 (y = 0) Set Houses[X*4+Y]=Region 6 (y = 1) Set Houses[X*4+Y]=Region 7 (y = 2) Set Houses[X*4+Y]=Region 8 (y = 3) Where the number 4 is the max number of regions per space, X represents the first property (up to 39, total of 40), and Y represents the region in the property (up to 3, total of 4). The resulting array from the above would look like this: Code:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] ...
R1 R2 R3 R4 R5 R6 R7 R8
{frist propetry} {second property} {third property} |
| 04-19-2003, 06:49 PM | #7 |
Here is one of the GUI triggers i created using the method as i see it. Tell me if i'm doing this correctly: Unit - Create 1 House for Neutral Passive at (Center of BuildingSpots[(destination[g] x (40 + (NumberOfBuildings[g] + 1)))]) Facing 90 Degrees variable array "destination" has the power of being an integer 1 - 40 at any given time; variable array "NumberOfBuildings", even with the +1 increment, has the power of being an integer 1 - 5 at any given time. With that said, is there any possibility of the index ever being the same? I want to make sure that this acts as a 2D array fully, i don't want it messing up on me. I need someone to explain this to me again, with more details! |
| 04-19-2003, 06:54 PM | #8 |
That is another issue guys... Most programmers start with 0, but i start with 1. My map, i've checked it, would not work correctly if i started it at 0 at this point. Would it still work if i started with 1? |
| 04-19-2003, 07:08 PM | #9 |
While it is preferable to start at 0, yes you could start at 1. In a lot of cases, getting the correct index is nothing more than adding in subtraction, or addition. As an example: Say you get the index of a player by ID, you would end up with 1 for player 1, 2 for player 2, etc... In order to get at array position 0 for a player array (to represent player 1), you would just take the player ID and subtract it by 1 to get 0. The opposite is also true. If your X is 0 for the first position of your houses array, you would simply add 1 to it for the other parts of your map that require a value starting at 1. |
| 04-19-2003, 07:18 PM | #10 | |
Guest | Quote:
While addition and substraction sound simple enough, off by one errors are the bane of many. I really screwed up this year's USACO in part due to them. |
| 04-19-2003, 07:22 PM | #11 |
So in the formula i posted above, changing the 40 to a 5, would the order of operations work as needed in the GUI form? Will it multiply Destination by 5 first, or will it add 5 to NumberOfBuildings first, then multiply Destination? |
| 04-19-2003, 09:30 PM | #12 |
well it still would work with [x position*10+ y position] think about it, 1*10+1=11 1*10+2=12 1*10+3=13 1*10+4=14 2*10+1=21 2*10+2=22 2*10+3=23 2*10+4=24 40*10+1=401 40*10+4=404 10*10+10=110 so if you use * 10 it will always work (im pretty sure at least) but still you need 160 array entries (4*40) |
| 04-19-2003, 10:56 PM | #13 |
thats slothful, and it won't won't work once someone tries to use something greater then 10. Entry 1,10=1*10+10=20 Entry 1,11=1*10+11=21 Entry 2,1=2*10+1=21 See the problem? |
| 04-19-2003, 11:03 PM | #14 |
oh now I see |
| 04-20-2003, 12:36 AM | #15 |
As I am a n00b programmer myself, I can't help but think that Blizzard would have been smart enough to include a way to create multi-dimensional arrays (also called a Matrix) in their programming, but I see the ways you have created to be sufficient. However, since I have seen people create new commands to be used in custom text, I am sure that someone would have the ingenious idea to create a way to make matrixes possible in the custom text of Warcraft 3. Though I have not had time due to AP tests coming up, and neumerous college visits, I'll take a look into this wonderous thing known as custom text, as I've noticed it reminds me much of the C++ language that I am currently learning. Oh, by the way, good luck with the Monopoly map DoomMaster! |
