Example #1
0
        public static void Run(int numGames)
        {
            Random random = new Random();

            int[] cumulativeTrickCounts            = new int[4]; // [playerId]
            IPlayGenerator <Play>[] playGenerators = new IPlayGenerator <Play> [4];
            //playGenerators[0] = new MultiThreadedMcts<Play>(100000, 50, random, 8);
            playGenerators[0]  = new Mcts <Play>(10000, 50, random);
            playGenerators[1]  = new RandomPlayGenerator <Play>();
            playGenerators[2]  = new HighestCardPlayGenerator();
            playGenerators[3]  = new HighestCardPlayGenerator();
            Logger.Log.Enabled = true;
            for (int gameNumber = 0; gameNumber < numGames; gameNumber++)
            {
                var gameState = TichuGameRunHarness.SetupFourPlayerGame(random);

                if (true)
                {
                    Logger.Log.WriteLine("Starting cards:");
                    for (int i = 0; i < 4; i++)
                    {
                        Logger.Log.WriteLine($"Player {i} {TichuCard.PrintCardsSortedBySuit(gameState.Players[i].Cards)}");
                    }
                }
                while (true)
                {
                    gameState.SetPointOfViewPlayer(gameState.CurrentPlayerTurn);
                    Play play = playGenerators[gameState.CurrentPlayerTurn].FindPlay(gameState);
                    Logger.Log.WriteLine($"Player {play.Player} played {play.Cards[0].ToString()}");
                    gameState.CommitPlay(play);
                    if (gameState.GameOver())
                    {
                        break;
                    }

                    if (gameState.PlayedCards.Count % 4 == 0)
                    {
                        Logger.Log.WriteLine("----");
                    }
                }

                var scores = gameState.Evaluate();
                for (int i = 0; i < 4; i++)
                {
                    cumulativeTrickCounts[i] += (int)scores[i];
                }

                Console.WriteLine($"Tricks: {scores[0]} {scores[1]} {scores[2]} {scores[3]} ");
            }

            string header = string.Format("{0, 6}{1, 10}{2, 10}", "Player", "Tricks", "Average");

            Console.WriteLine(header);

            for (int i = 0; i < 4; i++)
            {
                string output = string.Format("{0, 6}{1, 10}{2, 10:N1}", i, cumulativeTrickCounts[i], (double)cumulativeTrickCounts[i] / numGames);
                Console.WriteLine(output);
            }
        }
Example #2
0
        public static void Run(int iterations)
        {
            Random random = new Random();

            int[] wins  = new int[2];
            int   draws = 0;

            IPlayGenerator <TicTacToePlay>[] playGenerators = new IPlayGenerator <TicTacToePlay> [2];
            playGenerators[0] = new Mcts <TicTacToePlay>(10000, 20, random);
            playGenerators[1] = new Mcts <TicTacToePlay>(10000, 20, random);
            // playGenerators[1] = new RandomPlayGenerator<TicTacToePlay>();
            // playGenerators[0] = new ConsoleInputTicTacToePlayGenerator();
            // playGenerators[0] = new RandomPlayGenerator<TicTacToePlay>();
            // Logger.Log.Enabled = true;
            for (int iteration = 0; iteration < iterations; iteration++)
            {
                var state = CreateNewGame(random, 1);

                while (true)
                {
                    state.SetPointOfViewPlayer(state.CurrentPlayerTurn);
                    var play = playGenerators[state.CurrentPlayerTurn].FindPlay(state);
                    state.CommitPlay(play);
                    if (state.GameOver())
                    {
                        break;
                    }
                }

                var scores = state.Evaluate();
                if (scores[0] == scores[1])
                {
                    draws++;
                }
                else
                {
                    for (int i = 0; i < 2; i++)
                    {
                        wins[i] += scores[i] > scores[1 - i] ? 1 : 0;
                    }
                }
            }

            string header = string.Format("{0, 6}{1, 10}{2, 7}", "Player", "Wins", "%");

            Console.WriteLine(header);

            for (int i = 0; i < 2; i++)
            {
                string output = string.Format("{0, 6}{1, 10}{2, 7:P1}", i, wins[i], (double)wins[i] / (iterations - draws));
                Console.WriteLine(output);
            }
            Console.WriteLine($"Draws: {draws}");
        }
