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.