Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 59 additions & 36 deletions RobotPlayer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package zombomb; //So the program knows what to call our bot

import battlecode.common.*; //imports Battlecode UI

import java.util.Collection;//Used for a collection of things
import java.util.Random; //Use this instead of Math.random(); seeded by the robot's id so more likely to be random than Math.random

public class RobotPlayer{
Expand All @@ -11,13 +13,18 @@ public class RobotPlayer{
* $randall: our source of all randomness
* $ourTeam: our Team, to save bytecodes
* $opponentTeam: Opponent's (NOT ZOMBIES) team
*
* $zombieDenLocations: locations of the zombie dens
* $enemyArchonIDs: the IDs of enemy archons
*
*/
static RobotController rc;
static Random randall;
static Team ourTeam;
static Team opponentTeam;
static int[] zombieRounds;
static MapLocation[] zombieDenLocations;
static Collection<Integer> enemyArchonIDs;

/**
* run
Expand Down Expand Up @@ -51,18 +58,18 @@ else if(selftype == RobotType.GUARD) {
s.run();
}
}

/** class Guard
*
* The class outlining our Guard bots
*
*
*/
private class Guard {

public Guard() {
}

public void run() {
while(true){
try{
Expand Down Expand Up @@ -195,7 +202,7 @@ public void run(){
last = temp;
}
}
//Otherwise, moves back to where it last was to try to regain them
//Otherwise, moves back to where it last was to try to regain them
}else if(last != null){
if(rc.isCoreReady()){
if(rc.canMove(rc.getLocation().directionTo(last))){
Expand Down Expand Up @@ -312,7 +319,7 @@ public static boolean tryBuild(RobotType rt) throws GameActionException{
}
public static boolean tryBuild(RobotType rt,Direction startDirection) throws GameActionException{
for(int i = 0; i < 8; i++){
if(rc.canBuild(startDirection,rt)){
if(rc.isCoreReady() && rc.canBuild(startDirection,rt)){
rc.build(startDirection,rt);
//System.out.println("BUILT!");
return true;
Expand All @@ -322,7 +329,7 @@ public static boolean tryBuild(RobotType rt,Direction startDirection) throws Gam
}
return false;
}

/**
* boolean tryAttackLocation
*
Expand All @@ -333,13 +340,13 @@ public static boolean tryBuild(RobotType rt,Direction startDirection) throws Gam
*
*/
public static boolean tryAttackLocation(MapLocation loc) throws GameActionException {
boolean canAttack = rc.canAttackLocation(loc);
if (canAttack) {
if (rc.isWeaponReady() && rc.canAttackLocation(loc)) {
rc.attackLocation(loc);
return true;
}
return canAttack;
return false;
}

/**
* MapLocation findLargestPileOfParts
*
Expand All @@ -352,7 +359,7 @@ public static Tuple<MapLocation, Double> findLargestPileOfParts() {
MapLocation myLocation = rc.getLocation();
int sensingRadiusSquared = rc.getType().sensorRadiusSquared;
MapLocation[] visibleLocations = MapLocation.getAllMapLocationsWithinRadiusSq(myLocation, sensingRadiusSquared);

// find the largest pile of parts
double maxPileSize = 0;
MapLocation maxPileLocation = myLocation;
Expand All @@ -363,10 +370,10 @@ public static Tuple<MapLocation, Double> findLargestPileOfParts() {
maxPileLocation = loc;
}
}

// create Tuple
Tuple<MapLocation, Double> locationAndSize = new Tuple<MapLocation, Double>(maxPileLocation, maxPileSize);

return locationAndSize;
}

Expand Down Expand Up @@ -417,6 +424,7 @@ public static boolean moveAsFarAwayAsPossibleFrom(MapLocation epicenter) throws
rc.setIndicatorString(1,"max:none");
return false;
}

/**
* boolean trySendMessage
*
Expand Down Expand Up @@ -447,7 +455,7 @@ public static boolean trySendMessage(Tuple<Integer,Integer> information,int type
rc.broadcastMessageSignal(first,second,radiusSqr);
return true;
}

/**
* RobotType chooseRobotType
* @param none
Expand All @@ -463,46 +471,61 @@ public static RobotType chooseRobotType() {
if(Math.random()*3>1) {
return RobotType.SCOUT;
}
if(numberOfRobotsInRadius(RobotType.GUARD,3,ourTeam) == 7){
if(numberOfRobotsInRadiusAndThoseRobots(RobotType.GUARD,3,ourTeam).first == 7){
return RobotType.SCOUT;
}
return RobotType.GUARD;
}

/**
* Returns the number of robots within a given radius squared
* @param type the type of robot to look for
* @param radiusSqr the squared radius
* @param team the team the robot should be on
* @return the number of robots nearby
* @return a tuple containing the number of robots nearby and the array of all robots nearby
*/
public static int numberOfRobotsInRadius(RobotType type,int radiusSqr,Team team){
public static Tuple<Integer, RobotInfo[]> numberOfRobotsInRadiusAndThoseRobots(RobotType type,int radiusSqr,Team team){
int count = 0;
RobotInfo[] robats = rc.senseNearbyRobots(radiusSqr,team);
if(type == null){
return robats.length;
Tuple<Integer, RobotInfo[]> thingToReturn = new Tuple(robats.length, robats);
return thingToReturn;
}
for(int i = 0; i < robats.length; i++){
if(robats[i].type.equals(type)){
count++;
}
}
return count;
Tuple<Integer, RobotInfo[]> returnThing = new Tuple(count, robats);
return returnThing;
}

/**
* Collects the ID of enemy archons within sight range
* adds the IDs to the static collection enemyArchonIDs
*/
public static void collectEnemyArchonID() {
Tuple<Integer, RobotInfo[]> robs = numberOfRobotsInRadiusAndThoseRobots(RobotType.ARCHON, rc.getType().sensorRadiusSquared, opponentTeam);
if (robs.first > 0) {
for (RobotInfo rob : robs.second) {
enemyArchonIDs.add(rob.ID);
}
}
}
}

/**
* Tuple
*
* a simple tuple class so that tuples can be used.
*/
public static class Tuple<X, Y> {
public X first;
public Y second;
public Tuple(X first, Y second) {
this.first = first;
this.second = second;
}
public X first;
public Y second;
public Tuple(X first, Y second) {
this.first = first;
this.second = second;
}
}

/**
Expand Down Expand Up @@ -535,15 +558,15 @@ public static Tuple<Integer,boolean[]> decrypt(Tuple<Integer,Integer> inputs){
encryptor |= keyIn & 0b1111;
}
int first = (inputs.first ^ encryptor) >> 8;
int second = (inputs.second ^ encryptor);
boolean[] bit = new boolean[56];
for(int i = 0; i < 24; i++){
bit[i] = (first & (1 << i)) != 0;
}
for(int i = 0; i < 32; i++){
bit[i + 24] = (second & (1 << i)) != 0;
}
return new Tuple<Integer,boolean[]>(typeIn,bit);
int second = (inputs.second ^ encryptor);
boolean[] bit = new boolean[56];
for(int i = 0; i < 24; i++){
bit[i] = (first & (1 << i)) != 0;
}
for(int i = 0; i < 32; i++){
bit[i + 24] = (second & (1 << i)) != 0;
}
return new Tuple<Integer,boolean[]>(typeIn,bit);
}
}
}