Program to move box in a jar - what is wrong?

This is a discussion on Program to move box in a jar - what is wrong? within the C++ Programming forums, part of the General Programming Boards category; I am writing this program to move a box in a jar(a larger array). First, I overlap the box into ...

Program to move box in a jar - what is wrong?

I am writing this program to move a box in a jar(a larger array). First, I overlap the box into the array, then I print the array and attempt to move it. But for some reason, the moving part of the code only works when the box is set entirely to true and not for the set up I have now. Can anyone see what I am doing wrong? - the code compiles, so this should help visualizing what the code is about.

y+1 is overwritten, so on the next iteration of the loop it has not the value you expect. move box starting from the right column - not left

and move down starting with the last row, not the first.

Maybe start with the simple example - try to write memmove that works for overlaping regeons to see when you start copy from the first char and when - from the last. You will need to use the same principle

I realize everything here is boolean, but let's look at integers. Suppose jar[y][x] = y, so jar[1][x] = 1, jar[4][x] = 4, and so on; and let's also suppose that firstRow = 1. What happens?

y goes from 1 through 4; in each of our columns we first set jar[2][x] to jar[1][x], so jar[2][x] is now 1. Then we set jar[3][x] to jar[2][x], so jar[3][x] is now 1 (remember that we just changed jar[2][x]!); then jar[4][x] to jar[3][x] = 1, and jar[5][x] to jar[4][x] = 1.

So instead of moving four rows down one each, we moved one row down four times. This is why, when we are moving data, we must loop against the direction of the move; otherwise, we will overwrite data that hasn't gotten moved yet.

Imagine you have an array with values {1, 2, 3, 4, 0}. Now suppose you wanted to move the first four values right. If you go copy values left to right then after the first iteration you'll have {1, 1, 3, 4, 0}, after second {1, 1, 1, 4, 0} and finally {1, 1, 1, 1, 1}. If you iterated from end to beginning then the iterations would result in:
{1, 2, 3, 4, 4}
{1, 2, 3, 3, 4}
{1, 2, 2, 3, 4}
{1, 1, 2, 3, 4}
and you'll have to do something about the first element.

this is the simplest possible example that demonstrates why:
Imagine you have a 1D array with 3 items;

Code:

int x[3] = {10, 20, 30};

Now, you want to move the first two items over by one place, dropping whatever value was previously in the last spot, and filling the first spot with zero. i.e. it should end up with this:
(0, 10, 20)
This seems so simple that we don't even need a loop, we can simply unroll the loop in our head and would come up with this:

Also, I added an new if thing for rotation but it doesn't seem to be working. I am kind of losing my mind over this right now. If anyone can shed some light on this statement as well, I would highly appreciate it.

Is this going to be a tetris-like game? I recently wrote one (code can be found in the game downloads) and I found it to be easier to manipulate the moving block as an array of coordinate-pairs in its own small bounding box and updating its relative position and checking collisions against the tetris field.