From 6990a5f8c7d361930b465c679bb1524f3b8274d7 Mon Sep 17 00:00:00 2001 From: Prathamesh Chakote Date: Wed, 16 Oct 2024 13:07:21 -0700 Subject: [PATCH 1/3] healNearbyFriend coded healNearbyFriend --- .idea/.gitignore | 8 ++++++++ .idea/misc.xml | 5 +++++ .idea/vcs.xml | 6 ++++++ src/examplefuncsplayer/RobotPlayer.java | 13 +++++++++++++ version.txt | 2 +- 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..aecc280 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/examplefuncsplayer/RobotPlayer.java b/src/examplefuncsplayer/RobotPlayer.java index da15984..e377fa4 100644 --- a/src/examplefuncsplayer/RobotPlayer.java +++ b/src/examplefuncsplayer/RobotPlayer.java @@ -108,6 +108,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) { @@ -151,4 +152,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 + } + } + } } diff --git a/version.txt b/version.txt index 3eefcb9..7da3c16 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.0 +3.0.5 \ No newline at end of file From f57b2647d0f18986a109f349e1581944cac6ea9b Mon Sep 17 00:00:00 2001 From: Prathamesh Chakote Date: Mon, 21 Oct 2024 11:50:07 -0700 Subject: [PATCH 2/3] Setup the ducks. if round number is less than EXPLORE_ROUNDS and do relocation if needed --- src/Setup.java | 53 +++++++++++++++++++++++++ src/examplefuncsplayer/RobotPlayer.java | 2 + 2 files changed, 55 insertions(+) create mode 100644 src/Setup.java diff --git a/src/Setup.java b/src/Setup.java new file mode 100644 index 0000000..b8844ba --- /dev/null +++ b/src/Setup.java @@ -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); + } + } +} \ No newline at end of file diff --git a/src/examplefuncsplayer/RobotPlayer.java b/src/examplefuncsplayer/RobotPlayer.java index e377fa4..7957db5 100644 --- a/src/examplefuncsplayer/RobotPlayer.java +++ b/src/examplefuncsplayer/RobotPlayer.java @@ -78,6 +78,8 @@ public static void run(RobotController rc) throws GameActionException { 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!"); From b373db330263d296495f456a20cb424bfbde674e Mon Sep 17 00:00:00 2001 From: Prathamesh Chakote Date: Mon, 21 Oct 2024 12:54:27 -0700 Subject: [PATCH 3/3] Added Pathfind logic from lectures --- src/examplefuncsplayer/Pathfind.java | 35 +++++++++++++++++++++++++ src/examplefuncsplayer/RobotPlayer.java | 4 ++- src/{ => examplefuncsplayer}/Setup.java | 0 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/examplefuncsplayer/Pathfind.java rename src/{ => examplefuncsplayer}/Setup.java (100%) diff --git a/src/examplefuncsplayer/Pathfind.java b/src/examplefuncsplayer/Pathfind.java new file mode 100644 index 0000000..33b1438 --- /dev/null +++ b/src/examplefuncsplayer/Pathfind.java @@ -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); + } + } +} \ No newline at end of file diff --git a/src/examplefuncsplayer/RobotPlayer.java b/src/examplefuncsplayer/RobotPlayer.java index 7957db5..b22095c 100644 --- a/src/examplefuncsplayer/RobotPlayer.java +++ b/src/examplefuncsplayer/RobotPlayer.java @@ -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, @@ -71,6 +71,8 @@ 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. diff --git a/src/Setup.java b/src/examplefuncsplayer/Setup.java similarity index 100% rename from src/Setup.java rename to src/examplefuncsplayer/Setup.java