Multidimensioned Arrays
Blitz3D Forums/Blitz3D Beginners Area/Multidimensioned Arrays
| ||
Hi all! I am reading Krylars book and at the part of Multidimensioal Arrays. Now I learnt single dimensioned arrays and two dimensial like Dim Playernames$(5) Dim playernames$(3,2) But at the end of this paragprah it says: "Your not limited to two dim on your arrays either, if you want to move onto 3 DIM u can do so by delcaring your array as follows: Dim Playenames$(5,2,3) This statement creates an array that is 5 elements DEEp, 2HIGH and 3 wide." Now thats totally screwed with my brain. I understand single arrays for example an array consists of 3 memorie blocks like John ---> billy ----> tim dim playernames$(3) playername$(1) = "John" playername$(2) = "billy" etc etc I understand 2d dim arrays example 2,3 dim playernames$(2,3) playername$(1,1)= "John" playername$(1,2)= "billy" playername$(1,3)= "tim" playername$(2,1)= "sarah" playername$(2,2)= "claire" etc etc John ---> billy ----> tim sarah ---> claire ----> lisa But how the hell does dim playername$(5,2,3) work out? He states it be like 5 elements deep 2 high 2 wide. What the hells deep? is this a hole we talking about now. we see it as name ----> name ----> name ----> etc namd name ----> name ----> name name ----> name ----> name and blitz sees it as one consitatly line name ---> on and on But how do I do three dim its totally confused me the way he describes it.. can someone explain plz Cheers Waz |
| ||
here is how you would do it dim numbers(1,1,1) numbers(0,0,0) = 1 numbers(0,0,1) = 2 numbers(0,1,0) = 3 etc... |
| ||
Does this help: think of a one-dimensional array as a line (with values arranged on that line), a two-dimensional array as a square (with values arrranged in a grid), and a three-dimensional array as a cube (with values arranged in a multilayer grid)? |
| ||
Think of it in terms of boxes.. One dimension you have a row of boxes beside eachother where you can read/write values to.. Two dimensions you have a whole floor with boxes, say if you create an array (3,2), you have three boxes in length, and two in width.. Three dimensions you have a floor with boxes, and then you stack boxes on top of them too.. if you have the previous array (3,2) and add another similar stack of boxes on top of that, you make your array with Dim(1,3,2).. that way you can access the first layer of boxes like this -> Boxes(0,0,1) = the location of one of the bottom layer boxes.. Bottom(1,0,1) = the location of one of the top layer boxes.. you can have as many layers of boxes as you wish :) |
| ||
so it be like numbers(0,0,0) etc numbers(0,0,1) numbers(0,1,0) numbers(1,0,0) numbers(0,1,1) numbers(1,1,1) etc? I am lost still dude sorry..... ANther thing is what is this like the 3 dim ones? is it like 3 rows and what ever columns? |
| ||
Thanks for posts but I am still quiet confused LOL. Couldent just 2 dim arrays be used if needed.. why are 3 dim needed? u could just do bigger 2 dim arrays right (10,20) lol |
| ||
OK Waz, for a total beginner who might not know what is necessary to learn and what is not, let me just tell you that it is highly unlikely that you'll find the need to use a 3-D array in your programming. So, just learn enough about using a 2-D array, and that should be sufficient. If you ever need to use a 3-D array in the future, you'll understand it much better once you've had some experience using 2-D arrays. To confuse you even further, you could go on to 4-D, etc., and I even have difficulty imagining these (using tesseracts?)! So, stick to 2-D - that's probably why Krylar did not elaborate further on 3-D arrays. |
| ||
Example of use of a 3D array - if you were to write a Rubic's cube program, or a 3D Tetris (with shapes that you can move forwards/backwards, sideways, and upwards/downwards). |
| ||
To clear this stuff up.. I am using Blitz Basic is 3D arrays used at all in 2D stuff? |
| ||
You can use 3D arrays to make multilayered tile-maps. (all 2d) |
| ||
I don't really see why this is a problem... There has even been a program I coded in a far past in which I used 5 dimensions in an array, and I'm not even ashamed to make 6 dimensions in an array if I need to... If I use them I really do not count in elements deep or high or width or whatever... But more in elements I need for the specific purpose... But this might sound quite confusing... In most cases you'll only use 1D or 2D arrays... Words as wide and high and deep as used as it's to place the graphical image of an array... You can see a 1D array as this +---+---+---+---+---+---+ | | | | | | | +---+---+---+---+---+---+ 1 2 3 4 5 6 All boxes in a row, each box contains a piece of data and the boxes are numbers in this case from 1 till 6, so the image shown here can been seen as Dim Variable(6) When you turn this into a 2D array you can see it as this 1 2 3 4 5 6 7 8 9 +---+---+---+---+---+---+---+---+---+ 1 | | | | | | | | | | +---+---+---+---+---+---+---+---+---+ 2 | | | | | | | | | | +---+---+---+---+---+---+---+---+---+ 3 | | | | | X | | | | | +---+---+---+---+---+---+---+---+---+ 4 | | | | | | | | | | +---+---+---+---+---+---+---+---+---+ All boxes contain a piece of data... And they are numbered... All contain data... I placed X in a box with is at 5,3 as you can see, so Variable(5,3) = "X" Do you get it... As for 3D I can not draw that in ASCII, but then the boxes are in cube style, and THAT is what the books you got mean with "deep".... I hope this can makes things a little more clear.... |
| ||
I understand 2D and 1D so I stick with them for now.. Anyone disagree? ANother thing to clear this up...... ur 1D would really be 7 elements as Blitz works 0 as a number correct? I am not trying to correct ur work but I am just trying to see if this is correct so I dont make a mistake. so this dim varible(6) is really 0,1,2,3,4,5,6 7 elements (boxes). Also am I correct saying the x is in 3,5 (rows/columns) as if i programmed that to 5,3 it wouldent work as its only 3 rows and 5 columns or am I wrong. hehe Thanks for help WAZ! P.S I understand arrays.. but I dont understand em to an extent of how the hell I use em to make a game? maybe I am still too noobish ehehehe |
| ||
I understand 2D and 1D so I stick with them for now.. Anyone disagree? Perhaps that's better, take on everything one by one ANother thing to clear this up...... ur 1D would really be 7 elements as Blitz works 0 as a number correct? I am not trying to correct ur work but I am just trying to see if this is correct so I dont make a mistake. so this dim varible(6) is really 0,1,2,3,4,5,6 7 elements (boxes). That is for Blitz correct... In pascal it's a little more complicated for example, but as long as you stick with Blitz, that is the way it works... Also am I correct saying the x is in 3,5 (rows/columns) as if i programmed that to 5,3 it wouldent work as its only 3 rows and 5 columns or am I wrong. hehe True mathematics require first the collumn than the row, but if you find it easier to code the other way around you are allowed to do that and then your statement is correct... Thanks for help You're welcome... :) P.S I understand arrays.. but I dont understand em to an extent of how the hell I use em to make a game? maybe I am still too noobish ehehehe You will learn how to get advantage from them... My games are swamped with them now, but when I first used them I was also uncomfortable with them... Noobie to BlitzBasic.... Any support thankful. |
| ||
that would be correct... you can use arrays in a game to store tons of stuff, they may be the single most important peice of game programing(exaduration but still...) here is a simple example in which you could use an array graphics 400,400,32,2 ;set the gfx mode dim map(10,10) ;create a map ;create a path map(3,3) = 1 map(3,4) = 1 map(4,4) = 1 map(5,4) = 1 setbuffer backbuffer() ; set the buffer while not keydown(1) ;main loop ;draw the map by looping through each value for x = 0 to 10 for y = 0 to 10 if map(x,y) = 0 then color 255,0,0 ;if the value in the array is a 0 then draw 'mountains' if map(x,y) = 1 then color 0,0,255 ;if the value in the array is 1 then draw a path rect x*10,y*10,10,10 ;draw the map next next flip ; flip the buffer wend all this does is use a 2D array to create a small map, hope this helps, im not a good teacher at all so if it starts to make you confused just skip ahead |
| ||
I remember when I first started with arrays, it was difficult to grasp.. Keep working on it and eventually it gets (much) easier. In case it might help here is how I was taught to think of 3 dimensional arrays. First - 2 dimensial arrays can be thought of as graph paper, the rows are one dimension and the columns the other dimension. Now think if you had more then one piece of graph paper - that is the third dimension. To see what is on the first paper, in the the third column, of the 2nd row it would be (1,3,2) to see the same thing on the 4th piece of paper it would be (4,3,2) |
| ||
I guess dynaman has a good way of putting this... |
| ||
Yeah, that's better than trying to imagine 3d space, and then 4d, 5d space... arrays don't really work like that; it's just a visualisation tool (and it would seem, a confusing one). For 4d arrays, you can imagine several books full of sheets of graph paper. And for 5d arrays you can imagine several bookshelves full of books full of sheets of graph paper. As you can see, arrays get huge pretty quickly once you start adding more dimensions. 6D might be several libraries full of bookshelves full of books full of sheets of graph paper and could be accessed like: number(library_no, bookshelf_no, book_no, page_no, x,y) But yes I think 2d arrays are probably enough for now. You'll probably find that once you want to use a 3d array, you'll already know why. |
| ||
I wouldn't worry about this too much -- I can't remember that last time I used a multi-dimensional array! |
| ||
The vast majority of the time I use arrays of types, but Waz is just startig out so we don't want to send him there just yet... |
| ||
I should give you one warning Waz... You are thinking way too complicated... I notice that in all threads you post... Programming can be easy, and once you really get the hang of it, and as long as you start thinking easily, and Blitz is the perfect language to keep things easy... Once you made some complete working games you can try if you can use the more complex way of coding... Programming is an ability you must build up step by step, and which requires patience to learn... Keep going on an easy level and slowly see if you can add something to your skills... Sometimes I see you bothering with things that took me years to find out the feature even existed... Don't try to do all at once... Keep thinking simple... |
| ||
sorry, i think its cause this book talks about it so I think "I MUST LEARN THAT" kinda thing. What would u lot suggest I learn so far i know Varibles Types (kinda..) Arrays If,then,else, endif and,or,not (kinda) input text (basics) |
| ||
Hard to say... But you do have material to create a simple game... You could try to make a simple game out of it... I'm planning to write a book about Blitz in the "for dummies" style... But I still have to start on it, so I don't know if it'll be usefull for you because it'll take a lot of time... But my suggestion is to try a very simple game to try out what you've learned... The more bigger projects come later... |