/// <summary> /// Ensures the player. /// </summary> /// <param name="playerBuilder">The player builder.</param> /// <param name="id">The id.</param> private static EditablePlayer EnsurePlayer(Dictionary <int, EditablePlayer> playerBuilder, int id) { EditablePlayer result; if (!playerBuilder.TryGetValue(id, out result)) { result = new EditablePlayer(); playerBuilder.Add(id, result); } return(result); }
// Parses a game state from a string. On success, returns 1. On failure, // returns 0. private int ParseGameState(string boardLayout) { List <Fleet> fleetbuilder = new List <Fleet>(); Dictionary <int, EditablePlayer> playerBuilder = new Dictionary <int, EditablePlayer>(); int planetID = 0; string[] lines = boardLayout.Replace("\r", "").Split('\n'); for (int i = 0; i < lines.Length; ++i) { string line = lines[i]; int commentBegin = line.IndexOf('#'); if (commentBegin >= 0) { line = line.Substring(0, commentBegin); } if (line.Trim().Length == 0) { continue; } string[] tokens = line.Split(' '); if (tokens.Length == 0) { continue; } switch (tokens[0]) { default: break; case "P": case "p": #region Add planet { if (tokens.Length != 6) { return(0); } Planet planet = BuildPlanet(planetID++, tokens); EditablePlayer player = EnsurePlayer(playerBuilder, planet.Owner); player.Planets.Add(planet); planet.IdleForThisNumberOfTurns++; player.ShipsOnBase += planet.NumShips; player.ShipGrowth += planet.GrowthRate; player.ShipsHeavyPoint.X += planet.X * (double)planet.NumShips; player.ShipsHeavyPoint.Y += planet.Y * (double)planet.NumShips; player.AbsolutePlanetFocus.Y += planet.Y; player.AbsolutePlanetFocus.X += planet.X; if (WishList.ContainsKey(planet.PlanetID)) { if (planet.IsMine) { WishList.Remove(planet.PlanetID); } else { planet.IsOnWishList = true; } } #endregion } break; case "F": case "f": { #region AddFleet if (tokens.Length != 7) { return(0); } Fleet fleet = BuildFleet(tokens); EditablePlayer player = EnsurePlayer(playerBuilder, fleet.Owner); player.Fleets.Add(fleet); player.Targets.Add(fleet.DestinationPlanetId); fleetbuilder.Add(fleet); player.ShipsInTransit += fleet.NumShips; player.ShipInTransitFocus.X += fleet.DestinationPlanet.X * (double)fleet.NumShips; player.ShipInTransitFocus.Y += fleet.DestinationPlanet.Y * (double)fleet.NumShips; #endregion } break; } } //That's me EditablePlayer personalityBuilder = EnsurePlayer(playerBuilder, 1); Me = new Player(personalityBuilder.Fleets, personalityBuilder.Planets, personalityBuilder.Targets); personalityBuilder.InitializePlayer(Me); EditablePlayer neutralityBuilder = EnsurePlayer(playerBuilder, 0); Neutral = new Player(neutralityBuilder.Fleets, neutralityBuilder.Planets, personalityBuilder.Targets); neutralityBuilder.InitializePlayer(Neutral); All = new Player(fleetbuilder, Planets.Values.ToList(), new List <int>()); foreach (EditablePlayer player in playerBuilder.Values) { TotalFleetCount += player.ShipsOnBase + player.ShipsInTransit; } EditablePlayer enemyBuilder = EnsurePlayer(playerBuilder, 2); Enemy = new Player(enemyBuilder.Fleets, enemyBuilder.Planets, enemyBuilder.Targets); enemyBuilder.InitializePlayer(Enemy); All.ShipGrowth = Me.ShipGrowth + Enemy.ShipGrowth + Neutral.ShipGrowth; if (Universe.TurnCount == 1) { CreatePlanetaryTravelRoutes(); } foreach (Planet futurePlane in Planets.Values) { futurePlane.CreateTurnPredictions(); } DeterminePlanetStrengthOnFleetArrival(); return(1); }