//Resolves the battle at planet p, if there is one. //* Removes all fleets involved in the battle //* Sets the number of ships and owner of the planet according the outcome private void FightBattle(Planet p) { Map<int, int> participants = new TreeMap<int, int>(); participants.put(p.Owner, p.NumShips); bool doBattle = false; //PORT: Deleting items from iterators is not allowed. // converted to deletable for loop for (int fleetIndex = 0; fleetIndex < Fleets.Count; ) { Fleet fl = Fleets[fleetIndex]; if (fl.TurnsRemaining <= 0 && fl.DestinationPlanet == p.PlanetID) { int attackForce; doBattle = true; if (participants.TryGetValue(fl.Owner, out attackForce)) { participants[fl.Owner] = attackForce + fl.NumShips; } else { participants.Add(fl.Owner, fl.NumShips); } Fleets.Remove(fl); } else { fleetIndex++; } } if (doBattle) { Fleet winner = new Fleet(0, 0); Fleet second = new Fleet(0, 0); foreach (var f in participants) { if (f.Value > second.NumShips) { if (f.Value > winner.NumShips) { second = winner; winner = new Fleet(f.Key, f.Value); } else { second = new Fleet(f.Key, f.Value); } } } if (winner.NumShips > second.NumShips) { p.SetNumShips(winner.NumShips - second.NumShips); p.SetOwner(winner.Owner); } else { p.SetNumShips(0); } } }
// Parses a game state from a string. On success, returns 1. On failure, // returns 0. private int ParseGameState(String s) { Planets.Clear(); Fleets.Clear(); ClearDupeCache(); //PORT: Make WindowsCompatible String[] lines = s.Replace("\r\n", "\n").Split('\n'); int planetid = 0; 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; } if (tokens[0].Equals("P")) { if (tokens.Length != 6) { return 0; } double x = Double.parseDouble(tokens[1]); double y = Double.parseDouble(tokens[2]); int owner = Integer.parseInt(tokens[3]); int numShips = Integer.parseInt(tokens[4]); int growthRate = Integer.parseInt(tokens[5]); Planet p = new Planet(planetid++, owner, numShips, growthRate, x, y); Planets.Add(p); if (gamePlayback.Length > 0) { gamePlayback.Append(":"); } gamePlayback.Append("" + x + "," + y + "," + owner + "," + numShips + "," + growthRate); } else if (tokens[0].Equals("F")) { if (tokens.Length != 7) { return 0; } int owner = Integer.parseInt(tokens[1]); int numShips = Integer.parseInt(tokens[2]); int source = Integer.parseInt(tokens[3]); int destination = Integer.parseInt(tokens[4]); int totalTripLength = Integer.parseInt(tokens[5]); int turnsRemaining = Integer.parseInt(tokens[6]); Fleet f = new Fleet(owner, numShips, source, destination, totalTripLength, turnsRemaining); Fleets.Add(f); } else { return 0; } } gamePlayback.Append("|"); return 1; }
// Issue an order. This function takes num_ships off the source_planet, // puts them into a newly-created fleet, calculates the distance to the // destination_planet, and sets the fleet's total trip time to that // distance. Checks that the given player_id is allowed to give the given // order. If not, the offending player is kicked from the game. If the // order was carried out without any issue, and everything is peachy, then // 0 is returned. Otherwise, -1 is returned. public int IssueOrder(int playerID, int sourcePlanet, int destinationPlanet, int numShips) { Planet source = Planets[sourcePlanet]; bool isIllegalMove = source.Owner != playerID || numShips > source.NumShips || numShips < 0; if (isIllegalMove) { string message = String.Format( "Illegal move player: {0}. From planet {1} owned by {2} with {3} ships, wants to send {4} ships", playerID, sourcePlanet, source.Owner, numShips, source.NumShips); System.Diagnostics.Debug.Assert(false, message); WriteLogMessage(message); DropPlayer(playerID); return -1; } source.RemoveShips(numShips); string fleetKey = playerID + ";" + sourcePlanet + ";" + destinationPlanet; Fleet duplicate; if (deDupe.TryGetValue(fleetKey, out duplicate)) { //ha there is no add... duplicate.RemoveShips(-numShips); } else { int distance = Distance(sourcePlanet, destinationPlanet); Fleet f = new Fleet(source.Owner, numShips, sourcePlanet, destinationPlanet, distance, distance); deDupe.Add(fleetKey, f); Fleets.Add(f); } return 0; }
public void AddFleet(Fleet f) { Fleets.Add(f); }
// Parses a game state from a string. On success, returns 1. On failure, // returns 0. private int ParseGameState(String s) { Planets.Clear(); Fleets.Clear(); ClearDupeCache(); //PORT: Make WindowsCompatible String[] lines = s.Replace("\r\n", "\n").Split('\n'); int planetid = 0; 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; } if (tokens[0].Equals("P")) { if (tokens.Length != 6) { return(0); } double x = Double.parseDouble(tokens[1]); double y = Double.parseDouble(tokens[2]); int owner = Integer.parseInt(tokens[3]); int numShips = Integer.parseInt(tokens[4]); int growthRate = Integer.parseInt(tokens[5]); Planet p = new Planet(planetid++, owner, numShips, growthRate, x, y); Planets.Add(p); if (gamePlayback.Length > 0) { gamePlayback.Append(":"); } gamePlayback.Append("" + x + "," + y + "," + owner + "," + numShips + "," + growthRate); } else if (tokens[0].Equals("F")) { if (tokens.Length != 7) { return(0); } int owner = Integer.parseInt(tokens[1]); int numShips = Integer.parseInt(tokens[2]); int source = Integer.parseInt(tokens[3]); int destination = Integer.parseInt(tokens[4]); int totalTripLength = Integer.parseInt(tokens[5]); int turnsRemaining = Integer.parseInt(tokens[6]); Fleet f = new Fleet(owner, numShips, source, destination, totalTripLength, turnsRemaining); Fleets.Add(f); } else { return(0); } } gamePlayback.Append("|"); return(1); }
//Resolves the battle at planet p, if there is one. //* Removes all fleets involved in the battle //* Sets the number of ships and owner of the planet according the outcome private void FightBattle(Planet p) { Map <int, int> participants = new TreeMap <int, int>(); participants.put(p.Owner, p.NumShips); bool doBattle = false; //PORT: Deleting items from iterators is not allowed. // converted to deletable for loop for (int fleetIndex = 0; fleetIndex < Fleets.Count;) { Fleet fl = Fleets[fleetIndex]; if (fl.TurnsRemaining <= 0 && fl.DestinationPlanet == p.PlanetID) { int attackForce; doBattle = true; if (participants.TryGetValue(fl.Owner, out attackForce)) { participants[fl.Owner] = attackForce + fl.NumShips; } else { participants.Add(fl.Owner, fl.NumShips); } Fleets.Remove(fl); } else { fleetIndex++; } } if (doBattle) { Fleet winner = new Fleet(0, 0); Fleet second = new Fleet(0, 0); foreach (var f in participants) { if (f.Value > second.NumShips) { if (f.Value > winner.NumShips) { second = winner; winner = new Fleet(f.Key, f.Value); } else { second = new Fleet(f.Key, f.Value); } } } if (winner.NumShips > second.NumShips) { p.SetNumShips(winner.NumShips - second.NumShips); p.SetOwner(winner.Owner); } else { p.SetNumShips(0); } } }