Skip to content

Commit

Permalink
Updated example funcs player for 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
andywang02 authored Jan 9, 2023
1 parent 18fef25 commit 10d3051
Showing 1 changed file with 88 additions and 35 deletions.
123 changes: 88 additions & 35 deletions src/examplefuncsplayer/RobotPlayer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package examplefuncsplayer;

import battlecode.common.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
* RobotPlayer is the class that describes your main robot strategy.
Expand Down Expand Up @@ -60,7 +66,6 @@ public static void run(RobotController rc) throws GameActionException {
// loop, we call Clock.yield(), signifying that we've done everything we want to do.

turnCount += 1; // We have now been alive for one more turn!
System.out.println("Age: " + turnCount + "; Location: " + rc.getLocation());

// Try/catch blocks stop unhandled exceptions, which cause your robot to explode.
try {
Expand All @@ -69,14 +74,14 @@ public static void run(RobotController rc) throws GameActionException {
// use different strategies on different robots. If you wish, you are free to rewrite
// this into a different control structure!
switch (rc.getType()) {
case ARCHON: runArchon(rc); break;
case MINER: runMiner(rc); break;
case SOLDIER: runSoldier(rc); break;
case LABORATORY: // Examplefuncsplayer doesn't use any of these robot types below.
case WATCHTOWER: // You might want to give them a try!
case BUILDER:
case SAGE: break;
case HEADQUARTERS: runHeadquarters(rc); break;
case CARRIER: runCarrier(rc); break;
case LAUNCHER: runLauncher(rc); break;
case BOOSTER: // Examplefuncsplayer doesn't use any of these robot types below.
case DESTABILIZER: // You might want to give them a try!
case AMPLIFIER: break;
}

} catch (GameActionException e) {
// Oh no! It looks like we did something illegal in the Battlecode world. You should
// handle GameActionExceptions judiciously, in case unexpected events occur in the game
Expand All @@ -102,68 +107,117 @@ public static void run(RobotController rc) throws GameActionException {
}

/**
* Run a single turn for an Archon.
* Run a single turn for a Headquarters.
* This code is wrapped inside the infinite loop in run(), so it is called once per turn.
*/
static void runArchon(RobotController rc) throws GameActionException {
static void runHeadquarters(RobotController rc) throws GameActionException {
// Pick a direction to build in.
Direction dir = directions[rng.nextInt(directions.length)];
MapLocation newLoc = rc.getLocation().add(dir);
if (rc.canBuildAnchor(Anchor.STANDARD)) {
// If we can build an anchor do it!
rc.buildAnchor(Anchor.STANDARD);
rc.setIndicatorString("Building anchor! " + rc.getAnchor());
}
if (rng.nextBoolean()) {
// Let's try to build a miner.
rc.setIndicatorString("Trying to build a miner");
if (rc.canBuildRobot(RobotType.MINER, dir)) {
rc.buildRobot(RobotType.MINER, dir);
// Let's try to build a carrier.
rc.setIndicatorString("Trying to build a carrier");
if (rc.canBuildRobot(RobotType.CARRIER, newLoc)) {
rc.buildRobot(RobotType.CARRIER, newLoc);
}
} else {
// Let's try to build a soldier.
rc.setIndicatorString("Trying to build a soldier");
if (rc.canBuildRobot(RobotType.SOLDIER, dir)) {
rc.buildRobot(RobotType.SOLDIER, dir);
// Let's try to build a launcher.
rc.setIndicatorString("Trying to build a launcher");
if (rc.canBuildRobot(RobotType.LAUNCHER, newLoc)) {
rc.buildRobot(RobotType.LAUNCHER, newLoc);
}
}
}

/**
* Run a single turn for a Miner.
* Run a single turn for a Carrier.
* This code is wrapped inside the infinite loop in run(), so it is called once per turn.
*/
static void runMiner(RobotController rc) throws GameActionException {
// Try to mine on squares around us.
static void runCarrier(RobotController rc) throws GameActionException {
if (rc.getAnchor() != null) {
// If I have an anchor singularly focus on getting it to the first island I see
int[] islands = rc.senseNearbyIslands();
Set<MapLocation> islandLocs = new HashSet<>();
for (int id : islands) {
MapLocation[] thisIslandLocs = rc.senseNearbyIslandLocations(id);
islandLocs.addAll(Arrays.asList(thisIslandLocs));
}
if (islandLocs.size() > 0) {
MapLocation islandLocation = islandLocs.iterator().next();
rc.setIndicatorString("Moving my anchor towards " + islandLocation);
while (!rc.getLocation().equals(islandLocation)) {
Direction dir = rc.getLocation().directionTo(islandLocation);
if (rc.canMove(dir)) {
rc.move(dir);
}
}
if (rc.canPlaceAnchor()) {
rc.setIndicatorString("Huzzah, placed anchor!");
rc.placeAnchor();
}
}
}
// Try to gather from squares around us.
MapLocation me = rc.getLocation();
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
MapLocation mineLocation = new MapLocation(me.x + dx, me.y + dy);
// Notice that the Miner's action cooldown is very low.
// You can mine multiple times per turn!
while (rc.canMineGold(mineLocation)) {
rc.mineGold(mineLocation);
MapLocation wellLocation = new MapLocation(me.x + dx, me.y + dy);
if (rc.canCollectResource(wellLocation, -1)) {
if (rng.nextBoolean()) {
rc.collectResource(wellLocation, -1);
rc.setIndicatorString("Collecting, now have, AD:" +
rc.getResourceAmount(ResourceType.ADAMANTIUM) +
" MN: " + rc.getResourceAmount(ResourceType.MANA) +
" EX: " + rc.getResourceAmount(ResourceType.ELIXIR));
}
}
while (rc.canMineLead(mineLocation)) {
rc.mineLead(mineLocation);
}
}
// Occasionally try out the carriers attack
if (rng.nextInt(20) == 1) {
RobotInfo[] enemyRobots = rc.senseNearbyRobots(-1, rc.getTeam().opponent());
if (enemyRobots.length > 0) {
if (rc.canAttack(enemyRobots[0].location)) {
rc.attack(enemyRobots[0].location);
}
}
}


// If we can see a well, move towards it
WellInfo[] wells = rc.senseNearbyWells();
if (wells.length > 1 && rng.nextInt(3) == 1) {
WellInfo well_one = wells[1];
Direction dir = me.directionTo(well_one.getMapLocation());
if (rc.canMove(dir))
rc.move(dir);
}
// Also try to move randomly.
Direction dir = directions[rng.nextInt(directions.length)];
if (rc.canMove(dir)) {
rc.move(dir);
System.out.println("I moved!");
}
}

/**
* Run a single turn for a Soldier.
* Run a single turn for a Launcher.
* This code is wrapped inside the infinite loop in run(), so it is called once per turn.
*/
static void runSoldier(RobotController rc) throws GameActionException {
static void runLauncher(RobotController rc) throws GameActionException {
// Try to attack someone
int radius = rc.getType().actionRadiusSquared;
Team opponent = rc.getTeam().opponent();
RobotInfo[] enemies = rc.senseNearbyRobots(radius, opponent);
if (enemies.length > 0) {
MapLocation toAttack = enemies[0].location;
if (enemies.length >= 0) {
// MapLocation toAttack = enemies[0].location;
MapLocation toAttack = rc.getLocation().add(Direction.EAST);

if (rc.canAttack(toAttack)) {
rc.setIndicatorString("Attacking");
rc.attack(toAttack);
}
}
Expand All @@ -172,7 +226,6 @@ static void runSoldier(RobotController rc) throws GameActionException {
Direction dir = directions[rng.nextInt(directions.length)];
if (rc.canMove(dir)) {
rc.move(dir);
System.out.println("I moved!");
}
}
}

0 comments on commit 10d3051

Please sign in to comment.