Skip to main content
GameDev.net gamedev.net
🔒 Locked

How do i move the elements of an array around another array?

Started by copper103 Nov 15, 2009 at 12:52 PM 1 replies 800+ views
Original Post
copper103
copper103
In allegro, i am trying to make a block ([3][3] array) move around a [10][10] array. I have tried a kind of thing that writes the contents of the block to the [10][10] array, the thing i cant do is the x and y position of the block in the array
nobodynews
nobodynews
First, ignore the concept of moving the blocks around. Instead focus on a simpler problem: given an x,y coordinate, determine where to put the [3][3] array into the [10][10] array. Then once we figure that out we can try to figure out how to move the blocks.

Let's say both arrays are indexed such that [0][0] is the top left of the grid if plotted on graph paper, [1][0] would be to the right of that spot, and [0][1] would be below it. That might not be what you have, but stay with me I'm just trying to make sure we're on the same page.

The ideal situation would be to come up with a generic equation to copy one array onto another, but again, let's simplify. We'll hard-code the logic in this situation and once we figure out the hard-coded logic we'll try to generalize the solution.

If you are told to plot the [3][3] array onto the [10][10] array at coordinates 3,4 you need, for each block, to copy the contents 3 units right and 4 units down in the bigger block. Literally, if you didn't know about loops and you might to it like this:
bigger_block[3+0][4+0] = smaller_block[0][0];bigger_block[3+1][4+0] = smaller_block[1][0];bigger_block[3+2][4+0] = smaller_block[2][0];bigger_block[3+0][4+1] = smaller_block[0][1];bigger_block[3+1][4+1] = smaller_block[1][1];bigger_block[3+2][4+1] = smaller_block[2][1];bigger_block[3+0][4+1] = smaller_block[0][2];bigger_block[3+1][4+1] = smaller_block[1][2];bigger_block[3+2][4+1] = smaller_block[2][2];


But wait! We just kept repeating 3 and 4 on the left side. So, we might do this:
int x = 3;int y = 4;bigger_block[x+0][y+0] = smaller_block[0][0];bigger_block[x+1][y+0] = smaller_block[1][0];bigger_block[x+2][y+0] = smaller_block[2][0];bigger_block[x+0][y+1] = smaller_block[0][1];bigger_block[x+1][y+1] = smaller_block[1][1];bigger_block[x+2][y+1] = smaller_block[2][1];bigger_block[x+0][y+1] = smaller_block[0][2];bigger_block[x+1][y+1] = smaller_block[1][2];bigger_block[x+2][y+1] = smaller_block[2][2];

Cool, that looks much simpler. In fact, we could stop here if you knew you'd always have a 3x3 block being copied into a 4x4 block. Then you could always copy the smaller block into the larger block if you knew the x,y coordinates of where you wanted to go. But wait! again. We see that both the bigger and smaller block's subscripts change in the same way. When we have things increase regularly like that we can use loops! We could write that this way:
:
int x = 3;int y = 4;for(int i = 0; i < 3; i++){  for(int j = 0; j < 3; j++)  {    bigger_block[x+i][y+j] = smaller_block[j];  }}
This is a little dangerous though. What if x was 9? Well, we'd get an error. So don't forget to make sure you are able to copy the block correctly first. Hope this helps.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
copper103
copper103
wow, this really cleared things up for me, thanks a lot :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.