Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

healNearbyFriend coded #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions src/examplefuncsplayer/Pathfind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package examplefuncsplayer;

import battlecode.common.Direction;
import battlecode.common.GameActionException;
import battlecode.common.MapLocation;
import battlecode.common.RobotController;

public class Pathfind {

private static Direction dir;

public static void moveTowards(RobotController rc, MapLocation loc, boolean fill) throws GameActionException {
Direction dir = rc.getLocation().directionTo(loc);
if(rc.canMove(dir)) rc.move(dir);
else if(fill & rc.canFill(rc.getLocation().add(dir))) rc.fill(rc.getLocation().add(dir));
else {
Direction randDir = RobotPlayer.directions[RobotPlayer.random.nextInt(8)];
if(rc.canMove(randDir)) rc.move(randDir);
}
}

public static void explore(RobotController rc) throws GameActionException {
if(rc.isMovementReady()) {
MapLocation[] crumbLocs = rc.senseNearbyCrumbs(-1);
if(crumbLocs.length > 0) {
moveTowards(rc, crumbLocs[0], false);
}

if(dir == null || !rc.canMove(dir)) {
dir = RobotPlayer.directions[RobotPlayer.random.nextInt(8)];
}
if(rc.canMove(dir)) rc.move(dir);
}
}
}
19 changes: 18 additions & 1 deletion src/examplefuncsplayer/RobotPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public strictfp class RobotPlayer {
* we get the same sequence of numbers every time this code is run. This is very useful for debugging!
*/
static final Random rng = new Random(6147);

public static Random random = null;
/** Array containing all the possible movement directions. */
static final Direction[] directions = {
Direction.NORTH,
Expand Down Expand Up @@ -71,13 +71,17 @@ public static void run(RobotController rc) throws GameActionException {
try {
// Make sure you spawn your robot in before you attempt to take any actions!
// Robots not spawned in do not have vision of any tiles and cannot perform any actions.
if(random == null) random = new Random(rc.getID());

if (!rc.isSpawned()){
MapLocation[] spawnLocs = rc.getAllySpawnLocations();
// Pick a random spawn location to attempt spawning in.
MapLocation randomLoc = spawnLocs[rng.nextInt(spawnLocs.length)];
if (rc.canSpawn(randomLoc)) rc.spawn(randomLoc);
}
else{
int round = rc.getRoundNum();
if(round < GameConstants.SETUP_ROUNDS) Setup.runSetup(rc);
if (rc.canPickupFlag(rc.getLocation())){
rc.pickupFlag(rc.getLocation());
rc.setIndicatorString("Holding a flag!");
Expand Down Expand Up @@ -108,6 +112,7 @@ else if (rc.canAttack(nextLoc)){
rc.build(TrapType.EXPLOSIVE, prevLoc);
// We can also move our code into different methods or classes to better organize it!
updateEnemyRobots(rc);
healNearbyFriend(rc);
}

} catch (GameActionException e) {
Expand Down Expand Up @@ -151,4 +156,16 @@ public static void updateEnemyRobots(RobotController rc) throws GameActionExcept
}
}
}

public static void healNearbyFriend (RobotController rc) throws GameActionException {
RobotInfo[] nearbyFriends = rc.senseNearbyRobots(2, rc.getTeam());
for (RobotInfo friend : nearbyFriends) {
if (friend.health < 1000 && rc.canHeal(friend.getLocation())) {
rc.heal(friend.getLocation());
System.out.println("Healed a friendly unit!");
rc.setIndicatorString("Healing: " + friend.getLocation());
break; // Heal only one unit per turn
}
}
}
}
53 changes: 53 additions & 0 deletions src/examplefuncsplayer/Setup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package examplefuncsplayer;

import battlecode.common.*;

public class Setup {

private static final int EXPLORE_ROUNDS = 150;

public static void runSetup(RobotController rc) throws GameActionException {

if(rc.getRoundNum() < EXPLORE_ROUNDS) {
//pickup flag if possible, explore randomly
FlagInfo[] flags = rc.senseNearbyFlags(-1);
for(FlagInfo flag : flags) {
MapLocation flagLoc = flag.getLocation();
if(rc.senseMapInfo(flagLoc).isSpawnZone() && rc.canPickupFlag(flagLoc)) {
rc.pickupFlag(flag.getLocation());
}
}
Pathfind.explore(rc);
}
else {
//try to place flag if it is far enough away from other flags
if(rc.senseLegalStartingFlagPlacement(rc.getLocation())) {
if(rc.canDropFlag(rc.getLocation())) rc.dropFlag(rc.getLocation());
}
//move towards flags and place defenses around them
FlagInfo[] flags = rc.senseNearbyFlags(-1);

FlagInfo targetFlag = null;
for(FlagInfo flag : flags) {
if(!flag.isPickedUp()) {
targetFlag = flag;
break;
}
}

if(targetFlag != null) {
Pathfind.moveTowards(rc, targetFlag.getLocation(), false);
if(rc.getLocation().distanceSquaredTo(flags[0].getLocation()) < 9) {
if(rc.canBuild(TrapType.EXPLOSIVE, rc.getLocation())) {
rc.build(TrapType.EXPLOSIVE, rc.getLocation());
}
else {
MapLocation waterLoc = rc.getLocation().add(RobotPlayer.directions[RobotPlayer.random.nextInt(8)]);
if(rc.canDig(waterLoc)) rc.dig(waterLoc);
}
}
}
else Pathfind.explore(rc);
}
}
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
3.0.5