2014-07-12 100 views
-4

請幫助迭代列表的索引與數組。這行代碼score + = SquareTable [index.get(i)];會產生一個錯誤,我不知道如何適應它。提前致謝。需要幫助迭代arrayList與數組

@Override 
public int evaluatePieceScore() { 

    int score = 0; 
    List<Piece> index = chessgame.getPieces(); 


    for (Piece piece : this.chessgame.getPieces()){ 
     if(piece.getColor() == Piece.YELLOW_COLOR){ 


      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_PAWN), PawnSquareTable); 
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_KNIGHT), KnightSquareTable); 
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_BISHOP), BishopSquareTable);    
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_ROOK), RookSquareTable); 
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_QUEEN), QueenSquareTable); 
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_KING), KingMiddleGameSquareTable); 
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_KING), KingEndGameSquareTable); 


     }else if(piece.getColor() == Piece.BROWN_COLOR){ 

      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_PAWN), reverse(PawnSquareTable)); 
      score += this.CalculateScore(index ,getPieceValue(Piece.TYPE_KNIGHT), reverse(KnightSquareTable)); 
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_BISHOP), reverse(BishopSquareTable));    
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_ROOK), reverse(RookSquareTable)); 
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_QUEEN), reverse(QueenSquareTable)); 
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_KING), reverse(KingMiddleGameSquareTable)); 
      score += this.CalculateScore(index, getPieceValue(Piece.TYPE_KING), reverse(KingEndGameSquareTable)); 


     }else{ 
      throw new IllegalStateException("Unknown piece color found: "+piece.getColor()); 
     } 
    }  
    return score; 
} 

public int CalculateScore(List<Piece> index, int pieceValue, int[] SquareTable){ 

    int score = pieceValue * index.size(); 

    for(int i = 0; i < index.size(); i++){ 
     score += SquareTable[index.get(i)]; 
    } 

    return score; 
} 
+4

需要錯誤堆棧跟蹤。 –

+0

請您詳細說明 – user3816650

+0

您收到的錯誤是什麼? –

回答

0

您嘗試訪問,而不是一個int使用Piece實例(從列表)的SquareTable陣列。

public int CalculateScore(List<Piece> index, int pieceValue, int[] SquareTable){ 

SquareTable[index.get(i)]; 
+0

是的,什麼試圖做 – user3816650

+0

那麼,這是行不通的。數組只能由'int'索引訪問。 –

0
index.get(i) gives you an object of type `Piece` 

,並根據此代碼

int[] SquareTable

SquareTable接受類型int

這就是給你的TypeMismatchError

0

你搞亂起來執行的集合,可以像

public int CalculateScore(List<Piece> index, int pieceValue, int[] SquareTable){  
    int score = pieceValue * index.size(); 

    for(int i = 0; i < SquareTable.size(); i++){ 
     score += getPieceValue(index.get(SquareTable[i])); 
    } 

    return score; 
} 
0

index.get(I)獲取你一塊對象,所以你不能用它來訪問數組中的元素。