Пример #1
0
        private static void PlayGame(List <string> bots, bool parallel)
        {
            AILog.Log("PlayBots", "Creating game...");
            var templateID = 1;
            var gameID     = BotGameAPI.CreateGame(Enumerable.Range(10, bots.Count).Select(o => PlayerInvite.Create((PlayerIDType)o, PlayerInvite.NoTeam, null)), "PlayBots", templateID, gameSettings =>
            {
                gameSettings["MaxCardsHold"]      = 999;
                gameSettings["ReinforcementCard"] = "none";
                //gameSettings["Fog"] = GameFogLevel.NoFog.ToString();
                //gameSettings["Fog"] = GameFogLevel.ModerateFog.ToString();
                //gameSettings["Fog"] = GameFogLevel.ExtremeFog.ToString();
                //gameSettings["OneArmyStandsGuard"] = false;
                //ZeroAllBonuses(gameSettings);
                //gameSettings["Map"] = 16114; //Rise of Rome -- use to test how bots respond to super bonuses
                //gameSettings["Map"] = 24591; //big USA, 3066 territories
                //gameSettings["MultiAttack"] = true;
                //gameSettings["AllowPercentageAttacks"] = false;
                //gameSettings["AllowAttackOnly"] = false;
                //gameSettings["AllowTransferOnly"] = false;

                //var wastelands = new JObject();
                //wastelands["NumberOfWastelands"] = 0;
                //wastelands["WastelandSize"] = 10;
                //gameSettings["Wastelands"] = wastelands;
                //gameSettings["BombCard"] = new JObject(new JProperty("InitialPieces", 0), new JProperty("MinimumPiecesPerTurn", 1), new JProperty("NumPieces", 4), new JProperty("Weight", 1));
                //gameSettings["SanctionsCard"] = new JObject(new JProperty("InitialPieces", 0), new JProperty("MinimumPiecesPerTurn", 1), new JProperty("NumPieces", 4), new JProperty("Weight", 1), new JProperty("Duration", 1), new JProperty("Percentage", 0.5));
                //gameSettings["BlockadeCard"] = new JObject(new JProperty("InitialPieces", 50), new JProperty("MinimumPiecesPerTurn", 1), new JProperty("NumPieces", 1), new JProperty("Weight", 1), new JProperty("MultiplyAmount", 10));
                //gameSettings["DiplomacyCard"] = new JObject(new JProperty("InitialPieces", 0), new JProperty("MinimumPiecesPerTurn", 1), new JProperty("NumPieces", 1), new JProperty("Weight", 1), new JProperty("Duration", 1));
                //gameSettings["NumberOfCardsToReceiveEachTurn"] = 4;
            });

            AILog.Log("PlayBots", "Created game " + gameID);

            var settings = BotGameAPI.GetGameSettings(gameID);
            var game     = BotGameAPI.GetGameInfo(gameID, null);

            var botsDict = game.Players.Values.Zip(bots, (gp, bot) => new { Player = gp, Bot = bot }).ToDictionary(o => o.Player.ID, o => o.Bot);

            try
            {
                while (true)
                {
                    game = BotGameAPI.GetGameInfo(gameID, null);
                    if (game.State == GameState.Finished)
                    {
                        var winnerStr = game.Players.Values.Where(o => o.State == GamePlayerState.Won).Select(o => botsDict[o.ID]).JoinStrings(",");
                        _totals.AddOrUpdate(winnerStr, 1, (_, i) => i + 1);
                        Console.WriteLine("Game " + gameID + " finished.  Winner=" + winnerStr + ", totals: " + _totals.OrderByDescending(o => o.Value).Select(o => o.Key + "=" + o.Value).JoinStrings(", "));


                        break;
                    }

                    var players = game.Players.Values.Where(o => o.State == GamePlayerState.Playing).ToList();

                    Action <GamePlayer> play = player =>
                    {
                        var pg = BotGameAPI.GetGameInfo(gameID, player.ID);

                        EntryPoint.PlayGame(botsDict[player.ID], pg, player.ID, settings.Item1, settings.Item2, picks => BotGameAPI.SendPicks(pg.ID, player.ID, picks), orders => BotGameAPI.SendOrders(pg.ID, player.ID, orders, pg.NumberOfTurns + 1));
                    };

                    if (parallel) //note: Parallel won't work when teammates, cards, and limited holding cards are involved.
                    {
                        players.AsParallel().ForAll(play);
                    }
                    else
                    {
                        players.ForEach(play);
                    }

                    Thread.Sleep(100);
                }
            }
            finally
            {
                GameFinished(gameID);
            }
        }
Пример #2
0
        static void PlayGame(string botName, string opponent, int threadNum, int gameNum)
        {
            var usID  = (PlayerIDType)10;
            var oppID = (PlayerIDType)11;

            var invite = new List <PlayerInvite>();

            invite.Add(PlayerInvite.Create(usID, PlayerInvite.NoTeam, null));

            if (opponent == AIBotIdentifier)
            {
                invite.Add(PlayerInvite.Create("*****@*****.**", PlayerInvite.NoTeam, null));
            }
            else
            {
                invite.Add(PlayerInvite.Create(oppID, PlayerInvite.NoTeam, null));
            }

            AILog.Log("Compete", "Creating game...");
            var gameID = BotGameAPI.CreateGame(invite, "Compete", null, gameSettings =>
            {
                gameSettings["MaxCardsHold"]      = 999;
                gameSettings["ReinforcementCard"] = "none";
            });

            AILog.Log("Compete", "Created game " + gameID);

            var  settings = BotGameAPI.GetGameSettings(gameID);
            var  game     = BotGameAPI.GetGameInfo(gameID, null);
            bool?won      = null;

            try
            {
                while (true)
                {
                    game = BotGameAPI.GetGameInfo(gameID, null);

                    if (game.State == GameState.Finished)
                    {
                        won = game.Players.Values.Single(o => o.State == GamePlayerState.Won).ID == usID;
                        _botStats.GetOrAdd(opponent, _ => new Stats()).Record(won.Value);
                        Console.WriteLine("T" + threadNum.ToString("00") + " G" + gameNum.ToString("00") + ": " + (won.Value ? "Won " : "Lost") + " game vs " + opponent + " " + gameID + " finished. Totals: " + _botStats.OrderBy(o => o.Key).Select(o => o.Key + "=" + o.Value).JoinStrings(", "));
                        break;
                    }

                    //Play ourselves
                    var pg = BotGameAPI.GetGameInfo(gameID, usID);
                    EntryPoint.PlayGame(botName, pg, usID, settings.Item1, settings.Item2, picks => BotGameAPI.SendPicks(pg.ID, usID, picks), orders => BotGameAPI.SendOrders(pg.ID, usID, orders, pg.NumberOfTurns + 1));


                    //Play opponent
                    if (opponent != AIBotIdentifier)
                    {
                        pg = BotGameAPI.GetGameInfo(gameID, oppID);
                        EntryPoint.PlayGame(opponent, pg, oppID, settings.Item1, settings.Item2, picks => BotGameAPI.SendPicks(pg.ID, oppID, picks), orders => BotGameAPI.SendOrders(pg.ID, oppID, orders, pg.NumberOfTurns + 1));
                    }

                    Thread.Sleep(100);
                }
            }
            finally
            {
                ExportGame(gameID, opponent, won);
                BotGameAPI.DeleteGame(gameID);
            }
        }