Example #1
0
 private static IEnumerable<Node> GenerateNodes()
 {
     var player = new AIPlayerD();
      var rules = new GameRules();
      for (int i = 0; i < rules.BoardLength; i++)
     for (int j = 0; j < rules.BoardLength; j++)
     {
        Trace.WriteLine(string.Format("({0}, {1}))={2}", i, j, new Node(i, j, State.Void()).GetHashCode()));
        yield return new Node(i, j, State.Void());
     }
 }
Example #2
0
        static void Main(string[] args)
        {
            Console.SetWindowSize(40, 30);
             Trace.Listeners.Add(new ConsoleTraceListener());

             var rules = new GameRules(moveLimit: 3, boardLength: 6);
             var game =
            new GameBuilder(rules)
               .Build(
                  new Player[]
                  {
                     new AIPlayerP1{ Name = "PlayerP1" },
                     new AIPlayerP{ Name = "PlayerP" },
                     //new AIPlayerD{ Name = "PlayerD" },
                    // new AIPlayerD{ Name = "Player4" },
                  });

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

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

             int round = 0;
             var winner =
               game.Play()
              .Select(
                  player =>
                  {
                     Console.Clear();
                     Console.WriteLine(player + ": " + round++);
                     DrawBoard(game.Board);
                     Console.ReadLine();
                     return player;
                  })
              .Last();

             Console.WriteLine(winner + " Win!");
             Console.ReadKey();
        }
Example #3
0
 public GameBuilder(GameRules rules)
 {
     this.rules = rules;
 }
Example #4
0
        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();
        }