Skip to content

Commit 4d7caac

Browse files
committed
fix arrayindexoutofboundsexception
1 parent 001a093 commit 4d7caac

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

engine/src/main/battlecode/world/RobotControllerImpl.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,29 +1022,45 @@ public void attack(MapLocation loc, int cheese) throws GameActionException {
10221022

10231023
public void assertCanBecomeRatKing() throws GameActionException {
10241024
assertIsActionReady();
1025-
if (this.gameWorld.getTeamInfo().getCheese(this.robot.getTeam()) < GameConstants.RAT_KING_UPGRADE_CHEESE_COST) {
1025+
TeamInfo teamInfo = this.gameWorld.getTeamInfo();
1026+
1027+
if (teamInfo.getCheese(this.robot.getTeam()) < GameConstants.RAT_KING_UPGRADE_CHEESE_COST) {
10261028
throw new GameActionException(CANT_DO_THAT, "Not enough cheese to upgrade to a rat king");
10271029
}
1028-
if (this.gameWorld.getTeamInfo().getNumRatKings(this.robot.getTeam()) >= GameConstants.MAX_NUMBER_OF_RAT_KINGS){
1030+
1031+
if (teamInfo.getNumRatKings(this.robot.getTeam()) >= GameConstants.MAX_NUMBER_OF_RAT_KINGS){
10291032
throw new GameActionException(CANT_DO_THAT, "Cannot have more than " +GameConstants.MAX_NUMBER_OF_RAT_KINGS + "rat kings per team!" );
10301033
}
1034+
10311035
int numAllyRats = 0;
1036+
10321037
for (Direction d : Direction.allDirections()) {
10331038
MapLocation curLoc = this.adjacentLocation(d);
1039+
1040+
if (!onTheMap(curLoc)) {
1041+
throw new GameActionException(CANT_DO_THAT,
1042+
"Can't become a rat king when the 3x3 vicinity goes off the map!");
1043+
}
1044+
10341045
InternalRobot curRobot = this.gameWorld.getRobot(curLoc);
1046+
10351047
if (curRobot != null && curRobot.getTeam() == this.robot.getTeam() && curRobot.getType() == UnitType.BABY_RAT) {
10361048
numAllyRats += 1;
10371049
}
1050+
10381051
if (curRobot != null && !curRobot.getType().isBabyRatType()) {
10391052
throw new GameActionException(CANT_DO_THAT,
10401053
"Can't become a rat king when there are nearby cats or rat kings!");
10411054
}
1055+
10421056
MapInfo mapInfo = this.getMapInfo(curLoc);
1057+
10431058
if (!mapInfo.isPassable()) {
10441059
throw new GameActionException(CANT_DO_THAT,
10451060
"Can only upgrade if all squares in the 3x3 vicinity are passable");
10461061
}
10471062
}
1063+
10481064
if (numAllyRats < 7) {
10491065
throw new GameActionException(CANT_DO_THAT, "Not enough rats in the 3x3 square");
10501066
}

0 commit comments

Comments
 (0)