运行时检查失败#2 - 变量“位置”周围的堆栈已损坏

发布于 2024-11-04 20:12:44 字数 3316 浏览 4 评论 0原文

我在返回结构时遇到问题。它说运行时检查失败#2 - 变量“位置”周围的堆栈已损坏,我不知道为什么。我必须在今晚之前完成这项工作才能继续该项目的其余部分。如果你知道的话请帮忙。谢谢 :)

struct Position
{
    int SrcDstScore[50][5];
    char pieceColor [50][2];
};

int Sort(Position s2, Position s1)
{
    int x; // use compreto method
    return x;
}
Position EvaluateMoves(Piece * theBoard[8][8])
{   
    //store my result boards here 
    Position positions;
    for (int t=0; t<50; t++)
    {
        positions.SrcDstScore[t][1]= 0;
        positions.SrcDstScore[t][2]= 0;
        positions.SrcDstScore[t][3]= 0;
        positions.SrcDstScore[t][4]= 0;
        positions.SrcDstScore[t][5]= 0;
        positions.pieceColor[t][1]= ' ';
        positions.pieceColor[t][2]= ' ';
        //cout << "Eval";
    }
    char mcPlayerTurn = 'w';
    int counter = 0;
    // Fetching the board containing positions of these pieces
       for (int Row = 0; Row < 8; ++Row) 
        {
            for (int Col = 0; Col < 8; ++Col)   
            {
                if (theBoard[Row][Col] != 0)
                {
                    if (theBoard[Row][Col]->GetColor() == 'w')
                    {
                        Piece * piece =  theBoard[Row][Col];
                        for (int MoveableRow = 0; MoveableRow < 8; ++MoveableRow) // Now that we have found the knight find the legal squares. 
                        {
                            for (int MoveableCol = 0; MoveableCol < 8; ++MoveableCol)   
                            {
                                if(piece->IsLegalMove(Row, Col, MoveableRow, MoveableCol, theBoard))
                                {
                                    cout << counter << theBoard[Row][Col]->GetPiece() << " " << theBoard[Row][Col]->GetColor() <<" " <<  Row <<" " <<  Col <<" " <<  MoveableRow <<" " <<  MoveableCol << "\n";
                                    positions.SrcDstScore[counter][1]= Row;
                                    positions.SrcDstScore[counter][2]= Col;
                                    positions.SrcDstScore[counter][3]= MoveableRow;
                                    positions.SrcDstScore[counter][4]= MoveableCol; 

                                    //If the move is a capture add it's value to the score
                                    if (theBoard[MoveableRow][MoveableRow] != 0)
                                    {
                                        positions.SrcDstScore[counter][5] += theBoard[MoveableRow][MoveableRow]->GetValue();

                                        if (theBoard[Row][Row]->GetValue() < theBoard[MoveableRow][MoveableRow]->GetValue())
                                        {
                                            positions.SrcDstScore[counter][5] += theBoard[MoveableRow][MoveableRow]->GetValue() - theBoard[Row][Row]->GetValue();
                                        }
                                        //Add Score for Castling
                                    }
                                    counter ++;
                                }
                            }
                        }
                    }
                }
            }
       }

        return positions;
     }

Im having a problem returning a structure. It says Run-Time Check Failure #2 - Stack around the variable 'positions' was corrupted and i have no idea why. I have to finish this by tonight to proceed with the rest of the project. Do help if you know. thank you :)

struct Position
{
    int SrcDstScore[50][5];
    char pieceColor [50][2];
};

