Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLI/C++ Tetris block crashes when check if cell is occupied is empty MSVisualStudio 2008

Here is my code for checking if future move is legal, I have assumed its legal and copied move into mySquares array. I then call this method in the game cycle set in the form and in the timer handler which is:

 canvas->drawGrid();
 testBlock->drawBlock();
 testBlock->moveDown();//this method has checkBounds for when hit sides, top & bottom

if(newBlock->canMoveDown()==false)
{
    newBlock->addMySelfToGameBoard();

    mainGameBoard->updateGrid();

}

//timer1 handler finish


bool TTetrisBlock::canMoveDown()
{
    array<Point>^ temporaryCopy = gcnew array<Point>(4);

    bool canGoDown = true;
    for(int i=0;i<mySquares->Length;i++)
    {
        //Set future move
        temporaryCopy[i].X = mySquares[i].X;
        temporaryCopy[i].Y = mySquares[i].Y+1;
    }
    //Check if future move cells are full, if not assign values to mySquares
    //Check if future move is legal
        for(int j=0;j<temporaryCopy->Length;j++)
        {
            if(gameBoard->isCellOccupied(temporaryCopy[j].X,temporaryCopy[j].Y) == true)
            {

                mySquares[j].X = temporaryCopy[j].X;
                mySquares[j].Y = temporaryCopy[j].Y;
            }

        }
    return canGoDown;

}

//end of moveDown

in my gameboard class i have the method which checks if TCell is occupied or not. TGameBoar holds an array of TCells which has a color and bool isOccupied = false;

bool TGameBoard::isCellOccupied(int c,int r)
{
    //Checks if TCell is occupied
    return myGrid[c,r]->getIsOccupied();
}

It Crashes and indicates here was the problem, Im currently learning C++ at school. I would appreciate some help. I am also struggling with the Keydown for moving left and right using e->KeyData == Keys::Left) etc. and creating a newblock when gone through loop. I have my project rar if you want to check it out. I have all the classes done, its just putting it together is the hard bit.

Project Tetris

like image 578
Nolan Ratu Avatar asked Dec 03 '25 17:12

Nolan Ratu


1 Answers

I see three problems.

  • First you should only move mySquares when isCellOccupied returns false (not true as you currently have it). I suspect this is the cause of your crash as it looks like you will be moving a block into a cell that is already occupied.
  • Second, when isCellOccupied returns true you should set canGoDown to false and break out of your for loop (or better yet, make canGoDown (==true) an additional condition of your for loop i.e. j < temporaryCopy->Length && canGoDown). As it is, your function always return true because it is never set to false and that can't be right.
  • Just making an assumption here, but don't all mySquares consist of 4 elements? You are initializing temporaryCopy with 4 elements but it isn't clear whether mySquares has 4 elements. If not, this could be dangerous as in your first loop you are looping on mySquares->Length and addressing temporaryCopy with that index value, which could be out of range. And then later doing the opposite. It might be better to use a constant (4) in all all loops or better yet, always use mySquares->Length (especially when creating the temporaryCopy array) to ensure that both arrays contain the same number of elements.
like image 64
Jacob Holcomb Avatar answered Dec 06 '25 07:12

Jacob Holcomb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!