Пример #1
0
        public static int PlayGame(Kingdom kingdom, BuyList a, BuyList b)
        {
            GameModel model = new GameModel();
            BuyListAIStrategy player1 = new BuyListAIStrategy(model);
            BuyListAIStrategy player2 = new BuyListAIStrategy(model);
            player1.BuyList = a;
            player2.BuyList = b;
            Player p1 = new Player("player1", player1, model);
            Player p2 = new Player("player2", player2, model);
            model.Players.Add(p1);
            model.Players.Add(p2);

            model.InitializeGameState(kingdom, 0);

            model.PlayGame(100);

            if(!model.GameOver || model.Result.Winners.Count == 2)
            {
                return 0;
            }
            else if (model.Result.ResultMap[p1].Won)
            {
                return -1;
            }
            else
            {
                return 1;
            }
        }
Пример #2
0
        public static void MonteCarloTreeSearchCompareAITestCheckAll()
        {
            string log = "";

            DateTime start = DateTime.Now;
            int overallWins = 0;
            int overallGames = 0;
            CardSetsModel cardSets = new CardSetsModel(GameSets.Any);
            foreach (CardSetGroup group in cardSets.CardSetGroups)
            {
                foreach (CardSetViewModel cardSet in group.CardSets)
                {
                    Kingdom kingdom = new Kingdom(cardSet.CardSet.CardCollection, null, cardSet.CardSet.BaneCard, GameSets.Any, 2, CardUseType.DoNotUse, CardUseType.DoNotUse, StartingHandType.FourThreeSplit);

                    Console.WriteLine("start train");
                    BuyListTrainer trainer = new BuyListTrainer(kingdom, true);
                    BuyListTrainer.GamesPerMatchup = 4;
                    BuyListTrainer.MaxChallengerCount = 15;
                    BuyListTrainer.MaxLeaderCount = 3;
                    List<BuyListEntry> best = trainer.Train(8);
                    BuyList buyList2 = best.Last().BuyList;
                    Console.WriteLine("end train");
                    int wins = 0;
                    int ties = 0;
                    int games = 0;
                    while(games < 2560)
                    {
                        GameModel gameModel = new GameModel();
                        BuyListAIStrategy player1 = new BuyListAIStrategy(gameModel);
                        player1.BuyList = buyList2.Clone();
                        player1.UseSearch = true;
                        player1.SearchThreshold = 0.05;
                        //player1.SearchThreshold = 1.0;
                        player1.SearchNodes = 320;
                        player1.SearchKeepsHandInfo = true;
                        BuyListAIStrategy player2 = new BuyListAIStrategy(gameModel);
                        player2.BuyList = buyList2.Clone();
                        Player p1 = new Player("player1", player1, gameModel);
                        Player p2 = new Player("player2", player2, gameModel);
                        gameModel.Players.Add(p1);
                        gameModel.Players.Add(p2);
                        gameModel.InitializeGameState(kingdom, games % 2);
                        using (gameModel.TextLog.SuppressLogging())
                        {
                            gameModel.PlayGame();
                        }
                        if (gameModel.Result.Winners.Count == 2)
                        {
                            ties++;
                            games++;
                        }
                        else if (gameModel.Result.Winners.Contains(p1) && !gameModel.Result.Winners.Contains(p2))
                        {
                            wins++;
                            games++;
                        }
                        else if (gameModel.Result.Winners.Contains(p2) && !gameModel.Result.Winners.Contains(p1))
                        {
                            games++;
                        }
                    }

                    DateTime end2 = DateTime.Now;
                    Console.WriteLine((end2 - start).TotalSeconds + " seconds");

                    double ratio = wins / (double)(games-ties);
                    Console.WriteLine(Log.FormatSortedCards(cardSet.CardSet.CardCollection));
                    Console.WriteLine(buyList2.ToString());
                    Console.WriteLine(wins + " / " + games);
                    Console.WriteLine((wins +ties) + " / " + games);
                    Console.WriteLine(ratio);
                    Console.WriteLine();

                    log += Log.FormatSortedCards(cardSet.CardSet.CardCollection);
                    log += buyList2.ToString();
                    log += wins + " / " + games;
                    log += ratio;
                    log += Environment.NewLine;

                    overallWins += wins;
                    overallGames += games;
                }
            }
            double overallRatio = overallWins / (double)overallGames;

            Console.WriteLine(overallWins + " / " + overallGames);
            Console.WriteLine(overallRatio);

            log += overallWins + " / " + overallGames;
            log += overallRatio;

            DateTime end = DateTime.Now;
            Console.WriteLine((end - start).TotalSeconds + " seconds");

            File.WriteAllText("output.txt", log);
        }
