-1

Im practicing C++, and been working on tetris clone. I have tried to figure out my own solutions with help of few tutorials to learn to code with this language. Im having almost everything else working, but this function is giving me some problems:

    bool Board::isBoxFree(int pX, int pY, int pPiece[][4], int pBoard[][20])
{
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
        
            if (pPiece[i][j] != 0)
            {
                if (pBoard[(pX / 15) + i][(pY / 15) + j] != 0)
                {
                    return false;
                    break;
                }

                if (pY / 15 + j > 18)
                {
                    return false;
                    break;
                }
            
                else
                    return true;
                break;
            }
        
        
            
        }
    }

This works with some of the pieces, but gives wrong values systematically with different tetris pieces. For example when checking if straight line can go right the last block overlaps borders. With n-shape or t-shape there is no problem. Sometimes when going down tetrominos overlap one block before function working and tetromino saving to board with the overlapping position. Some tetrominos don't seem to hit anything but are still collided and saved to the board one block away when they should have been collided.

Is this function written wrong or is there something wrong with my use of two dimensional arrays?

This is how I check if tetromino can go down inside game tick:

if (this->playBoard(xLoc, yLoc, this->arr, rotate, this->boardArr).isBoxFree(xLoc, yLoc + boxSize, this->arr, this->boardArr))

And here is how I check if tetromino can go left or right:

if (this->ev.key.code == Keyboard::Left)
        {

            if (this->playBoard().isBoxFree(xLoc - boxSize, yLoc , this->arr, this->boardArr)&& move ==false )//(move == false && xLoc>boxSize)
            {
                xLoc = xLoc - boxSize;
                move = true;
            
            }

        }
        else if (this->ev.key.code == Keyboard::Right)
        {
            if(this->playBoard().isBoxFree(xLoc+boxSize*3, yLoc , this->arr, this->boardArr)&& move == false) //(move == false && xLoc<boxSize*7)
            {
                xLoc = xLoc + boxSize;
                move = true;
            }
        }

As you can see I have multiplied boxSize with 3 when checking if going right is possible for it to work right with at least some tetrominos. That alone tells that there is something wrong with my code that I haven't figured out.

Help is much appreciated since this is one of the last problems I have with my tetris. Even the line clearing function works.

  • `return false; break;` -- The `break` doesn't do anything, since you've already returned. – PaulMcKenzie Dec 09 '21 at 19:19
  • 1
    *Is this function written wrong or is there something wrong with my use of two dimensional arrays?* -- You should have written a simple `main` or `test` function that gives your `isBoxFree` function a failure case. Then [debugging](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) the problem would be your next step. – PaulMcKenzie Dec 09 '21 at 19:22
  • Thanks for clarification and help with right practices. I look into it. I used memcpy function to save two dimensional arrays to board to avoid difficult syntax with using them. This might have backlashed and given me some problems that I haven't figured out yet. Or it might be something else completly... – Dreamstuff Dec 09 '21 at 21:00

1 Answers1

0

Re: Is this function written wrong

You have an error in your Board::isBoxFree() functin, in the inner for loop (I've removed redundant breaks, as Paul commented):

if (pPiece[i][j] != 0)
{
    if (pBoard[(pX / 15) + i][(pY / 15) + j] != 0)
        return false;
    if (pY / 15 + j > 18)
        return false;
    else
        return true;
}

You can remove else too, as if a condition was true - you would return already. So:

if (pPiece[i][j] != 0)
{
    if (pBoard[(pX / 15) + i][(pY / 15) + j] != 0)
        return false;
    if (pY / 15 + j > 18)
        return false;
    return true;
}

So for the first pPiece[i][j] that is NOT 0, if the board at that location is empty - you would return true, without checking other elements.

This is a common pattern. You check all elements of your pPiece and return false as soon as you find the collision. Then, when you are out of the loop (no collisions were found) - you return true

Re: is there something wrong with my use of two dimensional arrays?

Possibly. My eyes hurt a little looking at all those / 15, > 18... :)

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27