0

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!

1 Answers1

1

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--)
kay27
  • 897
  • 7
  • 16
  • It works! I understand the logic now! Thank you very much for your time! Thank you! –  Apr 18 '16 at 22:09
  • 1
    Also consider a [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) of rows rather than a 2D array. vector practically does all of the work for you. – user4581301 Apr 18 '16 at 22:15