2017-03-14 10 views
0

私は自分のゲームでターン管理のためにリンクリストを使用しています。私はそれらを反復するプレイヤーを持っていますが、1人のプレイヤーがゲームを完了すると、スキップする必要があります。これは私が失敗した場所です。条件付きリンクリストの次の要素を取得

どうすればいいですか?ここに私が今持っているものがあります:

public Player GetNextPlayer() { 
     var current = linkedPlayerList.Find(currentPlayer); 

     Player nextPlayer = current.Next == null ? linkedPlayerList.First.Value : current.Next.Value; 

     SetCurrentPlayer(nextPlayer); 
     return nextPlayer; 
} 

私は以下を試しましたが、うまくいきません。

Player nextPlayer = current.Next.List.FirstOrDefault (x => !.RoundCompleted); 
+0

なぜ機能しないのか説明する必要があります。どのようなアウトプットを手に入れますか? –

+0

インデックスを持つ配列を使用して、それが誰であるかを示すのはなぜですか?なぜリンクリストを使用するのですか? – Amy

+0

コードを1つのステートメントにまとめるのは良い考えではなく、読みにくくなり、人のためのコードを書いて最適化を処理させます。 –

答えて

1

私はあなたの状態をチェックするためにループを使用します。

Player nextPlayer = current.Next == null 
    ? linkedPlayerList.First(f => !f.RoundCompleted) 
    : current.Next.List.Where(w=>!w.RoundCompleted).ElementAt(linkedPlayerList.ToList().IndexOf(currentPlayer)); 
+1

本当にうまく動作します。私はユニティではまだネット3.5を使用して以来、私はできませんnullの伝播をマイナス。ありがとう –

0

あなたはこのような何か(次回に余分なヌルチェックを追加)を行うことができます。説明するコードのコメント。

LinkedList<Player> linkedPlayerList = ......; 
Player currentPlayer = .......; 

public Player GetNextPlayer() 
{ 
    // Find the current node 
    var curNode = linkedPlayerList.Find(currentPlayer); 

    // Point to the next 
    LinkedListNode<Player> nextNode = curNode.Next; 

    // Check if at the end of the list 
    nextNode = nextNode == null ? linkedPlayerList.First : nextNode;  

    // Loop stops when we find the condition true or we reach the starting point 
    while (curNode != nextNode) 
    { 
     // Exit if found.... 
     if (!nextNode.Value.RoundCompleted) 
      break; 
     // Manage the next node to check for.... 
     nextNode = nextNode?.Next == null ? linkedPlayerList.First : nextNode.Next;  
    } 
    SetCurrentPlayer(nextNode.Value); 
    return nextNode.Value; 
} 
+0

残念ながら、これは動作していないようです。それは常に最初の選手を選ぶ。 –

関連する問題