2011-05-02 169 views
0

Im有一個返回結構的問題。它說運行時檢查失敗#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; 
    } 

回答

2

在初始循環中,分別使用索引1到5和1到2索引到SrcDstScorepieceColor。這些應該是索引0至4和0比1

同樣適用於進一步下降,在這條線和三個後:

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

而且在你有positions.SrcDstScore[counter][5]兩個實例,應該是...[4]

+0

非常感謝你。它現在完美了! – GeeKaush 2011-05-02 09:03:18

0
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.

+0

@GeeKaush - 通常,當您遇到這種類型的運行時錯誤時,表明嘗試訪​​問未分配到的位置的變量。 – Mahesh 2011-05-02 08:58:40

+0

非常感謝你。它現在完美了! – GeeKaush 2011-05-02 09:02:45

+0

@GeeKaush - 很高興我能夠幫助你。 – Mahesh 2011-05-02 09:04:10

0

如果變量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 
相關問題