Exemple #1
0
        private void PlayGames(int playerIndex1, int playerIndex2)
        {
            var player1 = Players[playerIndex1];
            var player2 = Players[playerIndex2];

            Parallel.For(0, GameCount, i => {
                //for (int i = 0; i < GameCount; i++) {
                var cardDeck          = cardDeckGenerator?.GetCardDeck(i);
                var gameServer        = new GameServer(player1, player2, MaxGameTurns, cardDeck);
                GameResult gameResult = gameServer.Run();
                lock (lockObject) {
                    if (gameResult is WinningGameResult)
                    {
                        if (((WinningGameResult)gameResult).WinningPlayer.Player == player1)
                        {
                            _Wins[playerIndex1][playerIndex2]++;
                        }
                        else
                        {
                            _Wins[playerIndex2][playerIndex1]++;
                        }
                    }
                    else
                    {
                        // draw, no increments
                    }
                }
                Console.Out.Write(".");
                //}
            });
        }
Exemple #2
0
        public void Run()
        {
            int total = 0;
            int wins1 = 0;
            int wins2 = 0;
            int draws = 0;

            Parallel.For(0, GameCount, i => {
                //if (i > 0 && i % 10 == 0) {
                //    Console.Out.WriteLine($"Played {i} games. {Player1.Name} won {wins1} ({((double)wins1 / i):0.000}) {Player2.Name} won {wins2} ({((double)wins2 / i):0.000})");
                //}
                var cardDeck          = cardDeckGenerator?.GetCardDeck(i);
                var gameServer        = new GameServer(Player1, Player2, MaxGameTurns, cardDeck);
                GameResult gameResult = gameServer.Run();
                lock (locker) {
                    total++;
                    if (gameResult is WinningGameResult)
                    {
                        if (((WinningGameResult)gameResult).WinningPlayer.Player == Player1)
                        {
                            wins1++;
                        }
                        else
                        {
                            wins2++;
                        }
                    }
                    else
                    {
                        draws++;
                    }
                    if (WriteResults)
                    {
                        Console.Out.Write("\r");
                        Console.Out.Write($"Played {total} games. {Player1.Name} won {wins1} ({((double)wins1 / GameCount):0.000}). {Player2.Name} won {wins2} ({((double)wins2 / GameCount):0.000}). Draws {draws} ({((double)draws / GameCount):0.000}).");
                    }
                }
            });
            if (WriteResults)
            {
                Console.Out.WriteLine();
            }
            WinsPlayer1 = wins1;
            WinsPlayer2 = wins2;
            Draws       = draws;
        }