Пример #3
0
        public static void PerfTest()
        {
            Randomizer.SetRandomSeed(123456789);
            int games = 0;
            DateTime start = DateTime.Now;
            CardSetsModel cardSets = new CardSetsModel(GameSets.Any);
            foreach (CardSetGroup group in cardSets.CardSetGroups)
            {
                foreach(CardSetViewModel cardSet in group.CardSets)
                {
                    for (int k = 0; k < 800; k++)
                    {
                        games++;
                        GameModel model = new GameModel();
                        using (model.TextLog.SuppressLogging())
                        {
                            BuyList list = new BuyList();
                            list.DuchyBuyThreshold = 5;
                            list.EstateBuyThreshold = 3;
                            list.List.Add(new BuyListItem(typeof(Gold), 99));
                            foreach (CardModel card in cardSet.CardSet.CardCollection)
                            {
                                list.List.Add(new BuyListItem(card.GetType(), 1));
                            }
                            list.List.Add(new BuyListItem(typeof(Silver), 99));

                            BuyListAIStrategy player1 = new BuyListAIStrategy(model);
                            player1.BuyList = list;
                            BuyListAIStrategy player2 = new BuyListAIStrategy(model);
                            player2.BuyList = list;
                            Player p1 = new Player("player1", player1, model);
                            Player p2 = new Player("player2", player2, model);
                            model.Players.Add(p1);
                            model.Players.Add(p2);

                            Kingdom kingdom = new Kingdom(cardSet.CardSet.CardCollection, null, GameSets.Any, 2);
                            model.InitializeGameState(kingdom);
                            model.PlayGame(100);
                        }
                    }
                }
            }
            TimeSpan elapsed = DateTime.Now - start;
            Console.WriteLine("total time = " + elapsed.TotalSeconds);
            Console.WriteLine("Total games = " + games);
            Console.WriteLine("games / sec = " + (games / elapsed.TotalSeconds));
        }
Пример #4
0
        public static void MonteCarloTreeSearchCompareAITest()
        {
            DateTime start = DateTime.Now;
            /*
            CardModel[] cards = new CardModel[]
            {
                new Chapel(),
                new ShantyTown(),
                new Militia(),
                new Moneylender(),
                new City(),
                new Mint(),
                new Goons(),
                new Hoard(),
                new Nobles(),
                new Expand()
            };

            BuyList buyList = new BuyList();
            buyList.OpeningBuy1 = typeof(Chapel);
            buyList.OpeningBuy2 = typeof(Silver);
            buyList.List.Add(new BuyListItem(typeof(Goons), 2));
            buyList.List.Add(new BuyListItem(typeof(City), 5));
            buyList.List.Add(new BuyListItem(typeof(Goons), 1));
            buyList.List.Add(new BuyListItem(typeof(Nobles), 4));
            buyList.List.Add(new BuyListItem(typeof(Militia), 1));
            buyList.List.Add(new BuyListItem(typeof(Nobles), 2));
            buyList.List.Add(new BuyListItem(typeof(City), 4));
            buyList.List.Add(new BuyListItem(typeof(Province), 8));
            buyList.List.Add(new BuyListItem(typeof(Nobles), 2));
            buyList.List.Add(new BuyListItem(typeof(Goons), 3));
            buyList.List.Add(new BuyListItem(typeof(Hoard), 5));
            buyList.List.Add(new BuyListItem(typeof(Gold), 99));
            buyList.List.Add(new BuyListItem(typeof(Silver), 99));
            */
            //Kingdom kingdom = new Kingdom(cards, null, GameSets.Any, 2, CardUseType.DoNotUse, CardUseType.DoNotUse, StartingHandType.FourThreeSplit);

            /*
            IList<CardModel> cards = new List<CardModel>(new BigMoney().CardCollection);
            BuyList buyList = new BuyList();
            buyList.OpeningBuy1 = typeof(Chancellor);
            buyList.OpeningBuy2 = typeof(Silver);
            buyList.List.Add(new BuyListItem(typeof(Gold), 1));
            buyList.List.Add(new BuyListItem(typeof(Laboratory), 2));
            buyList.List.Add(new BuyListItem(typeof(Province), 99));
            buyList.List.Add(new BuyListItem(typeof(Gold), 99));
            buyList.List.Add(new BuyListItem(typeof(Moneylender), 1));
            buyList.List.Add(new BuyListItem(typeof(Laboratory), 8));
            buyList.List.Add(new BuyListItem(typeof(Silver), 99));
            */

            IList<CardModel> cards = new List<CardModel>(new PoolsToolsAndFools().CardCollection);
            BuyList buyList = new BuyList();
            buyList.OpeningBuy1 = typeof(Silver);
            buyList.OpeningBuy2 = typeof(Silver);
            buyList.List.Add(new BuyListItem(typeof(ScryingPool), 2));
            buyList.List.Add(new BuyListItem(typeof(Golem), 8));
            buyList.List.Add(new BuyListItem(typeof(Nobles), 1));
            buyList.List.Add(new BuyListItem(typeof(Province), 99));
            buyList.List.Add(new BuyListItem(typeof(Gold), 3));
            buyList.List.Add(new BuyListItem(typeof(Nobles), 8));
            buyList.List.Add(new BuyListItem(typeof(Gold), 99));
            buyList.List.Add(new BuyListItem(typeof(ScryingPool), 2));
            buyList.List.Add(new BuyListItem(typeof(Silver), 99));
            buyList.List.Add(new BuyListItem(typeof(Potion), 1));
            buyList.List.Add(new BuyListItem(typeof(ScryingPool), 6));

            Kingdom kingdom = new Kingdom(cards, null, null, GameSets.Any, 2, CardUseType.DoNotUse, CardUseType.DoNotUse, StartingHandType.FourThreeSplit);

            /*
            BuyListTrainer trainer = new BuyListTrainer(kingdom, true);
            BuyListTrainer.GamesPerMatchup = 5;
            BuyListTrainer.MaxChallengerCount = 60;
            BuyListTrainer.MaxLeaderCount = 5;
            List<BuyListEntry> best = trainer.Train(20);
            Console.WriteLine("Trained");
            BuyList buyList2 = best.Last().BuyList;
            Console.WriteLine(buyList2.ToString());
            return;
            */
            int wins = 0;
            int games = 0;
            for (int p = 0; p < 256; p++)
            {
                GameModel gameModel = new GameModel();
                BuyListAIStrategy player1 = new BuyListAIStrategy(gameModel);
                player1.BuyList = buyList.Clone();
                player1.UseSearch = true;
                player1.SearchThreshold = 0.05;
                player1.SearchNodes = 320;
                player1.SearchConfirm = true;
                player1.SearchKeepsHandInfo = false;
                BuyListAIStrategy player2 = new BuyListAIStrategy(gameModel);
                player2.BuyList = buyList.Clone();
                Player p1 = new Player("player1", player1, gameModel);
                Player p2 = new Player("player2", player2, gameModel);
                gameModel.Players.Add(p1);
                gameModel.Players.Add(p2);
                gameModel.InitializeGameState(kingdom, p % 2);
                gameModel.PlayGame(200);
                //Console.WriteLine(gameModel.TextLog.Text);
                if (gameModel.Result.Winners.Contains(p1) && !gameModel.Result.Winners.Contains(p2))
                {
                    Console.WriteLine("won");
                    wins++;
                    games++;
                }
                else if(gameModel.Result.Winners.Contains(p2) && !gameModel.Result.Winners.Contains(p1))
                {
                    Console.WriteLine("lost");
                    games++;
                }

            }
            double ratio = wins / (double)games;
            Console.WriteLine(ratio);
            DateTime end = DateTime.Now;
            Console.WriteLine((end - start).TotalSeconds + " seconds");
        }
