public boolean arrival() { List<Fleet> fleets = new ArrayList<>(); boolean arrival = false; if (Fleets().size() == 0) { return true; } for (Fleet fleet : Fleets()) { if (fleet.TurnsRemaining() == 0) { arrival = true; fleets.add(fleet); } } fleets.forEach( fleet -> { Planet dest = GetPlanet(fleet.DestinationPlanet()); if (fleet.Owner() != dest.Owner()) { dest.NumShips(dest.NumShips() - fleet.NumShips()); if (dest.NumShips() < 0) { dest.Owner(fleet.Owner()); dest.NumShips(Math.abs(dest.NumShips())); } } else { dest.NumShips(dest.NumShips() + fleet.NumShips()); } }); return arrival; }
// Return a list of all the fleets owned by the current player. public List<Fleet> MyFleets() { List<Fleet> r = new ArrayList<Fleet>(); for (Fleet f : fleets) { if (f.Owner() == 1) { r.add(f); } } return r; }
private boolean fleet_already_sent(Planet destination) { List<Fleet> fleets = Fleets(); for (Fleet fleet : fleets) { if (fleet.DestinationPlanet() == destination.PlanetID() && fleet.Owner() == 1) { return true; } } return false; }
// ------------------------------------------------// // ------- Custom methods and/or properties: ------// // ------------------------------------------------// private PlanetWars(PlanetWars _pw) { this.planets = new ArrayList<>(); this.fleets = new ArrayList<>(); for (Planet planet : _pw.Planets()) { Planet lol = planet.clone(); this.planets.add(lol); } for (Fleet fleet : _pw.Fleets()) { fleets.add(fleet.clone()); } }
// Returns true if the named player owns at least one planet or fleet. // Otherwise, the player is deemed to be dead and false is returned. public boolean IsAlive(int playerID) { for (Planet p : planets) { if (p.Owner() == playerID) { return true; } } for (Fleet f : fleets) { if (f.Owner() == playerID) { return true; } } return false; }
// Returns the number of ships that the current player has, either located // on planets or in flight. public int NumShips(int playerID) { int numShips = 0; for (Planet p : planets) { if (p.Owner() == playerID) { numShips += p.NumShips(); } } for (Fleet f : fleets) { if (f.Owner() == playerID) { numShips += f.NumShips(); } } return numShips; }
// If the game is not yet over (ie: at least two players have planets or // fleets remaining), returns -1. If the game is over (ie: only one player // is left) then that player's number is returned. If there are no // remaining players, then the game is a draw and 0 is returned. public int Winner() { Set<Integer> remainingPlayers = new TreeSet<Integer>(); for (Planet p : planets) { remainingPlayers.add(p.Owner()); } for (Fleet f : fleets) { remainingPlayers.add(f.Owner()); } switch (remainingPlayers.size()) { case 0: return 0; case 1: return ((Integer) remainingPlayers.toArray()[0]).intValue(); default: return -1; } }
@Override public void DoTurn(PlanetWars pw) { write("==TURN " + ++turnNum); if (mirrorMap == null) { write("==Generating mirrorMap"); mirrorMap = new HashMap<Integer, Planet>(); double mapCenterX = 0; double mapCenterY = 0; for (Planet p : pw.Planets()) { mapCenterX += p.X(); mapCenterY += p.Y(); } mapCenterX = mapCenterX / pw.Planets().size(); mapCenterY = mapCenterY / pw.Planets().size(); // write("Map Center: " + "(" + mapCenterX + ", " + mapCenterY + ")"); HashMap<Point, Planet> mirroredLocation = new HashMap<Point, Planet>(); for (Planet p : pw.Planets()) { if (p.X() == mapCenterX && p.Y() == mapCenterY) { write("-- Detected Center Planet --"); mirrorMap.put(p.PlanetID(), p); continue; } Point mirroredPoint = new Point(p.X(), p.Y()); // write("Looking for point of: " + mirroredPoint + " for " + p.PlanetID()); if (mirroredLocation.containsKey(mirroredPoint)) { Planet p1 = mirroredLocation.get(mirroredPoint); write("-- Detected Mirror Planet: " + p.PlanetID() + " = " + p1.PlanetID() + " --"); mirrorMap.put(p1.PlanetID(), p); mirrorMap.put(p.PlanetID(), p1); } else { mirroredPoint.x = mapCenterX + mapCenterX - p.X(); mirroredPoint.y = mapCenterY + mapCenterY - p.Y(); mirroredLocation.put(mirroredPoint, p); // write("Set mirror point of: " + mirroredPoint + " for " + p.PlanetID()); } } write("--Generated mirrorMap"); } if (pw.EnemyFleets().size() > 0) { write("Checking Enemy Fleets"); List<Fleet> fleets = pw.EnemyFleets(); for (Fleet o : mirroredFleets) { fleets.remove(o); } write("Fleet count: " + fleets.size() + " -- mirrored: " + mirroredFleets.size()); for (Fleet f : fleets) { boolean handled = false; Planet src = mirrorMap.get(f.SourcePlanet()); Planet dest = mirrorMap.get(f.DestinationPlanet()); // write("lamo: " + f.SourcePlanet() + " " + f.DestinationPlanet()); // write("lame: " + src.PlanetID() + " " + dest.PlanetID()); // write("owner: " + src.Owner() + " " + dest.Owner()); // Don't trade fleets between mirrored planets: if (src.PlanetID() != f.DestinationPlanet()) { write("Ships: " + src.PlanetID() + " -- " + src.NumShips() + " - " + f.NumShips()); if (src.NumShips() > f.NumShips() && src.Owner() == 1) { pw.IssueOrder(src, dest, f.NumShips()); // src.NumShips(src.NumShips() - f.NumShips()); handled = true; } } else { handled = true; } if (handled) mirroredFleets.add(f); // } // else { // write("Already handled fleet"); // } } } else { write("Waiting"); // write("Attacking Something"); // if (distances == null) { // distances = new PlanetDistances(pw); // } // // //Attack something // for (Planet p : pw.MyPlanets()) { // Planet attack = Find.NearestPlanetNotOwnedCanTake(pw.Planets(), distances, // p.PlanetID(), p.NumShips() - 10); // if (attack != null && p.NumShips() > 20) { // pw.IssueOrder(p.PlanetID(), attack.PlanetID(), p.NumShips() - 15); // } // } } }