Hi, I'm currently designing some game and I've got myself to a problem. Let me explain
I'm trying to write down an algorithm which goes from ex1 to ex2 :
Ex1
Code
2 1
1 -1
-1 2
-1 1
Ex2
Code
-1 -1
-1 1
2 1
1 1
So basically, -1 represents an empty space in my game. What I want to happen is some sort of gravity.
Here is my code so far (Not working)
Code
public void applyGravity()
{
for (int i = 0; i < BOARD_WIDTH; i++)
{
for (int j = BOARD_HEIGHT - 1; j > 0; j--)
{
if (grid[i, j] == -1)
{
int cpt = 1;
while (grid[i, j - cpt] == -1)
{
if (cpt < j)
{
cpt++;
}
else
{
break;
}
}
//swap values
grid[i, j - cpt] = grid[i, j];
grid[i, j] = -1;
}
}
}
}
Can someone explain where I go wrong? Thanks