运行时检查失败#2 - 变量“位置”周围的堆栈已损坏
我在返回结构时遇到问题。它说运行时检查失败#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在初始循环中,您分别使用索引 1 到 5 和 1 到 2 为
SrcDstScore
和pieceColor
建立索引。这些应该是索引 0 到 4 和 0 到 1。同样的情况也适用于这一行及其后面的三行:
并且在您有
positions.SrcDstScore[counter][5]
,应该是...[4]
In the initial loop, you're indexing in to
SrcDstScore
andpieceColor
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:
And in the two instances where you have
positions.SrcDstScore[counter][5]
, that should be...[4]
它应该是 -
并且与
Position::SrcDstScore
的情况也相同。数组索引从0开始到4。It should be -
And is the same case with
Position::SrcDstScore
too. Array index start from 0 to 4.如果变量
theBoard
是一个 2D 数组或指向Piece
的 2D 数组的指针,其尺寸为8 * 8
(对于棋盘),则该函数声明应如下所示:If the variable
theBoard
is a 2D array or pointer to 2D array ofPiece
with dimensions as8 * 8
(for chess board) then the function declaration should look like this: