Thread: help coming up with loop/algorithm

1. help coming up with loop/algorithm

I have a 4X4 bool "box" array inside a larger "jar" array and want to write a drop loop so if the user presses a certain key, it will drop the piece (this is for a tetris game) until it hits something else in the jar. I am not sure how to come up with the for loop to detect the lowest block in the box array and then drop it until it hits something (if there is a block, the jar is true).

http://img412.imageshack.us/img412/964/droprt5.png

Can anyone suggest a solution for this?

Thank you.

2. There may not be such a thing as "the" lowest block in your box (there isn't in your picture). But you can start on the bottom row; if any are true, those are your lowest box(es). If not, go up a row, etc.

Then for each of the lowest box(es), find where they are inside your jar, and go down row by row until you find another block. Whichever gives you the shortest distance to the bottom, that's how far you can move.

3. So, let me get this right:
You have a large 2D array of bool, that you want to see if it's intersecting with a smaller 2D array that is offset into the larger array?

Doesn't sound too tricky to me. Just a double loop to iterate through the smaller 2D array, and test the content of the smaller array vs. the corresponding position in the larger array.

--
Mats

4. Basically, I have the small 4 X 4 bool box and the larger jar 10 X 20, which is also bool. I want to move drop the bo until the block in the box collides with any piece from the jar.

I tried a couple of loops byt they turned out to be very very very convoluted and had many definitions. Does anyone see an easier method of doing this?

5. For any "square" that it is true in the smaller box, if the "square" at small_box + xy_offset_into_the_big_box is true, you got a collision, may-be need to move the smaller box up again by one step.

In addition, you may start from bounds checking, to see that the small box has not reached the bottom of the big box.

6. Code:
int finalHeight = 0;
for( int i = 0; i < 4; i++)
{
for(int j = 3; j > -1; j--)
{
if(box[j][i] != 0)
{
for(int k = firstRow + j; k < 20; k++)
{
if(jar[firstCol+i][k] != 0)
{
if(k > finalHeight)
{
finalHeight = k;
break;
}
}
}
}
}
}
I tried doing it that way, but it is way too complicated. I am not sure what you mean by xy offset. If it helps, as of now the top left corner of the box is located in position [0][3] of the jar, which is also a bool array.

7. You seem to have too many loops.

Loop through each cell of the small square. If a bit is true, see if the corresponding location* in the large square 1) is in range and 2) that bit is set.

*Hopefully you are storing the position of the upper left corner!?

8. I can do that, but I am not sure how to actually drop the piece until it hits something of the larger jar array.