c++ - 当检查单元格是否被占用时 CLI/C++ 俄罗斯方 block block 崩溃 MSVisualStudio 2008

标签 c++ visual-studio-2008 c++-cli tetris

这是我用于检查 future 移动是否合法的代码,我假设它是合法的并将移动复制到 mySquares 数组中。然后,我在表单中设置的游戏周期和定时器处理程序中调用此方法:

 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

在我的游戏板类中,我有检查 TCell 是否被占用的方法。 TGameBoar 持有一个 TCells 数组,它有一个颜色和 bool isOccupied = false;

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

它崩溃并表明问题出在这里,我目前正在学校学习 C++。我会很感激一些帮助。我也在努力使用 Keydown 来左右移动,使用 e->KeyData == Keys::Left) 等,并在通过循环时创建一个新 block 。 如果您想查看,我有我的项目 rar。我已经完成了所有类(class),只是将它们放在一起是困难的部分。

Project Tetris

最佳答案

我看到了三个问题。

  • 首先,您应该只在 isCellOccupied 返回 false 时才移动 mySquares(不是您当前拥有的 true)。我怀疑这是导致您崩溃的原因,因为看起来您要将一个 block 移动到一个已被占用的单元格中。
  • 其次,当 isCellOccupied 返回 true 时,您应该将 canGoDown 设置为 false 并跳出您的 for 循环(或者更好的是,使 canGoDown (==true) 成为您的 for 循环的附加条件,即 j < temporaryCopy->Length && canGoDown ).实际上,您的函数始终返回 true,因为它永远不会设置为 false,这不可能是对的。
  • 只是在这里做一个假设,但不是所有的 mySquares 都由 4 个元素组成吗?您正在用 4 个元素初始化 temporaryCopy,但不清楚 mySquares 是否有 4 个元素。如果不是,这可能很危险,因为在您的第一个循环中,您正在循环 mySquares->Length 并使用该索引值寻址 temporaryCopy,这可能超出范围。然后再做相反的事情。在所有循环中使用常量 (4) 可能更好,或者更好的是,始终使用 mySquares->Length(尤其是在创建临时复制数组时)以确保两个数组包含相同数量的元素。

关于c++ - 当检查单元格是否被占用时 CLI/C++ 俄罗斯方 block block 崩溃 MSVisualStudio 2008,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12109860/

相关文章:

vb.net - 无法在Visual Studio中进行调试

.net - 编码(marshal)委托(delegate)给 std::function

c# - 从 C++/CLI 函数调用 C# 函数

c# - 从 C++/CLI 访问 TryGetMember

c++ - 你能在不同的翻译单元中有两个同名和同一个成员函数的类吗?

c++ - std::integral_constant 背后的原因是什么?

css - 在 VS 2008 中取消注释 CSS 的快捷方式?

sql - 如何使用约束对话框向 SQL Server 2008 添加约束?

c++ - 为什么不能将 int 分配给 struct 成员的 union 成员?

c++ - 如何获取 promise::set_exception(x) 的参数?