- Home /
creating and manipulating a grid efficiently
So, I created a grid using a list. No problem. However, when it comes to manipulating the grid the way I need it's become a bit of a hassle and i'm looking for opinions on the best way to handle this. Here are my requirements for my grid: 1. Layout a grid that is 20W X 30H, however the screen will only display 20W X 15H. 2. We will be spawning bricks, but I only want them to spawn off screen. I've figured out how to do that, but if I change the structure, it's important that I retain that ability. 3. The first brick will spawn anywhere in the offscreen section of the grid. After that, each brick will spawn so that it has at least one adjacent brick that it is touching. 4. When a brick spawns, I need to somehow remove that grid space from the list of available brick spaces so that it doesn't try to put 2 bricks in the same location.
3 and 4 are the ones I'm having trouble with. I started working with just a list and had bricks spawning off screen the way I wanted but they were simply scattered. The problem with the list is that when I spawn a brick, i simply remove that entry from the list. Well then I have nothing to check against to see if there are any adjacent bricks.
Someone suggested using a 2D array because it would be more efficient than a list and easier to check if bricks were touching. I had another person suggest using 2 lists and 2 2-dimensional arrays to cover all bases, but this seems a bit heavy handed to me. I also had a friend suggest using hash tables, which I have not used before, but after reading about them they also seem like a viable answer. I'm just wondering if anyone has any ideas or suggestions on the best way to keep this as lightweight as possible while still getting the work done. Thanks!
Answer by Lazdude17 · Apr 07, 2018 at 05:18 PM
If your size is not going to change i.e. 20Wx30H then absolutely use Arrays over Lists. Lists should be saved for things that need resizing down the road since you cannot resize an Array. Do you have multiple brick types? Do you need to be able to reference these?
2D Array of type(your bricks) and you wont place two in the same spot if you just check for it in your spawn script. For example in your spawn script you probably have a For Loop spawning them and if you don't I think you should. Then just check the array position and check if it is null, if so then spawn a brick. If not then it's already taken.