对接系统N vs N从列表

时间:2019-05-21 18:56:31

标签: c# list unity3d arraylist foreach

我正在尝试建立一个婚介系统... 它将如何工作?

已连接的用户将出现在列表中,随后他们将随机选择,例如:   3 vs 3 等,具体取决于玩家的偶数

我有这样的东西

带有昵称的列表:

List<string> list = new List<string>() { "Bob", "Michael", "Adrian", "Daniel", "Denzel", "Peter" }; //etc

稍后我想从列表中选择它们:

  foreach (var item in list.OrderBy(f => Guid.NewGuid()).Distinct().Take(list.Count() / 2 ))
    {
     Console.WriteLine(item);
    }

现在它将随机列出其中的3个,但是我如何列出下一个将输出类似内容的刻痕:

丹泽尔·迈克尔·彼得
vs
丹尼尔·鲍勃·阿德里安

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

    List<string> list = new List<string>() { "Bob", "Michael", "Adrian", "Daniel", "Denzel", "Peter" }; //etc
    var milist = list.OrderBy(f => Guid.NewGuid()).Distinct().ToList();
    string firstTeam = "";
    foreach (var item in milist.Take(list.Count() / 2))
    {
        firstTeam += item + ", ";
    }
    firstTeam = firstTeam.Substring(0, firstTeam.Length - 2);
    string secondTeam = "";
    foreach (var item in milist.Skip(list.Count() / 2))
    {
        secondTeam += item + ", ";
    }
    secondTeam = secondTeam.Substring(0, secondTeam.Length - 2);
    Console.WriteLine(firstTeam + " vs " + secondTeam);
    Console.ReadKey();

只需确保玩家人数是偶数即可。