Tetris game in C: Function for moving blocks down enters an infinite loop and doesn't make the blocks fall down correctly

162 Views Asked by At

I'm writing a Tetris game for my 1st year computer science project and I have a problem with the function which shifts blocks downwards. I represent my game state as a 2d array in a following way (example of a square Tetromino):

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 1 1 0 0 0 0 
0 0 0 0 2 1 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 

I would like the function to cause the following change to the game state:

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 1 1 0 0 0 0 
0 0 0 0 2 1 0 0 0 0 

However, rather than shifting the block down until it reaches the last row and then exiting the while loop, it doesn't exit the while loop at all.

Here is the function which is supposed to make the block shift down:

void block_falling(int gamestate[NO_BLOCKS_FOR_GAP][NUMBER_OF_BLOCKS_HORIZONTALLY])
{
    int temporary_storage;

    bool is_falling = true;

    while (is_falling)
    {
        for (int row_counter = 0; row_counter < NO_BLOCKS_FOR_GAP; row_counter++)
        {
            for (int column_counter = 0; column_counter < NUMBER_OF_BLOCKS_HORIZONTALLY; column_counter++)
            {
                if ((gamestate[row_counter][column_counter] == 1) || (gamestate[row_counter][column_counter] == 2))
                {
                    if (row_counter != NO_BLOCKS_FOR_GAP - 1)
                    {
                        temporary_storage = gamestate[row_counter][column_counter];
                        gamestate[row_counter][column_counter] = gamestate[row_counter - 1][column_counter];
                        gamestate[row_counter - 1][column_counter] = temporary_storage;
                    }
                    if (row_counter == NO_BLOCKS_FOR_GAP - 1)
                    {
                        is_falling = false;
                    }
                }
            }
        }
    }
}

I know that the part of code responsible for shifting the blocks down is correct, as when I copy the code below for example 10 times, the blocks shifts down 10 by ten blocks.

for (int row_counter = 0; row_counter < NO_BLOCKS_FOR_GAP; row_counter++)
{
    for (int column_counter = 0; column_counter < NUMBER_OF_BLOCKS_HORIZONTALLY; column_counter++)
    {
        if ((gamestate[row_counter][column_counter] == 1) || (gamestate[row_counter][column_counter] == 2))
        {
            temporary_storage = gamestate[row_counter][column_counter];
            gamestate[row_counter][column_counter] = gamestate[row_counter - 1][column_counter];
            gamestate[row_counter - 1][column_counter] = temporary_storage;
        }
    }
}

How should I fix the function so that it transfers the block downwards until it reaches the bottom row?

0

There are 0 best solutions below