コード例 #1
0
        public static void PlayForTurn(string botName, GameIDType gameID, int playForTurn)
        {
            AILog.Log("PlayHuman", "Generating orders for game " + gameID + " turn " + playForTurn);
            var settings = HumanGameAPI.GetGameSettings(gameID);
            var game     = HumanGameAPI.GetGameInfo(gameID, playForTurn);

            EntryPoint.PlayGame(botName, game, MeID, settings.Item1, settings.Item2, picks => { }, orders => { });
        }
コード例 #2
0
ファイル: PlayFFA.cs プロジェクト: Norman1/Multiattack-Bot
        private static void PlayGame(string[] args)
        {
            var botName = args[0];

            AILog.Log("PlayFFA", "Creating game...");
            var gameID = BotGameAPI.CreateGame(Enumerable.Range(10, 6).Select(o => PlayerInvite.Create((PlayerIDType)o, PlayerInvite.NoTeam, null)), "PlayFFA", null, gameSettings =>
                                               //var gameID = BotGameAPI.CreateGame(Enumerable.Range(10, 6).Select(o => PlayerInvite.Create((PlayerIDType)o, (TeamIDType)(o == 0 ? 0 : 1), (SlotType)o)), "PlayFFA", 17, gameSettings =>
            {
                //gameSettings["DistributionMode"] = 0; //full distribution, so we have plenty of starting territories
                //gameSettings["InitialNeutralsInDistribution"] = 2; //since we're full distribution, most territories an in-distribution one so we want them to start with 2s not 4s
                gameSettings["MaxCardsHold"] = 999;
                //gameSettings["AutomaticTerritoryDistribution"] = "Automatic";
            });

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

            var settings = BotGameAPI.GetGameSettings(gameID);

            try
            {
                while (true)
                {
                    var game = BotGameAPI.GetGameInfo(gameID, null);
                    if (game.State == GameState.Finished)
                    {
                        AILog.Log("PlayFFA", "Game finished: " + gameID);
                        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(botName, 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 (args.Any(o => o.ToLower() == "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);
            }
        }
コード例 #3
0
        public static void PlayLoop(string botName, GameIDType gameID, PlayerIDType playerID)
        {
            var settings = HumanGameAPI.GetGameSettings(gameID);

            int  turnNumber    = int.MinValue;
            bool checkedAccept = false;

            while (true)
            {
                var status = HumanGameAPI.GetGameStatus(gameID);
                if (status.Item2 == GameState.WaitingForPlayers)
                {
                    if (!checkedAccept)
                    {
                        var game = HumanGameAPI.GetGameInfo(gameID, null);
                        if (game.Players[playerID].State == GamePlayerState.Invited)
                        {
                            AILog.Log("PlayHuman", "Accepting invite...");
                            HumanGameAPI.AcceptGame(gameID);
                            AILog.Log("PlayHuman", "Accepted invite");
                        }

                        checkedAccept = true;
                    }
                }
                else if (status.Item2 == GameState.Finished)
                {
                    AILog.Log("PlayHuman", "Game finished");
                    break;
                }
                else if (status.Item1 > turnNumber)
                {
                    var game = HumanGameAPI.GetGameInfo(gameID, null);

                    if (!EntryPoint.PlayGame(botName, game, MeID, settings.Item1, settings.Item2, picks =>
                    {
                        HumanGameAPI.SendPicks(game.ID, picks);
                        AILog.Log("PlayHuman", "Sent picks");
                    }, orders =>
                    {
                        HumanGameAPI.SendOrders(game.ID, orders, game.NumberOfTurns + 1);
                        AILog.Log("PlayHuman", "Sent orders");
                    }))
                    {
                        AILog.Log("PlayHuman", "We're no longer alive");
                        break;
                    }
                    turnNumber = status.Item1;
                }

                Thread.Sleep(10000);
            }
        }
コード例 #4
0
        public static void PlayGame(string botName)
        {
            var players = new[] {
                PlayerInvite.Create(MeID, PlayerInvite.NoTeam, null),
                PlayerInvite.Create("*****@*****.**", PlayerInvite.NoTeam, null)
            };

            AILog.Log("StressTest", "Creating game...");
            var gameID = BotGameAPI.CreateGame(players, "AI Competition", null, gameSettings =>
            {
                gameSettings["MaxCardsHold"] = 999;

                gameSettings["Map"] = 52545; //53822; //hex earth, 3200 territories
                //gameSettings["Map"] = 24591; //big USA, 3066 territories
                //gameSettings["Map"] = 42717; //thirty years war, 2264 territories
                //gameSettings["Map"] = 34083; //Africa big, 1544 territories
                gameSettings["DistributionMode"] = 0; //full dist
                gameSettings["TerritoryLimit"]   = 0; //terr limit
                gameSettings["MultiAttack"]      = true; gameSettings["AllowPercentageAttacks"] = true;
                //gameSettings["AutomaticTerritoryDistribution"] = "Automatic"; //skip picking, if you're only looking to optimize orders
            });


            var settings = BotGameAPI.GetGameSettings(gameID);

            try
            {
                while (true)
                {
                    var game = BotGameAPI.GetGameInfo(gameID, MeID);
                    if (game.State == GameState.Finished || game.NumberOfTurns >= 2)
                    {
                        break;
                    }

                    if (!EntryPoint.PlayGame(botName, game, MeID, settings.Item1, settings.Item2, picks => BotGameAPI.SendPicks(game.ID, MeID, picks), orders => BotGameAPI.SendOrders(game.ID, MeID, orders, game.NumberOfTurns + 1)))
                    {
                        break;
                    }

                    Thread.Sleep(100);
                }
            }
            finally
            {
                ExportGame(gameID);
                BotGameAPI.DeleteGame(gameID);
            }
        }
コード例 #5
0
        public static void PlayGame(string botName)
        {
            var players = new[] { PlayerInvite.Create(MeID, (TeamIDType)3, null) }.Concat(Enumerable.Range(0, 6).Select(o => PlayerInvite.Create("*****@*****.**", (TeamIDType)(o % 2), null)));

            AILog.Log("PlayAI", "Creating game...");
            var gameID = BotGameAPI.CreateGame(players, "Crazy Challenge", 16, gameSettings =>
            {
                gameSettings["MaxCardsHold"] = 999;
                gameSettings["Fog"]          = "NoFog";
            });

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

            var  settings = BotGameAPI.GetGameSettings(gameID);
            bool?weWon    = null;

            try
            {
                while (true)
                {
                    var game = BotGameAPI.GetGameInfo(gameID, MeID);
                    if (game.State == GameState.Finished)
                    {
                        weWon = GameFinished(game);
                        break;
                    }

                    if (!EntryPoint.PlayGame(botName, game, MeID, settings.Item1, settings.Item2, picks => BotGameAPI.SendPicks(game.ID, MeID, picks), orders => BotGameAPI.SendOrders(game.ID, MeID, orders, game.NumberOfTurns + 1)))
                    {
                        weWon = GameFinished(game);
                        break;
                    }

                    Thread.Sleep(100);
                }
            }
            finally
            {
                ExportGame(gameID, weWon);
                BotGameAPI.DeleteGame(gameID);
            }
        }
コード例 #6
0
        public static void Create(string botName, string opponent, string gameName)
        {
            AILog.Log("PlayHuman", "Creating game...");
            var gameID = HumanGameAPI.CreateGame(new[] {
                PlayerInvite.Create("me", (TeamIDType)0, null),
                PlayerInvite.Create("*****@*****.**", (TeamIDType)0, null),
                PlayerInvite.Create(opponent, (TeamIDType)1, null)
            }, gameName, null, settings =>
            {
                settings["Fog"]          = "NoFog";    //turn off fog so we can see what the AI is doing
                settings["MaxCardsHold"] = 999;        //so AI doesn't have to discard
                //settings["OrderPriorityCard"] = "none";
                //settings["OrderDelayCard"] = "none";
                //settings["BlockadeCard"] = "none";
                //settings["NumberOfCardsToReceiveEachTurn"] = 1;
            });

            AILog.Log("PlayHuman", "Created game " + gameID);
            PlayLoop(botName, gameID, MeID);
        }
コード例 #7
0
ファイル: EntryPoint.cs プロジェクト: Norman1/Multiattack-Bot
        public static bool PlayGame(string botName, GameObject game, PlayerIDType playerID, GameSettings settings, MapDetails map, Action <List <TerritoryIDType> > sendPicks, Action <List <GameOrder> > sendOrders)
        {
            if (game.State == GameState.WaitingForPlayers)
            {
                return(true);
            }

            if (!game.Players.ContainsKey(playerID))
            {
                return(false); //not in game
            }
            if (game.Players[playerID].State == GamePlayerState.Invited)
            {
                throw new NotImplementedException("TODO: Accept the invite");
            }
            if (game.Players[playerID].State != GamePlayerState.Playing)
            {
                return(false); //not alive anymore
            }
            var bot = BotFactory.Construct(botName);

            bot.Init(game.ID, playerID, game.Players, map, game.LatestInfo.DistributionStanding, settings, game.NumberOfTurns, game.LatestInfo.Income, game.LatestInfo.LatestTurn == null ? null : game.LatestInfo.LatestTurn.Orders, game.LatestInfo.LatestStanding, game.LatestInfo.PreviousTurnStanding, game.LatestInfo.TeammatesOrders, game.LatestInfo.Cards, game.LatestInfo.CardsMustUse, Stopwatch.StartNew(), new List <string>());

            AILog.Log("PlayGame", "State=" + game.State + ", numTurns=" + game.NumberOfTurns + ", income=" + game.LatestInfo.Income[playerID] + ", cardsMustUse=" + game.LatestInfo.CardsMustUse);

            if (game.State == GameState.DistributingTerritories)
            {
                sendPicks(_speeds.GetOrAdd(botName, _ => new Speeds()).Record(true, () => bot.GetPicks()));
            }
            else if (game.State == GameState.Playing)
            {
                sendOrders(_speeds.GetOrAdd(botName, _ => new Speeds()).Record(false, () => bot.GetOrders()));
            }

            return(true);
        }
コード例 #8
0
        public static void Go(string[] args)
        {
            Console.SetIn(new StreamReader(Console.OpenStandardInput(512))); //from http://theaigames.com/languages/cs

            while (true)
            {
                var line = Console.ReadLine();
                if (line == null)
                {
                    break;
                }
                line = line.Trim();

                if (line.Length == 0)
                {
                    continue;
                }

                var parts = line.Split(' ');
                if (parts[0] == "pick_starting_region")
                {
                    if (PickedTerritories == null)
                    {
                        LatestTurnStanding = DistributionStanding; //during picking, LatestStanding and DistributionStanding are the same thing
                        InitBot();
                        PickedTerritories = Bot.GetPicks();
                        AILog.Log("AIGamesParser", "Bot picked " + PickedTerritories.Select(o => o.ToString()).JoinStrings(" "));
                    }

                    var timeout = long.Parse(parts[1]);
                    // pick which territories you want to start with
                    var pickableStartingTerritories = parts.Skip(2).Select(o => (TerritoryIDType)int.Parse(o)).ToHashSet(false);
                    var pick = PickedTerritories.Where(o => pickableStartingTerritories.Contains(o)).ToList();

                    if (pick.Count > 0)
                    {
                        Console.Out.WriteLine(pick[0]);
                    }
                    else
                    {
                        AILog.Log("AIGamesParser", "None of bot's picks are still available, picking random");
                        Console.Out.WriteLine(pickableStartingTerritories.Random());
                    }
                }
                else if (parts[0] == "go")
                {
                    var timeout = long.Parse(parts[2]);
                    Assert.Fatal(parts.Length == 3);
                    NumberOfTurns++;


                    // we need to do a move
                    var output = new StringBuilder();
                    if (parts[1] == "place_armies")
                    {
                        AILog.Log("AIGamesParser", "================= Beginning turn " + NumberOfTurns + " =================");

                        //Re-create the bot before every turn.  This is done to simulate how the bot will run in production -- it can't be active and maintaining state for the entirety of a multi-day game, since those can take months or years.  Instead, it will be created before each turn, ran once, then thrown away.
                        InitBot();

                        CompletedOrders = Bot.GetOrders();

                        // place armies
                        foreach (var move in CompletedOrders.OfType <GameOrderDeploy>())
                        {
                            output.Append(GetDeployString(move) + ",");
                        }
                    }
                    else if (parts[1] == "attack/transfer")
                    {
                        foreach (var move in CompletedOrders.OfType <GameOrderAttackTransfer>())
                        {
                            output.Append(GetAttackString(move) + ",");
                        }
                    }
                    else
                    {
                        throw new Exception("Unexpected " + parts[1]);
                    }

                    if (output.Length > 0)
                    {
                        Console.Out.WriteLine(output);
                    }
                    else
                    {
                        Console.Out.WriteLine("No moves");
                    }
                }
                else if (parts[0] == "settings" && parts[1] == "starting_regions")
                {
                    foreach (var terrID in parts.Skip(2).Select(o => (TerritoryIDType)int.Parse(o)))
                    {
                        DistributionStanding.Territories[terrID].OwnerPlayerID = TerritoryStanding.AvailableForDistribution;
                    }
                }
                else if (parts.Length == 3 && parts[0] == "settings")
                {
                    UpdateSettings(parts[1], parts[2]); // update settings
                }
                else if (parts[0] == "setup_map")
                {
                    SetupMap(parts); // initial full map is given
                }
                else if (parts[0] == "update_map")
                {
                    PreviousTurnStanding = LatestTurnStanding;
                    LatestTurnStanding   = ReadMap(parts);
                }
                else if (parts[0] == "opponent_moves")
                {
                    ReadOpponentMoves(parts); // all visible opponent moves are given
                }
                else
                {
                    throw new Exception("Unable to parse line \"" + line + "\"");
                }
            }
        }
コード例 #9
0
ファイル: EntryPoint.cs プロジェクト: Norman1/Multiattack-Bot
 public static void LogSpeeds()
 {
     AILog.Log("Speeds", _speeds.Select(o => o.Key + " " + o.Value).JoinStrings(", "));
 }
コード例 #10
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);
            }
        }
コード例 #11
0
ファイル: Compete.cs プロジェクト: Norman1/Multiattack-Bot
        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);
            }
        }