需要帮助迭代带有数组的arrayList

时间:2014-07-12 15:00:24

标签: java

我需要帮助迭代带有数组的List的索引。这行代码得分+ = SquareTable [index.get(i)]; 会产生错误,而且我不知道如何适应它。提前谢谢。

@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 :(得分:0)

您正尝试使用SquareTable实例(来自列表)而不是Piece来访问int数组。

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

 SquareTable[index.get(i)];

答案 1 :(得分:0)

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

并根据此代码

int[] SquareTable

SquareTable接受类型int

这就是给你TypeMismatchError

的东西

答案 2 :(得分: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;
}

答案 3 :(得分:0)

index.get(I)获取Piece对象,因此您无法使用它来访问数组中的元素。