Skip to content

Commit

Permalink
inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate committed Nov 25, 2010
0 parents commit 86e60b4
Show file tree
Hide file tree
Showing 152 changed files with 32,366 additions and 0 deletions.
Empty file added __init__.py
Empty file.
Binary file added __init__.pyc
Binary file not shown.
65 changes: 65 additions & 0 deletions bot.py
@@ -0,0 +1,65 @@
from itertools import chain
from logging import getLogger
from planetwars import BaseBot, Game
from fleets import Fleets, bigger
from planetwars.player import ME, ENEMY
from exceptions import MustAbandon, InsufficientFleets
log = getLogger(__name__)

def order_neutrals(universe):
neutrals = universe.nobodies_planets
return sorted(neutrals, cmp=bigger)


class MustAbandon(Exception):
pass


class InsufficientFleets(Exception):
pass


class MyBot(BaseBot):
def __init__(self, *args, **kwargs):
super(MyBot, self).__init__(*args, **args)

def do_turn(self):
# 1. For each newly issued fleet to your planets,
# issue a commitment to stop it
# 2. For each newly issued fleet to your neutrals,
# issue a commitment to stop it
# 3. Issue commitments to new neutrals
# 4. Update all bids
universe = self.universe
planets = universe.my_planets
en_route = lambda n: universe.find_fleets(destination=n, owner=ME)
contests = filter(en_route, universe.nobodies_planets)
attacking = universe.new_fleets(owner=ENEMY, destination=planets)
contesting = universe.new_fleets(owner=ENEMY, destination=contests)
for fleet in chain(attacking, contesting):
fleet.mark()
try:
fleet.destination.commit_against(fleet)
except MustAbandon:
fleet.destination.abandon_to(fleet)
for target in self.targets():
try:
target.conquer()
except InsufficientFleets:
continue

fleets = Fleets(self.universe, log)
fleets.reinforce()
neutrals = list(order_neutrals(self.universe))
for i in range(50):
fleets.increase_range()
for neutral in neutrals:
if fleets.can_take(neutral):
log.info("Taking %s" % neutral)
fleets.take(neutral)
log.info(fleets.range)
if not self.universe.nobodies_planets:
for enemy in self.universe.enemy_planets:
fleets.conquer(enemy)

Game(MyBot)
Binary file added bot.pyc
Binary file not shown.
Binary file added example_bots/BullyBot.jar
Binary file not shown.
65 changes: 65 additions & 0 deletions example_bots/BullyBot.java
@@ -0,0 +1,65 @@
import java.util.*;

public class BullyBot {
public static void DoTurn(PlanetWars pw) {
// (1) If we current have a fleet in flight, just do nothing.
if (pw.MyFleets().size() >= 1) {
return;
}
// (2) Find my strongest planet.
Planet source = null;
double sourceScore = Double.MIN_VALUE;
for (Planet p : pw.MyPlanets()) {
double score = (double)p.NumShips();
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()) {
double score = 1.0 / (1 + p.NumShips());
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.
if (source != null && dest != null) {
int numShips = source.NumShips() / 2;
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.
}
}
}

Binary file added example_bots/DualBot.jar
Binary file not shown.
86 changes: 86 additions & 0 deletions example_bots/DualBot.java
@@ -0,0 +1,86 @@
import java.util.*;

public class DualBot {
public static void DoTurn(PlanetWars pw) {
int numFleets = 1;
boolean attackMode = false;
if (pw.NumShips(1) > pw.NumShips(2)) {
if (pw.Production(1) > pw.Production(2)) {
numFleets = 1;
attackMode = true;
} else {
numFleets = 3;
}
} else {
if (pw.Production(1) > pw.Production(2)) {
numFleets = 1;
} else {
numFleets = 5;
}
}
// (1) If we current have more tha numFleets fleets in flight, just do
// nothing until at least one of the fleets arrives.
if (pw.MyFleets().size() >= numFleets) {
return;
}
// (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;
List<Planet> candidates = pw.NotMyPlanets();
if (attackMode) {
candidates = pw.EnemyPlanets();
}
for (Planet p : candidates) {
double score = (double)(1 + p.GrowthRate()) / p.NumShips();
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.
if (source != null && dest != null) {
int numShips = source.NumShips() / 2;
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.
}
}
}

65 changes: 65 additions & 0 deletions example_bots/ExpandBot.java
@@ -0,0 +1,65 @@
import java.util.*;

public class BullyBot {
public static void DoTurn(PlanetWars pw) {
// (1) If we current have a fleet in flight, just do nothing.
if (pw.MyFleets().size() >= 1) {
return;
}
// (2) Find my strongest planet.
Planet source = null;
double sourceScore = Double.MIN_VALUE;
for (Planet p : pw.MyPlanets()) {
double score = (double)p.NumShips();
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()) {
double score = 1.0 / (1 + p.NumShips());
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.
if (source != null && dest != null) {
int numShips = source.NumShips() / 2;
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.
}
}
}

92 changes: 92 additions & 0 deletions example_bots/Fleet.java
@@ -0,0 +1,92 @@
public class Fleet implements Comparable, Cloneable {
// Initializes a fleet.
public Fleet(int owner,
int numShips,
int sourcePlanet,
int destinationPlanet,
int totalTripLength,
int turnsRemaining) {
this.owner = owner;
this.numShips = numShips;
this.sourcePlanet = sourcePlanet;
this.destinationPlanet = destinationPlanet;
this.totalTripLength = totalTripLength;
this.turnsRemaining = turnsRemaining;
}

// Initializes a fleet.
public Fleet(int owner,
int numShips) {
this.owner = owner;
this.numShips = numShips;
this.sourcePlanet = -1;
this.destinationPlanet = -1;
this.totalTripLength = -1;
this.turnsRemaining = -1;
}

// Accessors and simple modification functions. These should be mostly
// self-explanatory.
public int Owner() {
return owner;
}

public int NumShips() {
return numShips;
}

public int SourcePlanet() {
return sourcePlanet;
}

public int DestinationPlanet() {
return destinationPlanet;
}

public int TotalTripLength() {
return totalTripLength;
}

public int TurnsRemaining() {
return turnsRemaining;
}

public void RemoveShips(int amount) {
numShips -= amount;
}

// Subtracts one turn remaining. Call this function to make the fleet get
// one turn closer to its destination.
public void TimeStep() {
if (turnsRemaining > 0) {
--turnsRemaining;
} else {
turnsRemaining = 0;
}
}

@Override
public int compareTo(Object o) {
Fleet f = (Fleet)o;
return this.numShips - f.numShips;
}

private int owner;
private int numShips;
private int sourcePlanet;
private int destinationPlanet;
private int totalTripLength;
private int turnsRemaining;

private Fleet(Fleet _f) {
owner = _f.owner;
numShips = _f.numShips;
sourcePlanet = _f.sourcePlanet;
destinationPlanet = _f.destinationPlanet;
totalTripLength = _f.totalTripLength;
turnsRemaining = _f.turnsRemaining;
}
public Object clone() {
return new Fleet(this);
}
}

0 comments on commit 86e60b4

Please sign in to comment.