2016-12-09 47 views
1

我有以下幾點:的Java:檢查是否在迴路電流項是堆棧中的最後一個項目繞環

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實現類似的循環?

+0

使用迭代器。 – shmosel

回答

1

您不需要檢查循環中的最後一項,因爲循環即將完成。一旦循環結束,返回無條件:

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

你需要檢查什麼,如果它的大小是1

if(possibleMoves.size() == 1) { //Checking the size of the stack currently 
    return best; 
} else { 
    continue; 
} 
相關問題