Example #3
0
        public static void Run(int iterations)
        {
            Random random = new Random();

            int[] wins  = new int[PlayerCount];
            int   draws = 0;

            IPlayGenerator <int>[] playGenerators = new IPlayGenerator <int> [PlayerCount];
            // playGenerators[0] = new ConsoleInputSixNimmtPlayGenerator();
            // playGenerators[1] = new RandomPlayGenerator<int>();
            // playGenerators[2] = new RandomPlayGenerator<int>();
            // playGenerators[3] = new RandomPlayGenerator<int>();
            // playGenerators[4] = new RandomPlayGenerator<int>();

            playGenerators[0]  = new Mcts <int>(numIterations: 2000, simulationDepth: 10, random);
            playGenerators[1]  = new RandomPlayGenerator <int>();
            playGenerators[2]  = new RandomPlayGenerator <int>();
            playGenerators[3]  = new RandomPlayGenerator <int>();
            playGenerators[4]  = new RandomPlayGenerator <int>();
            Logger.Log.Enabled = false;
            Stopwatch stopwatch = Stopwatch.StartNew();

            for (int iteration = 0; iteration < iterations; iteration++)
            {
                double[] scores = null;
                while (true)
                {
                    var state = CreateNewGame(random, scores, SixNimmtHarness.ProMode);

                    if (true)
                    {
                        Logger.Log.WriteLine("Starting cards:");
                        for (int i = 0; i < PlayerCount; i++)
                        {
                            var playerCards = state.PlayerCards[i].ToList();
                            playerCards.Sort();

                            Logger.Log.WriteLine($"Player {i} : {string.Join(" ", playerCards)}");
                        }
                    }

                    while (true)
                    {
                        if (state.CurrentPlayerTurn == 0 && state.PlayInputState == SixNimmtInputState.SelectCard)
                        {
                            Logger.Log.WriteLine(state.ToString());
                        }
                        state.SetPointOfViewPlayer(state.CurrentPlayerTurn);
                        var play = playGenerators[state.CurrentPlayerTurn].FindPlay(state);
                        // Console.WriteLine($"Player {state.CurrentPlayerTurn} plays {play}");
                        state.CommitPlay(play);
                        if (state.GameOver())
                        {
                            break;
                        }
                    }

                    scores = state.Evaluate();
                    if (scores.Any(x => x < 0))
                    {
                        // Somebody went below 0 - game over
                        double highestScore = double.MinValue;
                        foreach (var s in scores)
                        {
                            if (s > highestScore)
                            {
                                highestScore = s;
                            }
                        }

                        for (int i = 0; i < PlayerCount; i++)
                        {
                            if (scores[i] == highestScore)
                            {
                                wins[i]++;
                            }
                        }
                        break;
                    }
                }
            }

            string header = string.Format("{0, 6}{1, 10}{2, 7}", "Player", "Wins", "%");

            Console.WriteLine(header);

            for (int i = 0; i < PlayerCount; i++)
            {
                string output = string.Format("{0, 6}{1, 10}{2, 7:P1}", i, wins[i], (double)wins[i] / (iterations - draws));
                Console.WriteLine(output);
            }
            Console.WriteLine($"Draws: {draws}");
            stopwatch.Stop();
            Console.WriteLine($"Finished after {stopwatch.ElapsedMilliseconds:N0}ms.");
        }
Example #4
0
        public static void Run()
        {
            Random random = new Random();

            IPlayGenerator <int>[] playGenerators = new IPlayGenerator <int> [PlayerCount];
            playGenerators[0] = new Mcts <int>(10000, 20, random);
            playGenerators[1] = new ConsoleInputSixNimmtPlayGenerator("player2");
            playGenerators[2] = new ConsoleInputSixNimmtPlayGenerator("player3");
            // playGenerators[3] = new ConsoleInputSixNimmtPlayGenerator();
            // playGenerators[4] = new ConsoleInputSixNimmtPlayGenerator();
            Logger.Log.Enabled = true;
            while (true)
            {
                double[] scores = null;
                while (true)
                {
                    var state = CreateNewGame(random, scores, SixNimmtSuggestMoveHarness.ProMode);

                    if (true)
                    {
                        Logger.Log.WriteLine("Starting cards:");
                        var playerCards = state.PlayerCards[0].ToList();
                        playerCards.Sort();
                        Logger.Log.WriteLine($"Player {0} : {string.Join(" ", playerCards)}");
                    }

                    while (true)
                    {
                        if (state.CurrentPlayerTurn == 0 && state.PlayInputState == SixNimmtInputState.SelectCard)
                        {
                            Logger.Log.WriteLine(state.ToString());
                        }
                        state.SetPointOfViewPlayer(state.CurrentPlayerTurn);
                        var play = playGenerators[state.CurrentPlayerTurn].FindPlay(state);
                        state.CommitPlay(play);
                        if (state.GameOver())
                        {
                            break;
                        }
                    }

                    scores = state.Evaluate();
                    if (scores.Any(x => x < 0))
                    {
                        // Somebody went below 0 - game over
                        double highestScore = double.MinValue;
                        foreach (var s in scores)
                        {
                            if (s > highestScore)
                            {
                                highestScore = s;
                            }
                        }

                        int winningPlayer = 0;
                        for (int i = 0; i < PlayerCount; i++)
                        {
                            if (scores[i] == highestScore)
                            {
                                winningPlayer = i;
                            }
                        }

                        Console.WriteLine($"Game over. Winning player number: {winningPlayer}. Highest score is {highestScore}");
                        break;
                    }
                }
            }
        }