int Sort(Position s2, Position s1)
{
    int x; // use compreto method
    return x;
}
Position EvaluateMoves(Piece * theBoard[8][8])
{   
    //store my result boards here 
    Position positions;
    for (int t=0; t<50; t++)
    {
        positions.SrcDstScore[t][1]= 0;
        positions.SrcDstScore[t][2]= 0;
        positions.SrcDstScore[t][3]= 0;
        positions.SrcDstScore[t][4]= 0;
        positions.SrcDstScore[t][5]= 0;
        positions.pieceColor[t][1]= ' ';
        positions.pieceColor[t][2]= ' ';
        //cout << "Eval";
    }
    char mcPlayerTurn = 'w';
    int counter = 0;
    // Fetching the board containing positions of these pieces
       for (int Row = 0; Row < 8; ++Row) 
        {
            for (int Col = 0; Col < 8; ++Col)   
            {
                if (theBoard[Row][Col] != 0)
                {
                    if (theBoard[Row][Col]->GetColor() == 'w')
                    {
                        Piece * piece =  theBoard[Row][Col];
                        for (int MoveableRow = 0; MoveableRow < 8; ++MoveableRow) // Now that we have found the knight find the legal squares. 
                        {
                            for (int MoveableCol = 0; MoveableCol < 8; ++MoveableCol)   
                            {
                                if(piece->IsLegalMove(Row, Col, MoveableRow, MoveableCol, theBoard))
                                {
                                    cout << counter << theBoard[Row][Col]->GetPiece() << " " << theBoard[Row][Col]->GetColor() <<" " <<  Row <<" " <<  Col <<" " <<  MoveableRow <<" " <<  MoveableCol << "\n";
                                    positions.SrcDstScore[counter][1]= Row;
                                    positions.SrcDstScore[counter][2]= Col;
                                    positions.SrcDstScore[counter][3]= MoveableRow;
                                    positions.SrcDstScore[counter][4]= MoveableCol; 

                                    //If the move is a capture add it's value to the score
                                    if (theBoard[MoveableRow][MoveableRow] != 0)
                                    {
                                        positions.SrcDstScore[counter][5] += theBoard[MoveableRow][MoveableRow]->GetValue();

                                        if (theBoard[Row][Row]->GetValue() < theBoard[MoveableRow][MoveableRow]->GetValue())
                                        {
                                            positions.SrcDstScore[counter][5] += theBoard[MoveableRow][MoveableRow]->GetValue() - theBoard[Row][Row]->GetValue();
                                        }
                                        //Add Score for Castling
                                    }
                                    counter ++;
                                }
                            }
                        }
                    }
                }
            }
       }

        return positions;
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漆黑的白昼 2024-11-11 20:12:44

在初始循环中,您分别使用索引 1 到 5 和 1 到 2 为 SrcDstScorepieceColor 建立索引。这些应该是索引 0 到 4 和 0 到 1。

同样的情况也适用于这一行及其后面的三行:

positions.SrcDstScore[counter][1]= Row;

并且在您有 positions.SrcDstScore[counter][5],应该是 ...[4]

In the initial loop, you're indexing in to SrcDstScore and pieceColor using indexes 1 to 5 and 1 to 2 respectively. These should be indexes 0 to 4 and 0 to 1.

The same applies further down, on this line and the three after it:

positions.SrcDstScore[counter][1]= Row;

And in the two instances where you have positions.SrcDstScore[counter][5], that should be ...[4]

娇妻 2024-11-11 20:12:44
 positions.pieceColor[t][1]= ' ';
 positions.pieceColor[t][2]= ' ';

它应该是 -

 positions.pieceColor[t][0]= ' ';
 positions.pieceColor[t][1]= ' ';  // Array index starts from 0 to numElements - 1.

并且与 Position::SrcDstScore 的情况也相同。数组索引从0开始到4。

 positions.pieceColor[t][1]= ' ';
 positions.pieceColor[t][2]= ' ';

It should be -

 positions.pieceColor[t][0]= ' ';
 positions.pieceColor[t][1]= ' ';  // Array index starts from 0 to numElements - 1.

And is the same case with Position::SrcDstScore too. Array index start from 0 to 4.

蛮可爱 2024-11-11 20:12:44

如果变量 theBoard 是一个 2D 数组或指向 Piece 的 2D 数组的指针,其尺寸为 8 * 8(对于棋盘),则该函数声明应如下所示:

Position EvaluateMoves(Piece (*theBoard)[8])  // 1st way
Position EvaluateMoves(Piece theBoard[][8])  // 2nd(a) way
Position EvaluateMoves(Piece theBoard[8][8])  // 2nd(b) way
Position EvaluateMoves(Piece (&theBoard)[8][8])  // 3rd way, preferred if theBoard is an array

If the variable theBoard is a 2D array or pointer to 2D array of Piece with dimensions as 8 * 8 (for chess board) then the function declaration should look like this:

Position EvaluateMoves(Piece (*theBoard)[8])  // 1st way
Position EvaluateMoves(Piece theBoard[][8])  // 2nd(a) way
Position EvaluateMoves(Piece theBoard[8][8])  // 2nd(b) way
Position EvaluateMoves(Piece (&theBoard)[8][8])  // 3rd way, preferred if theBoard is an array
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文