Ejemplo n.º 1
0
        public GameStanding Clone()
        {
            var r = new GameStanding();

            r.Territories = this.Territories.ToDictionary(o => o.Key, o => o.Value.Clone());
            r.ActiveCards = this.ActiveCards.Select(o => o.Clone()).ToList();
            return(r);
        }
Ejemplo n.º 2
0
        private static GameStanding ReadMap(string[] mapInput)
        {
            var ret = new GameStanding(Map.Territories.Values.Select(o => TerritoryStanding.Create(o.ID, TerritoryStanding.FogPlayerID, new Armies(fogged: true))));

            for (var i = 1; i < mapInput.Length; i++)
            {
                var ts = ret.Territories[(TerritoryIDType)int.Parse(mapInput[i++])];
                ts.OwnerPlayerID = (PlayerIDType)ToPlayerID(mapInput[i++]);
                ts.NumArmies     = new Armies(int.Parse(mapInput[i]));
            }
            return(ret);
        }
Ejemplo n.º 3
0
        public static GameStanding ReadGameStanding(JToken jToken)
        {
            var ret = new GameStanding();

            if (jToken == null || (jToken.Type == JTokenType.String && (string)jToken == "null"))
            {
                return(null);
            }

            foreach (var terr in (JArray)jToken)
            {
                var terrID   = (TerritoryIDType)(int)terr["terrID"];
                var playerID = ToPlayerID((string)terr["ownedBy"]);
                var armies   = ToArmies((string)terr["armies"]);
                ret.Territories.Add(terrID, TerritoryStanding.Create(terrID, playerID, armies));
            }

            return(ret);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Determines who controls this bonus. Will return null if nobody does or at least one territory is fogged
        /// </summary>
        public PlayerIDType?ControlsBonus(GameStanding standing)
        {
            Assert.Fatal(standing != null, "standing is null");

            PlayerIDType playerID = (PlayerIDType)int.MinValue;

            foreach (var territoryID in this.Territories)
            {
                TerritoryStanding cs = standing.Territories[territoryID];
                if (cs.OwnerPlayerID == TerritoryStanding.AvailableForDistribution)
                {
                    return(null);
                }
                if (cs.IsNeutral)
                {
                    return(null);
                }
                else if (cs.OwnerPlayerID == TerritoryStanding.FogPlayerID)
                {
                    return(null);
                }
                else if ((int)playerID == int.MinValue)
                {
                    playerID = cs.OwnerPlayerID;
                }
                else if (playerID != cs.OwnerPlayerID)
                {
                    return(null);
                }
            }

            Assert.Fatal(playerID != TerritoryStanding.FogPlayerID && playerID != TerritoryStanding.NeutralPlayerID);
            if ((int)playerID == int.MinValue)
            {
                throw new Exception("Bonus " + this.Name + " (" + this.ID + ") has no territories assigned");
            }

            return(new Nullable <PlayerIDType>(playerID));
        }
Ejemplo n.º 5
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 + "\"");
                }
            }
        }
Ejemplo n.º 6
0
        /*
         * Bot1: setup_map super_regions 1 5 2 4 3 1 4 3 5 4 6 3 7 3 8 2 9 3 10 3 11 6 12 5 13 4
         * Bot1: setup_map regions 1 1 2 1 3 1 4 1 5 1 6 2 7 2 8 2 9 2 10 3 11 3 12 3 13 3 14 4 15 4 16 4 17 4 18 4 19 4 20 5 21 5 22 5 23 5 24 6 25 6 26 6 27 6 28 7 29 7 30 7 31 7 32 7 33 8 34 8 35 8 36 8 37 9 38 9 39 9 40 9 41 10 42 10 43 10 44 10 45 10 46 10 47 11 48 11 49 11 50 11 51 11 52 11 53 11 54 12 55 12 56 12 57 12 58 12 59 13 60 13 61 13 62 13 63 13 64 13
         * Bot1: setup_map neighbors 1 3,2,6 2 3,5,4,8,6 3 4 4 24,5,25 5 24,20,7,8 6 8,7 7 9,8,20 9 20,21 10 11,12 11 12,13 12 13,29 13 29,28 14 15 15 18,28,16 16 17,18 17 18,33,19 18 33,28,34,29,30 19 33 20 21,22,24,26,23 21 23 22 26,23,38,24,59 24 26,27,25,38 25 27 27 37 28 30,29 29 31,30,34,32 31 48,32,51 32 34 33 35,34 34 36,35 35 36 37 38,39,40 38 40,55,60 39 54,40,49 40 55,54,56 41 44,42,46,62 42 44,43,45 43 45 44 61,62,45 45 59,61 46 62 47 48,50 48 51,50 49 50,56,54 50 56,51,52,53 51 52 52 53 53 56,58 54 56 55 57,56 56 58,57 57 58 59 61,63,64,60 60 63 61 62,64 62 64 63 64
         * Bot1: setup_map wastelands 5 13 40 49 62
         */
        private static void SetupMap(string[] mapInput)
        {
            if (Map == null)
            {
                Map = new MapDetails();
            }


            if (mapInput[1] == "super_regions")
            {
                for (var i = 2; i < mapInput.Length; i++)
                {
                    var bonusID = (BonusIDType)int.Parse(mapInput[i]);
                    i++;
                    var reward = int.Parse(mapInput[i]);
                    Map.Bonuses.Add(bonusID, new BonusDetails(Map, bonusID, reward));
                }
            }
            else if (mapInput[1] == "regions")
            {
                for (var i = 2; i < mapInput.Length; i++)
                {
                    var terrID = (TerritoryIDType)int.Parse(mapInput[i]);
                    i++;
                    var bonusID = (BonusIDType)int.Parse(mapInput[i]);
                    Map.Territories.Add(terrID, new TerritoryDetails(Map, terrID));
                    Map.Bonuses[bonusID].Territories.Add(terrID);
                    Map.Territories[terrID].PartOfBonuses.Add(bonusID);
                }
            }
            else if (mapInput[1] == "neighbors")
            {
                for (var i = 2; i < mapInput.Length; i++)
                {
                    var terrID = (TerritoryIDType)int.Parse(mapInput[i++]);

                    var terr = Map.Territories[terrID];
                    mapInput[i].Split(',').Select(o => (TerritoryIDType)int.Parse(o)).ForEach(o => terr.ConnectedTo.Add(o, null));
                    foreach (var conn in terr.ConnectedTo.Keys)
                    {
                        Map.Territories[conn].ConnectedTo[terrID] = null;
                    }
                }

                //Map is now done being read
                DistributionStanding = new GameStanding();
                foreach (var terr in Map.Territories.Values)
                {
                    DistributionStanding.Territories.Add(terr.ID, TerritoryStanding.Create(terr.ID, TerritoryStanding.NeutralPlayerID, new Armies(2)));
                }
            }
            else if (mapInput[1] == "wastelands")
            {
                foreach (var terrID in mapInput.Skip(2).Select(o => (TerritoryIDType)int.Parse(o)).ToList())
                {
                    DistributionStanding.Territories[terrID].NumArmies = new Armies(6);
                }
            }
            else if (mapInput[1] == "opponent_starting_regions")
            {  /* don't care */
            }
            else
            {
                throw new Exception("Unexpected map input: " + mapInput[1]);
            }
        }
Ejemplo n.º 7
0
 public int Cost(ResourceType resource, GameSettings settings, int armiesDeployedSoFarThisTurn, GameStanding standing)
 {
     return(0); //not implemented
 }