user5759898
user5759898

Reputation:

Tetris shifting 2D array

I'm currently writing a tetris in C++ and I am at the last stage, I need to delete a row when it is full. Once a piece falls it is stored in a boolean array grid[20][10]. For example I check which row is full (or true), if it is I call a method deleteRow, where n is a number of row:

void Grid::deleteRow(int n)
{
  for (j = 0; j < WIDTH; j++)
   {
     grid[n][j] = false;
   }
}

Once the row is deleted I call a method moveRowDown:

void Grid::moveRowDown()
{
  for (i = 0; i < HEIGHT; i++)
   {
    for (j = 0; j < WIDTH; j++)
     {
       grid[i+1][j]=grid[i][j];
     }
  }
}

So this method does not work, and all of the pieces disappear. I know I am missing the logic. Thanks for the help in advance!

Upvotes: 0

Views: 404

Answers (1)

kay27
kay27

Reputation: 909

They disappear because you copy 1st empty row to 2nd, then to 3rd and etc. You need to rewrite your first loop in Grid::moveRowDown() to work from the bottom of a glass:

for (i = HEIGHT-2; i>=0; i--)

Upvotes: 1

Related Questions