Skip to content

Instantly share code, notes, and snippets.

/gai

Created September 29, 2010 06:28
Show Gist options
  • Save anonymous/602358 to your computer and use it in GitHub Desktop.
Save anonymous/602358 to your computer and use it in GitHub Desktop.
import java.util.*;
public class MyBot {
// The DoTurn function is where your code goes. The PlanetWars object
// contains the state of the game, including information about all planets
// and fleets that currently exist. Inside this function, you issue orders
// using the pw.IssueOrder() function. For example, to send 10 ships from
// planet 3 to planet 8, you would say pw.IssueOrder(3, 8, 10).
//
// There is already a basic strategy in place here. You can use it as a
// starting point, or you can throw it out entirely and replace it with
// your own. Check out the tutorials and articles on the contest website at
// http://www.ai-contest.com/resources.
public static void DoTurn(PlanetWars pw) {
if (pw.EnemyPlanets().size() == 0) {
return;
}
int x = 1;
// (2) Find my strongest planet.
Planet source = null;
double sourceScore = Double.MIN_VALUE;
for (Planet p : pw.MyPlanets()) {
double score = (double)p.NumShips() / (1 + p.GrowthRate());
if (score > sourceScore) {
sourceScore = score;
source = p;
}
}
// (3) Find the weakest enemy or neutral planet.
Planet dest = null;
double destScore = Double.MIN_VALUE;
for (Planet p : pw.NotMyPlanets()) {
int inscore = 0;
for (Fleet f: pw.EnemyFleets()) {
if (f.DestinationPlanet() == p.PlanetID()) {
if (f.turnsremaining() < pw.Distance(source.PlanetID(),p.PlanetID())) {
int inships = (int)f.NumShips();
inscore = inscore + inships;
}
}
}
if (p.owner == 2) {
double score = (double)(p.GrowthRate()) / (inscore + p.NumShips()+pw.Distance(source.PlanetID(),p.PlanetID()));
if (score > destScore) {
destScore = score;
dest = p;
}
else {
double score = (double)(p.GrowthRate()) / (p.NumShips()+pw.Distance(source.PlanetID(),p.PlanetID()) -
inscore);
if (score > destScore) {
destScore = score;
dest = p;
}
}
}
// (4) Send half the ships from my strongest planet to the weakest
// planet that I do not own.
Fleet flt = null;
int fscore = 0;
for (Fleet f : pw.MyFleets()) {
if ((dest.PlanetID()) == (f.DestinationPlanet())) {
int fleetscore = (int)f.NumShips();
fscore= fscore + fleetscore;
}
}
int ffscore = 0;
for (Fleet f: pw.EnemyFleets()) {
if (f.DestinationPlanet() == dest.PlanetID()) {
int fleetscore1 = (int)f.NumShips();
ffscore= ffscore + fleetscore1;
}
}
int numShips = 1;
if (source != null && dest != null) {
if (dest.Owner() >= 2) {
numShips = (dest.NumShips()+ (pw.Distance(source.PlanetID(),dest.PlanetID())*(dest.GrowthRate())) - fscore +
ffscore +2) ;
}
else {
numShips = (dest.NumShips() + 1- fscore + ffscore) ;
}
if (source.NumShips() > numShips && numShips>0) {
pw.IssueOrder(source, dest, numShips);
x=numShips;
}
}
// (2) Find my strongest planet.
for (Planet q : pw.MyPlanets()) {
double score = (double)q.NumShips() / (1 + q.GrowthRate());
if (score > sourceScore) {
sourceScore = score;
source = q;
}
}
// (3) Find the weakest enemy or neutral planet.
for (Planet q : pw.NotMyPlanets()) {
double score = (double)(q.GrowthRate()) / (q.NumShips()+pw.Distance(source.PlanetID(),q.PlanetID()));
if (score > destScore) {
destScore = score;
dest = q;
}
}
// (4) Send half the ships from my strongest planet to the weakest
// planet that I do not own.
for (Fleet f : pw.MyFleets()) {
if ((dest.PlanetID()) == (f.DestinationPlanet())) {
int fleetscore = (int)f.NumShips();
fscore= fscore + fleetscore;
}
}
for (Fleet f: pw.EnemyFleets()) {
if (f.DestinationPlanet() == dest.PlanetID()) {
int fleetscore1 = (int)f.NumShips();
ffscore= ffscore + fleetscore1;
}
}
if (source != null && dest != null) {
if (dest.Owner() >= 2) {
numShips = (dest.NumShips()+ pw.Distance(source.PlanetID(),dest.PlanetID())*(dest.GrowthRate())-fscore +ffscore
+2) ;
}
else {
numShips = (dest.NumShips() +1-fscore + ffscore) ;
}
if (source.NumShips()-x > numShips && numShips>0) {
pw.IssueOrder(source, dest, numShips);
}
}
public static void main(String[] args) {
String line = "";
String message = "";
int c;
try {
while ((c = System.in.read()) >= 0) {
switch (c) {
case '\n':
if (line.equals("go")) {
PlanetWars pw = new PlanetWars(message);
DoTurn(pw);
pw.FinishTurn();
message = "";
} else {
message += line + "\n";
}
line = "";
break;
default:
line += (char)c;
break;
}
}
} catch (Exception e) {
// Owned.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment