当前位置: 首页>>代码示例>>C#>>正文


C# Func.ToDictionary方法代码示例

本文整理汇总了C#中Func.ToDictionary方法的典型用法代码示例。如果您正苦于以下问题:C# Func.ToDictionary方法的具体用法?C# Func.ToDictionary怎么用?C# Func.ToDictionary使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Func的用法示例。


在下文中一共展示了Func.ToDictionary方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            var rules = new GameRules(moveLimit: 3, boardLength: 6);
             var timeout = TimeSpan.FromSeconds(60);
             var players =
            new Func<Player>[]
            {
             //  () => new AIPlayerR{ Name = "PlayerR#0", Timeout = timeout,  },
                 () => new AIPlayerP{ Name = "PlayerP#1", Timeout = timeout,  },
                 () => new AIPlayerP1{ Name = "PlayerP1#2", Timeout = timeout,  },
                 () => new AIPlayerP2{ Name = "PlayerP2#3", Timeout = timeout,  },
            //     () => new AIPlayerD{ Name ="PlayerD#2", Timeout = timeout,  },
            //   () => new AIPlayerD1{ Name = "PlayerD1#3", Timeout = timeout, },
            };

             var color = 8;
             var colors = players.ToDictionary(p => p().Name, _ => ++color);
             Action<string> write =
            p =>
            {
               var temp = Console.ForegroundColor;
               if (p != "None")
                  Console.ForegroundColor = (ConsoleColor)colors[p];

               Console.Write(p);
               Console.ForegroundColor = temp;
            };

             var stats = new Dictionary<string, int>();
             foreach (var p1 in players)
            foreach (var p2 in players)
            {
               var ps = new[] { p1(), p2() };
               if (ps[0].Name == ps[1].Name)
                  continue;

               var rounds = 10;
               write(ps[0].Name);
               Console.Write(" vs. ");
               write(ps[1].Name);
               Console.WriteLine();

               while (rounds-- > 0)
               {
                  Console.Write("round " + rounds + ": ");

                  var game =
                     new GameBuilder(rules)
                        .Build(ps);

                  var startPositions =
                     new[]
                     {
                        new {X = 0, Y = game.Board.Length - 1},
                        new {X = game.Board.Length - 1, Y = 0},
                     };

                  int i = 0;
                  foreach (var player in game.Board.Players)
                     using (game.Board.Flip(player))
                        game.Board.Occupy(startPositions[i].X, startPositions[i++].Y);

                  var stopwatch = new Stopwatch();
                  stopwatch.Start();
                  int moves = 0;
                  var winner =
                    game.Play()
                       .Select(
                           player =>
                           {
                              moves++;
                              return player;
                           })
                       .Last();
                  stopwatch.Stop();

                  write(winner.Name);
                  Console.WriteLine(" won, in " + moves + " moves[" + stopwatch.Elapsed + "]");

                  int numberOfWins;
                  if (!stats.TryGetValue(winner.Name, out numberOfWins))
                     stats[winner.Name] = 1;
                  else
                     stats[winner.Name] = numberOfWins + 1;
               }

               Console.WriteLine();
            }

             Console.WriteLine("Stat: ");
             var place = 0;
             foreach (var pair in stats.OrderByDescending(p => p.Value))
             {
            Console.Write(++place + ".");
            write(pair.Key);
            Console.WriteLine(": " + pair.Value + " wins");
             }
             Console.ReadKey();
        }
开发者ID:klumba12,项目名称:bacteria,代码行数:99,代码来源:Program.cs


注:本文中的Func.ToDictionary方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。