d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C# Problem With Array > Gravity Effect
Add Reply New Topic New Poll
Member
Posts: 2,303
Joined: Oct 11 2007
Gold: 0.00
Mar 21 2014 03:03pm
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
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 21 2014 04:56pm
there's no pattern between the first and second example...

what are you trying to do exactly?
Member
Posts: 2,303
Joined: Oct 11 2007
Gold: 0.00
Mar 24 2014 12:18pm
Quote (labatymo @ 21 Mar 2014 17:56)
there's no pattern between the first and second example...

what are you trying to do exactly?



All the -1 elements get on top on the array... It represents an empty zone (tile) in my game

Sorry I have mad a mistake in my example :

Code

1 2 1
-1 2 -1
1 -1 -1


should become

Code

-1 -1 -1
1 2 -1
1 2 1


Thanks

This post was edited by Eleven11 on Mar 24 2014 12:22pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 24 2014 05:25pm
So you basically just want to sort a column? Something like this (LOL BubbleSort):

Code
public void applyGravity(int[,] grid)
{
if(grid.GetLength(0) != grid.GetLength(1)) throw new ArgumentException("Must be a NxN matrix");

int len = grid.GetLength(0);

for (int column = 0; column < len; column++)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
if (grid[j, column] == -1)
{
int t = grid[i, column];
grid[i, column] = grid[j, column];
grid[j, column] = t;
}
}
}
}
}


This post was edited by Minkomonster on Mar 24 2014 05:25pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Mar 24 2014 05:27pm
Quote (Minkomonster @ Mar 24 2014 04:25pm)
So you basically just want to sort a column? Something like this (LOL BubbleSort):
I somehow don't think that is what he wants, although you are right: that is what is example seems to demand...

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 24 2014 05:33pm
Quote (Azrad @ Mar 24 2014 06:27pm)
I somehow don't think that is what he wants, although you are right: that is what is example seems to demand...


No, he doesn't want to sort. he just wants to shift each cell down until -1 are all on top.

ex:
1 2
2 1
-1 -1

would become:

-1 -1
1 2
2 1

just loop from the bottom. if it's -1, copy the value from above down and change the value above to -1. repeat for every row

eg:
Code
for (row = max; row > 0; row--){
if (matrix[col][row] == -1){
matrix[col][row] = matrix[col][row-1];
matrix[col][row-1] = -1;
}
}


can't think right now if it's col, row or vice versa. regardless, this should shift everything down once, or one step of your gravity. you can repeat #rows-1 times if you want to shift until everything's down.

This post was edited by carteblanche on Mar 24 2014 05:36pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 24 2014 05:33pm
Quote (Azrad @ Mar 24 2014 06:27pm)
I somehow don't think that is what he wants, although you are right: that is what is example seems to demand...


He said specifically he wanted all the -1's to be "on top." I can't think of any other way to interpret that /shrug


Perhaps sort was the wrong word? I maintanined the order of the array column. I just shifted all -1's to the top.

This post was edited by Minkomonster on Mar 24 2014 05:34pm
Member
Posts: 2,303
Joined: Oct 11 2007
Gold: 0.00
Mar 24 2014 06:29pm
Done it... I admit i must have been lazy thinking it

Code
for (int j = 0; j < BOARD_WIDTH; j++)
{
for (int i = BOARD_HEIGHT-1/; i > 0; i--)
{

if (grid[i, j] == -1)
{

int cpt = 1;

while (grid[i - cpt, j] == -1)
{
if (cpt < i)
{
cpt++;
}
else
{
break;
}
}
grid[i, j] = grid[i - cpt, j];
grid[i - cpt, j] = -1;


}
}
}


e/ Yet I dont want to base myself on the numbers themselves because I dont know if I will have an element of the -2 value in the future

This post was edited by Eleven11 on Mar 24 2014 06:31pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll