help coming up with loop/algorithm

This is a discussion on help coming up with loop/algorithm within the C++ Programming forums, part of the General Programming Boards category; I have a 4X4 bool "box" array inside a larger "jar" array and want to write a drop loop so ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    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. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    And may-be post your code.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    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. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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!?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    I can do that, but I am not sure how to actually drop the piece until it hits something of the larger jar array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My C++ test is COMING!!...
    By [Z-D] in forum C++ Programming
    Replies: 52
    Last Post: 12-01-2006, 07:02 PM
  2. Aktif 6.0 coming eventually...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-15-2006, 08:36 AM
  3. Interview coming up
    By thomashdavies in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2004, 08:23 PM
  4. Job Interview Coming Up
    By adamg in forum C Programming
    Replies: 5
    Last Post: 05-02-2004, 11:25 PM
  5. what's the world coming to?
    By Waldo2k2 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-22-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21