Пример #5
0
        public static void LogCheck()
        {
            int max = 1000;
            for (int i = 0; i < max; i++)
            {
                GameModel model = new GameModel();

                RandomAIStrategy player1 = new RandomAIStrategy(model);
                BaseAIStrategy player2 = new OpeningBookRandomizedAIStrategy(model);
                GameViewModel viewModel = new GameViewModel(model);
                Player p1 = new Player("player1", player1, model);
                Player p2 = new Player("player2", player2, model);
                model.Players.Add(p1);
                model.Players.Add(p2);

                Kingdom kingdom = new Kingdom(new RandomAllCardSet(GameSets.Any).CardCollection, null, GameSets.Any, 2);
                model.InitializeGameState(kingdom);
                try
                {
                    model.PlayGame(250);
                    if (model.GameOver)
                    {
                        GameRecord record = model.Result.ToGameRecord(p1);
                        var s = new System.Xml.Serialization.XmlSerializer(typeof(GameRecord));
                        StringWriter w = new StringWriter();
                        s.Serialize(w, record);
                        GameRecord g2 = (GameRecord)s.Deserialize(new StringReader(w.ToString()));
                        if (record.Name != g2.Name)
                        {
                            Console.WriteLine("Error");
                        }
                        if (record.Won != g2.Won)
                        {
                            Console.WriteLine("Error");
                        }

                        if (record.Players.Count != g2.Players.Count)
                        {
                            Console.WriteLine("Error");
                        }
                        else
                        {
                            for (int j = 0; j < record.Players.Count; j++)
                            {
                                if (record.Players[j].Name != g2.Players[j].Name)
                                {
                                    Console.WriteLine("Error");
                                }
                                if (record.Players[j].Score != g2.Players[j].Score)
                                {
                                    Console.WriteLine("Error");
                                }
                                if (record.Players[j].Deck != g2.Players[j].Deck)
                                {
                                    Console.WriteLine("Error");
                                }
                            }
                        }
                        if (record.Log.Count != g2.Log.Count)
                        {
                            Console.WriteLine("Error");
                        }
                        else
                        {
                            for (int j = 0; j < record.Log.Count; j++)
                            {
                                if (record.Log[j].Replace(Environment.NewLine, "\n") != g2.Log[j])
                                {
                                    Console.WriteLine("Error");
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("error!");
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                    Debugger.Launch();
                }
                if (i % (max / 100) == 0)
                {
                    Console.WriteLine(i / (double)max);
                }
            }
        }
Пример #6
0
        public static void TrainerCompareAITest()
        {
            DateTime start = DateTime.Now;

            Kingdom kingdom = new Kingdom(new BigMoney().CardCollection, null, null, GameSets.Any, 2, CardUseType.DoNotUse, CardUseType.DoNotUse, StartingHandType.FourThreeSplit);
            /*
            BuyListTrainer trainer = new BuyListTrainer(kingdom, true);
            BuyListTrainer.GamesPerMatchup = 5;
            BuyListTrainer.MaxChallengerCount = 60;
            BuyListTrainer.MaxLeaderCount = 5;
            List<BuyListEntry> best = trainer.Train(20);
            Console.WriteLine("Trained");
            BuyList buyList1 = best.Last().BuyList;
            Console.WriteLine(buyList1.ToString(true));

            List<BuyListEntry> best2 = trainer.TrainThresholds();
            BuyList buyList2 = best2.Last().BuyList;
            Console.WriteLine(buyList2.ToString(true));
            */

            BuyList buyList1 = new BuyList();
            buyList1.OpeningBuy1 = typeof(Silver);
            buyList1.OpeningBuy2 = typeof(Moneylender);
            buyList1.List.Add(new BuyListItem(typeof(Adventurer), 1));
            buyList1.List.Add(new BuyListItem(typeof(Gold), 2));
            buyList1.List.Add(new BuyListItem(typeof(Province), 99));
            buyList1.List.Add(new BuyListItem(typeof(Adventurer), 2));
            buyList1.List.Add(new BuyListItem(typeof(Gold), 99));
            buyList1.List.Add(new BuyListItem(typeof(Laboratory), 6));
            buyList1.List.Add(new BuyListItem(typeof(Moneylender), 1));
            buyList1.List.Add(new BuyListItem(typeof(Silver), 99));

            BuyList buyList2 = buyList1.Clone();
            buyList2.EstateBuyThreshold = 2;
            buyList2.DuchyBuyThreshold = 5;
            buyList2.ProvinceBuyThreshold = 7;
            buyList2.ColonyBuyThreshold = 0;

            int wins = 0;
            int games = 0;
            for (int p = 0; p < 25600; p++)
            {
                GameModel gameModel = new GameModel();
                BuyListAIStrategy player1 = new BuyListAIStrategy(gameModel);
                player1.BuyList = buyList1.Clone();

                BuyListAIStrategy player2 = new BuyListAIStrategy(gameModel);
                player2.BuyList = buyList2.Clone();

                Player p1 = new Player("player1", player1, gameModel);
                Player p2 = new Player("player2", player2, gameModel);
                gameModel.Players.Add(p1);
                gameModel.Players.Add(p2);
                gameModel.InitializeGameState(kingdom, p % 2);
                gameModel.PlayGame(200);
                //Console.WriteLine(gameModel.TextLog.Text);
                if (gameModel.Result.Winners.Contains(p1) && !gameModel.Result.Winners.Contains(p2))
                {
                    wins++;
                    games++;
                }
                else if (gameModel.Result.Winners.Contains(p2) && !gameModel.Result.Winners.Contains(p1))
                {
                    games++;
                }

            }
            double ratio = wins / (double)games;
            Console.WriteLine(ratio);
        }
Пример #7
0
 public static void CrashCheckWorker()
 {
     int max = 1000000;
     for (int i = 0; i < max; i++)
     {
         GameModel model = new GameModel();
         using (model.TextLog.SuppressLogging())
         {
             RandomAIStrategy player1 = new RandomAIStrategy(model);
             //BaseAIStrategy player2 = new OpeningBookRandomizedAIStrategy(model);
             BuyListTrainingAIStrategy player2 = new BuyListTrainingAIStrategy(model, 4, 2, 4, 2, false);
             player2.UseSearch = true;
             player2.SearchNodes = 20;
             player2.SearchThreshold = 0.05;
             player2.SearchKeepsHandInfo = true;
             player2.SearchConfirm = true;
             GameViewModel viewModel = new GameViewModel(model);
             Player p1 = new Player("player1", player1, model);
             Player p2 = new Player("player2", player2, model);
             model.Players.Add(p1);
             model.Players.Add(p2);
             IEnumerable<CardModel> cards = new RandomAllCardSet(GameSets.Any).CardCollection;
             List<CardModel> cardList = cards.ToList();
             if (!cardList.Any(c => c is Prince))
             {
                 cardList.RemoveAt(0);
                 cardList.Add(new Prince());
             }
             Kingdom kingdom = new Kingdom(cardList, null, null, GameSets.Any, 2);
             model.InitializeGameState(kingdom);
             try
             {
                 model.PlayGame(100);
             }
             catch (Exception e)
             {
                 Console.WriteLine("error!");
                 Console.WriteLine(e.Message);
                 Console.WriteLine(e.StackTrace);
                 Debugger.Launch();
                 Console.WriteLine("error!");
                 Console.WriteLine(e.Message);
                 Console.WriteLine(e.StackTrace);
             }
             if (i % (max / 100) == 0)
             {
                 Console.WriteLine(i / (double)max);
             }
         }
     }
 }
Пример #8
0
        public void Play()
        {
            GameModel model = new GameModel();
            foreach(Connection connection in this.connections)
            {
                model.Players.Add(new Player(connection.User.Name, new ServerPlayerStrategy(connection.User.Name, model, connection), model));
            }

            Kingdom kingdom = this.gameSpecification.ToKingdom();
            model.InitializeGameState(kingdom);

            SupplyPileInfo pileInfo = new SupplyPileInfo();
            pileInfo.Piles.AddRange(model.SupplyPiles.Select(p => p.Card.ID));

            NetworkMessage message = new NetworkMessage();
            message.MessageCategory = GameMessages.GamePrefix;
            message.MessageType = GameMessages.SupplyPileInfo;
            message.MessageContent = NetworkSerializer.Serialize(pileInfo);
            foreach (Connection connection in this.connections)
            {
                connection.SendMessage(message);
            }

            if (model.ExtraPiles.Count > 0)
            {
                ExtraPileInfo extraPileInfo = new ExtraPileInfo();
                pileInfo.Piles.AddRange(model.ExtraPiles.Select(p => p.Card.ID));

                NetworkMessage message2 = new NetworkMessage();
                message2.MessageCategory = GameMessages.GamePrefix;
                message2.MessageType = GameMessages.ExtraPileInfo;
                message2.MessageContent = NetworkSerializer.Serialize(extraPileInfo);
                foreach (Connection connection in this.connections)
                {
                    connection.SendMessage(message2);
                }
            }

            NetworkMessage playerInfoMessage = new NetworkMessage();
            playerInfoMessage.MessageCategory = GameMessages.GamePrefix;
            playerInfoMessage.MessageType = GameMessages.PlayerInfo;
            PlayerInfo playerInfo = new PlayerInfo();
            playerInfo.Players.AddRange(model.Players.Select(p => p.Name));
            playerInfoMessage.MessageContent = NetworkSerializer.Serialize(playerInfo);
            foreach (Connection connection in this.connections)
            {
                connection.SendMessage(playerInfoMessage);
            }

            ClientStateSynchronizer synchronizer = new ClientStateSynchronizer(connections, model);
            model.TurnEnded += model_TurnEnded;
            try
            {
                model.PlayGame();
            }
            catch (System.Net.Sockets.SocketException)
            {
            }
        }