Skip to content

Commit 09b2a83

Browse files
Merge pull request #7 from CoffeeCoder1015/OOPMaxing
Oop maxing - created s2 engine now with balls to the walls OOP integration
2 parents 328d605 + 71fa39b commit 09b2a83

8 files changed

+624
-0
lines changed

java/src/s2/GenericFunc.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package s2;
2+
3+
import battlecode.common.*;
4+
5+
@FunctionalInterface
6+
public interface GenericFunc {
7+
void p() throws GameActionException;
8+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package s2;
2+
3+
import battlecode.common.*;
4+
5+
public interface GenericRobotContoller {
6+
void run() throws GameActionException;
7+
}

java/src/s2/Mopper.java

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package s2;
2+
3+
import battlecode.common.*;
4+
5+
public class Mopper implements GenericRobotContoller {
6+
7+
RobotController rc;
8+
Pathing pathing_engine;
9+
10+
public Mopper(RobotController handler) throws GameActionException{
11+
rc = handler;
12+
pathing_engine = new Pathing(handler);
13+
}
14+
15+
public void run() throws GameActionException{
16+
System.out.println("Starting mopper logic...");
17+
18+
// Define all possible directions
19+
Direction[] directions = Direction.values();
20+
21+
// Initialize variables to track the best direction
22+
Direction bestDirection = null;
23+
int maxEnemiesInDirection = 0;
24+
25+
// Get the mopper's current location
26+
MapLocation curLoc = rc.getLocation();
27+
28+
// Scan for all enemies within the circle of radius 2 * sqrt(2)
29+
RobotInfo[] nearbyEnemies = rc.senseNearbyRobots(curLoc, 8, rc.getTeam().opponent());
30+
31+
// Count enemies in each direction based on their relative position
32+
for (Direction dir : directions) {
33+
int enemyCount = 0;
34+
35+
for (RobotInfo enemy : nearbyEnemies) {
36+
MapLocation enemyLoc = enemy.getLocation();
37+
38+
// Check if the enemy lies in the swing range for the current direction
39+
if (isInSwingRange(curLoc, enemyLoc, dir)) {
40+
enemyCount++;
41+
}
42+
}
43+
44+
System.out.println("Direction: " + dir + ", Enemies: " + enemyCount);
45+
46+
// Update the best direction if this one has more enemies
47+
if (enemyCount > maxEnemiesInDirection) {
48+
maxEnemiesInDirection = enemyCount;
49+
bestDirection = dir;
50+
}
51+
}
52+
53+
// Perform the mop swing in the best direction if enemies are found
54+
if (bestDirection != null && maxEnemiesInDirection > 0) {
55+
56+
rc.mopSwing(bestDirection);
57+
} else {
58+
59+
}
60+
61+
// If no enemies to mop swing, move randomly
62+
pathing_engine.Move();
63+
64+
// Try to paint beneath us as we walk to avoid paint penalties
65+
MapInfo currentTile = rc.senseMapInfo(rc.getLocation());
66+
if (!currentTile.getPaint().isAlly() && rc.canAttack(rc.getLocation())) {
67+
rc.attack(rc.getLocation());
68+
}
69+
}
70+
71+
private boolean isInSwingRange(MapLocation mopperLoc, MapLocation targetLoc, Direction swingDir) {
72+
// Get the relative position of the target
73+
int dx = targetLoc.x - mopperLoc.x;
74+
int dy = targetLoc.y - mopperLoc.y;
75+
76+
// Check based on direction and relative positions
77+
switch (swingDir) {
78+
case NORTH:
79+
return dx >= -1 && dx <= 1 && dy < 0 && dy >= -2;
80+
case SOUTH:
81+
return dx >= -1 && dx <= 1 && dy > 0 && dy <= 2;
82+
case EAST:
83+
return dy >= -1 && dy <= 1 && dx > 0 && dx <= 2;
84+
case WEST:
85+
return dy >= -1 && dy <= 1 && dx < 0 && dx >= -2;
86+
default:
87+
return false;
88+
}
89+
}
90+
}

java/src/s2/Pathing.java

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package s2;
2+
3+
import battlecode.common.*;
4+
import java.util.Random;
5+
6+
public class Pathing {
7+
static final Direction[] directions = {
8+
Direction.NORTH,
9+
Direction.NORTHEAST,
10+
Direction.EAST,
11+
Direction.SOUTHEAST,
12+
Direction.SOUTH,
13+
Direction.SOUTHWEST,
14+
Direction.WEST,
15+
Direction.NORTHWEST,
16+
};
17+
18+
GenericFunc diffuse = Pathing::diffuse1;
19+
20+
static final Random rng = new Random(6147);
21+
22+
static int robot_dir_idx = -1; //also technically velocity
23+
24+
static RobotController rc;
25+
26+
static private int modulo(int x, int y) {
27+
int temp = Math.floorDiv(x,y);
28+
return x-temp*y;
29+
}
30+
31+
public Pathing(RobotController handler) throws GameActionException{
32+
rc = handler;
33+
}
34+
35+
public void Move() throws GameActionException{
36+
diffuse = Pathing::diffuse1;
37+
int bias = rng.nextInt(5);
38+
if (bias == 3) {
39+
diffuse = Pathing::diffuse2;
40+
}
41+
check_dir();
42+
diffuse.p();
43+
}
44+
45+
private void check_dir() {
46+
if (robot_dir_idx == -1) {
47+
robot_dir_idx = rng.nextInt(8);
48+
}
49+
}
50+
51+
private static void diffuse2() throws GameActionException{
52+
MapLocation current_location = rc.getLocation();
53+
boolean CurrIsAlly = rc.senseMapInfo(current_location).getPaint().isAlly();
54+
for (int i = 0; i < 6; i++) {
55+
Direction goal_dir = directions[robot_dir_idx];
56+
if (rc.canMove(goal_dir) ){
57+
boolean NextIsAlly = rc.senseMapInfo(current_location.add(goal_dir)).getPaint().isAlly();
58+
if (!(!CurrIsAlly && NextIsAlly)) {
59+
rc.move(goal_dir);
60+
return;
61+
}
62+
}
63+
int adj1 = modulo(robot_dir_idx + 1, 8);
64+
int adj2 = modulo(robot_dir_idx - 1, 8);
65+
boolean adj1_canMove = rc.canMove(directions[adj1]);
66+
boolean adj2_canMove = rc.canMove(directions[adj2]);
67+
if (robot_dir_idx % 2 == 0 || (!adj1_canMove && !adj2_canMove)) {
68+
robot_dir_idx = modulo(robot_dir_idx + 3 + rng.nextInt(3), 8);
69+
} else {
70+
if (adj1_canMove) {
71+
robot_dir_idx = adj1;
72+
} else { // no need for 2nd check because if both fail then it would have gone into the
73+
// previous if statement
74+
robot_dir_idx = adj2;
75+
}
76+
}
77+
}
78+
// if stuck in hole
79+
for (int i = 0; i < 10; i++) {
80+
robot_dir_idx = rng.nextInt(8);
81+
Direction goal_dir = directions[robot_dir_idx];
82+
if (rc.canMove(goal_dir)) {
83+
rc.move(goal_dir);
84+
}
85+
}
86+
}
87+
88+
private static void diffuse1() throws GameActionException{
89+
MapInfo[] surrounding = rc.senseNearbyMapInfos(2);
90+
int surround_count = 0;
91+
for (MapInfo mapInfo : surrounding) {
92+
if (mapInfo.getPaint().isAlly()) {
93+
surround_count++;
94+
}
95+
}
96+
for (int i = 0; i < 8; i++) {
97+
Direction goal_dir = directions[robot_dir_idx];
98+
if (rc.canMove(goal_dir)) {
99+
rc.move(goal_dir);
100+
break;
101+
}
102+
int adj1 = modulo(robot_dir_idx + 1, 8);
103+
int adj2 = modulo(robot_dir_idx - 1, 8);
104+
boolean adj1_canMove = rc.canMove(directions[adj1]);
105+
boolean adj2_canMove = rc.canMove(directions[adj2]);
106+
if (robot_dir_idx % 2 == 0 || (!adj1_canMove && !adj2_canMove)) {
107+
robot_dir_idx = modulo(robot_dir_idx + 3 + rng.nextInt(3), 8);
108+
} else {
109+
if (adj1_canMove) {
110+
robot_dir_idx = adj1;
111+
} else { // no need for 2nd check because if both fail then it would have gone into the
112+
// previous if statement
113+
robot_dir_idx = adj2;
114+
}
115+
}
116+
boolean next_is_ally = rc.canMove(directions[robot_dir_idx]) && rc.senseMapInfo(rc.getLocation().add(directions[robot_dir_idx])).getPaint().isAlly();
117+
boolean is_surrounded = surround_count > 3;
118+
if (next_is_ally && !is_surrounded) {
119+
int shift = rng.nextInt(7)+1;
120+
int next = modulo( robot_dir_idx + shift ,8);
121+
if (next == robot_dir_idx) {
122+
next++;
123+
}
124+
robot_dir_idx = next;
125+
}
126+
}
127+
}
128+
129+
130+
}

java/src/s2/RobotPlayer.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package s2;
2+
3+
import battlecode.common.*;
4+
public class RobotPlayer {
5+
public static void run(RobotController rc) throws GameActionException {
6+
GenericRobotContoller processor;
7+
switch (rc.getType()) {
8+
case SOLDIER:
9+
processor = new Solider(rc);
10+
break;
11+
case MOPPER:
12+
processor = new Mopper(rc);
13+
break;
14+
case SPLASHER:
15+
processor = new Splasher(rc) ;
16+
break; // Consider upgrading examplefuncsplayer to use splashers!
17+
default:
18+
processor = new Tower(rc);
19+
break;
20+
}
21+
22+
while (true) {
23+
try {
24+
processor.run();
25+
} catch (GameActionException e) {
26+
System.out.println("GameActionException");
27+
e.printStackTrace();
28+
} catch (Exception e) {
29+
System.out.println("Exception");
30+
e.printStackTrace();
31+
32+
33+
} finally {
34+
// Signify we've done everything we want to do, thereby ending our turn.
35+
// This will make our code wait until the next turn, and then perform this loop
36+
// again.
37+
Clock.yield();
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)