我有以下内容:
public Move nextBestMove(Stack<Move> possibleMoves, Stack<Square> enemyPieces){
int highestWeight;
Move best = null;
for(Move move: possibleMoves){
Square landing = move.getLanding();
int landingX = move.getLandingXC();
int landingY = move.getLandingYC();
int score = scoring(enemyPiece.pieceName()){
if(score > highestWeight){
highestWeight = score;
best = move;
if(!possibleMoves.hasNext()){ //Check if i am at end of stack
return best;
else{
continue;
}
}
}
}
}
我正在尝试通过根据权重不断更新最佳举措来评估简单国际象棋策略实现中的最佳举措。但是我不确定如何检查我是否处于堆栈循环中的最后一项。
我知道我可以使用 .hasNext()
作为列表,但是如何通过 Stack 进行循环来完成类似的任务?
最佳答案
您不需要检查循环中的最后一项,因为循环无论如何都会结束。循环结束后,无条件返回:
for(Move move: possibleMoves){
Square landing = move.getLanding();
int landingX = move.getLandingXC();
int landingY = move.getLandingYC();
int score = scoring(enemyPiece.pieceName());
if(score > highestWeight){
highestWeight = score;
best = move;
}
}
return best;
关于Java:检查循环中的当前项目是否是正在循环的堆栈中的最后一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41067828/