Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取链表中具有条件的下一个元素_C#_Linked List - Fatal编程技术网

C# 获取链表中具有条件的下一个元素

C# 获取链表中具有条件的下一个元素,c#,linked-list,C#,Linked List,我在游戏中使用一个链表进行回合管理。我对一些玩家进行了反复的讨论,但是当一个玩家完成游戏时,他需要被跳过,这就是我失败的地方 我该怎么做?以下是我现在拥有的: public Player GetNextPlayer() { var current = linkedPlayerList.Find(currentPlayer); Player nextPlayer = current.Next == null ? linkedPlayerList.First.Val

我在游戏中使用一个链表进行回合管理。我对一些玩家进行了反复的讨论,但是当一个玩家完成游戏时,他需要被跳过,这就是我失败的地方

我该怎么做?以下是我现在拥有的:

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 == null
   ? linkedPlayerList.First(f => !f.RoundCompleted)
   : current.Next.List.Where(w=>!w.RoundCompleted).ElementAt(linkedPlayerList.ToList().IndexOf(currentPlayer));

您可以这样做(在next上添加一个额外的null检查):


我会用一个循环来检查你的情况。注释在代码中进行解释

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;
}
LinkedList linkedPlayerList=。。。。。。;
玩家当前玩家=。。。。。。。;
公共播放器GetNextPlayer()
{
//查找当前节点
var curNode=linkedPlayerList.Find(currentPlayer);
//指向下一个
LinkedListNode nextNode=curNode.Next;
//检查是否在列表的末尾
nextNode=nextNode==null?linkedPlayerList。第一个:nextNode;
//当我们发现条件为真或到达起点时,循环停止
while(curNode!=nextNode)
{
//如果找到,请退出。。。。
如果(!nextNode.Value.RoundCompleted)
打破
//管理要检查的下一个节点。。。。
nextNode=nextNode?.Next==null?linkedPlayerList.First:nextNode.Next;
}
SetCurrentPlayer(下一个节点值);
返回nextNode.Value;
}

您需要解释为什么它不起作用。你得到了什么输出?为什么不使用一个带有索引的数组来指示该轮到谁了?为什么要使用链表?将代码压入一条语句通常不是一个好主意,它会使阅读变得更加困难,而是为人们编写代码,让优化器处理优化。它不会返回下一个播放器。Unity在我设置断点时崩溃,因此无法调试。我更喜欢使用LinkedList来提高可读性,即使使用数组,我们也需要找到下一个未完成轮次的玩家。如果需要进行线性搜索,我不确定链表是否是正确的数据类型。不幸的是,这似乎不起作用。它总是选择第一个球员,效果非常好。减去零传播,我不能在Unity中这样做,因为它仍然使用Net 3.5。。非常感谢。
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;
}