Skip to content

Commit

Permalink
Fix ImmutablePair issue, update leagues, fix pathfinding bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Azkellas committed Mar 26, 2019
1 parent eb82797 commit 2576760
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
3 changes: 1 addition & 2 deletions config/statement_en.html
Expand Up @@ -72,8 +72,7 @@ <h1>
<img src="https://i.imgur.com/DzcGk2J.png" alt="tower-protection" style="margin-left:auto; margin-right:auto; width:350px;" /><br/>
<figcaption>
The tower owned by red protect the nearby cells marked by a grey square. Cells in the diagonal (marked by a black cross) are <strong>not</strong> protected.
Moreover, the blue cell on the right of the tower is not protected as it is not a red cell. Finally, even though the cell on the bottom of the tower is protected, the level <const>2</const> unit
can capture it.
Moreover, the blue cell on the right of the tower is not protected as it is not a red cell.
</figcaption>
</figure>
</li>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/codingame/antiyoy/Constants.java
Expand Up @@ -23,10 +23,11 @@ public final class Constants {

static public final int UNIT_COST[] = {0, 10, 20, 30};
static public final int UNIT_UPKEEP[] = {0, 1, 4, 20};
static public final int MAX_LEVEL = 3;
static public final int CAPTURE_LEVEL = 2;

static public final int MAX_MOVE_LENGTH = 3;
// league constants
static public int MAX_MOVE_LENGTH = 3;
static public int MAX_LEVEL = 3;

public enum ACTIONTYPE {MOVE, BUILD, TRAIN}

Expand Down
23 changes: 15 additions & 8 deletions src/main/java/com/codingame/antiyoy/Pathfinding.java
Expand Up @@ -3,8 +3,9 @@


import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.ImmutablePair;
// import org.apache.commons.lang3.tuple.ImmutablePair;

import javax.naming.LinkLoopException;
import java.util.LinkedList;

import static com.codingame.antiyoy.Constants.*;
Expand Down Expand Up @@ -77,18 +78,23 @@ public Cell getNearestCell(Cell[][] map, Unit unit, Cell target) {
}

// left = depth, right = cellId
LinkedList<Pair<Integer, Integer>> queue = new LinkedList<>();
queue.add(new ImmutablePair<> (0, getId(start)));
// LinkedList<Pair<Integer, Integer>> queue = new LinkedList<>();
LinkedList<Vector2> queue = new LinkedList<>();
// queue.add(new ImmutablePair<> (0, getId(start)));
queue.add(new Vector2(0, getId(start)));
visited[startId] = true;


Cell bestCell = start;
int bestDistance = distances[startId][targetId];

while (!queue.isEmpty()) {
Pair<Integer, Integer> pair = queue.pop();
int depth = pair.getLeft();
int cellId = pair.getRight();
// Pair<Integer, Integer> pair = queue.pop();
// int depth = pair.getLeft();
// int cellId = pair.getRight();
Vector2 pair = queue.pop();
int depth = pair.getX();
int cellId = pair.getY();
Cell cell = getCell(map, cellId);

// new best: free and nearer
Expand All @@ -97,14 +103,15 @@ public Cell getNearestCell(Cell[][] map, Unit unit, Cell target) {
bestCell = cell;
}

if (depth < MAX_MOVE_LENGTH && cell.getOwner() == start.getOwner()) {
if (depth < MAX_MOVE_LENGTH && cell.getOwner() == unit.getOwner()) {
// we can move further
for (int direction : order) {
Cell neighbour = cell.getNeighbour(direction);
if (neighbour != null) {
int neighbourId = getId(neighbour);
visited[neighbourId] = true;
queue.add(new ImmutablePair<>(depth + 1, neighbourId));
// queue.add(new ImmutablePair<>(depth + 1, neighbourId));
queue.add(new Vector2(depth + 1, neighbourId));
}
}
}
Expand Down
35 changes: 18 additions & 17 deletions src/main/java/com/codingame/game/Referee.java
Expand Up @@ -70,20 +70,24 @@ public void init() {
// Get league
switch (this.gameManager.getLeagueLevel()) {
case 1:
this.league = LEAGUE.WOOD3;
// Only T1 units, no building
MAX_MOVE_LENGTH = 1;
MAX_LEVEL = 1;
this.league = LEAGUE.WOOD3;
break;
case 2:
this.league = LEAGUE.WOOD2;
// Now T2/T3 units, kill mechanism
MAX_MOVE_LENGTH = 1;
this.league = LEAGUE.WOOD2;
break;
case 3:
// Now Mines and Towers
MAX_MOVE_LENGTH = 1;
this.league = LEAGUE.WOOD1;
// Now Mines
break;
default:
// Now pathfinding
this.league = LEAGUE.BRONZE;
// Now Towers
}

// Random generation
Expand Down Expand Up @@ -192,15 +196,16 @@ private boolean makeMoveAction(Action action) {
return false;
}


// if (!action.getCell().isNeighbour(unit.getCell())) {
// gameManager.addToGameSummary(player.getNicknameToken() + ": Invalid action (not a neighbour) " + action);
// return false;
//}
Cell nextCell = this.gameState.getNextCell(unit, action.getCell());

if (nextCell.getX() == unit.getCell().getX() && nextCell.getY() == unit.getCell().getY()) {
// no MOVE to do
gameManager.addToGameSummary(player.getNicknameToken() + ": Unit " + unitId + " stayed still (no nearest cell in range) " + action);
return false;
}
this.gameState.moveUnit(unit, nextCell);
this.gameState.computeAllActiveCells();
gameManager.addToGameSummary(player.getNicknameToken() + " moved " + unitId + " to (" + action.getCell().getX() + ", " + action.getCell().getY() + ")");
gameManager.addToGameSummary(player.getNicknameToken() + " moved " + unitId + " to (" + nextCell.getX() + ", " + nextCell.getY() + ")");
return true;
}

Expand Down Expand Up @@ -234,10 +239,6 @@ private boolean makeBuildAction(Action action) {
return false;
}

if (league == LEAGUE.WOOD1 && action.getBuildType() == BUILDING_TYPE.TOWER) {
gameManager.addToGameSummary(player.getNicknameToken() + ": Invalid action (no Tower in this league) " + action);
return false;
}

if (!action.getCell().isFree()) {
gameManager.addToGameSummary(player.getNicknameToken() + ": Invalid action (cell occupied) " + action);
Expand Down Expand Up @@ -266,9 +267,9 @@ private boolean makeAction() {

Player player = gameManager.getPlayer(action.getPlayer());


// Pathfinding only exists in Bronze and higher
if (league != LEAGUE.BRONZE && !action.getCell().isPlayable(player.getIndex())) {
// TRAIN & BUILD can only be done on a playable cell
// MOVE on the contrary can target any cell
if (action.getType() != ACTIONTYPE.MOVE && !action.getCell().isPlayable(player.getIndex())) {
gameManager.addToGameSummary(player.getNicknameToken() + ": Invalid action (cell not playable) " + action);
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/SkeletonMain.java
Expand Up @@ -10,13 +10,13 @@ public static void main(String[] args) {

// Another way to add a player
gameRunner.setLeagueLevel(4);
// gameRunner.addAgent("./src/test/Agent");
gameRunner.addAgent("./src/test/Agent");
// gameRunner.addAgent("./src/test/Agent");

gameRunner.addAgent("python3 ./src/test/Agent2.py");
gameRunner.addAgent("python3 ./src/test/Agent2.py");
// gameRunner.addAgent("python3 ./src/test/Agent2.py");

// gameRunner.setSeed(5685912415515854218L);
gameRunner.setSeed(-7095413993656790854L);
gameRunner.start();
}
}

0 comments on commit 2576760

Please sign in to comment.