Skip to content

Commit

Permalink
almost final
Browse files Browse the repository at this point in the history
  • Loading branch information
ThiagoRRamos committed Dec 2, 2013
1 parent 1b2170d commit bad7c16
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 58 deletions.
150 changes: 150 additions & 0 deletions my-bot/AttackAnalysis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AttackAnalysis {

private Ants ants;
private int cols;
private int rows;
private int[][] mine;
private Map<Tile, List<Tile>> mineTile;
private int[][] theirs;
private Map<Tile, List<Tile>> theirsTile;
private MyBot bot;

public AttackAnalysis(Ants ants, MyBot myBot) {
this.ants = ants;
this.rows = ants.getRows();
this.cols = ants.getCols();
this.bot = myBot;
mine = new int[rows][cols];
theirs = new int[rows][cols];
mineTile = new HashMap<Tile, List<Tile>>();
theirsTile = new HashMap<Tile, List<Tile>>();
}

public void process() {
for (Tile my : ants.getMyAnts()) {
for (Tile t : tileswithinradius(my)) {
mine[t.getRow()][t.getCol()] += 1;
addTiletoMineMap(t, my);
}
}
for (Tile thei : ants.getEnemyAnts()) {
for (Tile t : tileswithinradius(thei)) {
theirs[t.getRow()][t.getCol()] += 1;
addTiletoTheirsMap(t, thei);
}
}
}

private void addTiletoTheirsMap(Tile t, Tile thei) {
if (!theirsTile.containsKey(t)) {
theirsTile.put(t, new ArrayList<Tile>());
}
theirsTile.get(t).add(thei);
}

private void addTiletoMineMap(Tile t, Tile my) {
if (!mineTile.containsKey(t)) {
mineTile.put(t, new ArrayList<Tile>());
}
mineTile.get(t).add(my);
}

public void decideAttacks() {
for (Tile my : ants.getMyAnts()) {
for (Tile neigh : ants.possibleNextStates(my)) {
if (theirs[neigh.getRow()][neigh.getCol()] > 1) {
int numberEnemies = theirs[neigh.getRow()][neigh.getCol()];
for (Tile enemy : theirsTile.get(neigh)) {
for (Tile enemyneigh : ants.possibleNextStates(enemy)) {
if (mineTile.get(enemyneigh) != null
&& mineTile.get(enemyneigh).contains(my)) {
if (mine[enemyneigh.getRow()][enemyneigh
.getCol()] > numberEnemies) {
} else if (mine[enemyneigh.getRow()][enemyneigh
.getCol()] < numberEnemies) {
fugir(my, enemy);
break;
} else {
}
} else {
}
}
}
}
}
}
}

private void fugir(Tile my, Tile enemy) {
Aim d = getOppositeDirection(ants.getDirections(my, enemy).get(0));
bot.doMoveDirection(my, d);
}

private Aim getOppositeDirection(Aim aim) {
switch (aim) {
case EAST:
return Aim.WEST;
case NORTH:
return Aim.SOUTH;
case SOUTH:
return Aim.NORTH;
case WEST:
return Aim.EAST;
default:
break;
}
throw new RuntimeException("OPPS");
}

public void printMine() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.err.print(mine[i][j]);
}
System.err.println("");
}
}

public void printTheirs() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.err.print(theirs[i][j]);
}
System.err.println("");
}
}

private List<Tile> tileswithinradius(Tile my) {
List<Tile> res = new ArrayList<>();
int mx = (int) Math.sqrt(ants.getAttackRadius2()) + 1;
for (int row = -mx; row <= mx; ++row) {
for (int col = -mx; col <= mx; ++col) {
double d = Math.pow((Math.abs(row) - 1), 2.0)
+ Math.pow((Math.abs(col) - 1), 2.0);
double d2 = row * row + col * col;
if (d <= ants.getAttackRadius2() + 1
&& d2 >= ants.getAttackRadius2()) {
int mrow = my.getRow() + row;
int mcol = my.getCol() + col;
if (mrow < 0) {
mrow += rows;
} else {
mrow = mrow % rows;
}
if (mcol < 0) {
mcol += cols;
} else {
mcol = mcol % cols;
}
res.add(new Tile(mrow, mcol));
}
}
}
return res;
}
}
94 changes: 55 additions & 39 deletions my-bot/DiffusionMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,123 +7,139 @@
import java.util.Comparator;

public class DiffusionMap {

private double[][] values;
private Ants ants;
private int rows;
private int cols;

public DiffusionMap(Ants ants){
public DiffusionMap(Ants ants) {
this.ants = ants;
rows = ants.getRows();
cols = ants.getCols();
values = new double[rows][cols];
init(ants);
}

public void clear(){
public void clear() {
values = new double[rows][cols];
}

public Comparator<Tile> getComparator(){
public Comparator<Tile> getComparator() {
return new TileComparator(this, ants);
}

public double getValue(Tile t){
public double getValue(Tile t) {
int row = t.getRow();
int col = t.getCol();
if(row < 0 || row >= rows){
row = (row+rows)%rows;
if (row < 0 || row >= rows) {
row = (row + rows) % rows;
}
if(col < 0 || col >= cols){
col = (col+cols)%cols;
if (col < 0 || col >= cols) {
col = (col + cols) % cols;
}
return values[row][col];
}

public double getValue(int row, int col){
if(row < 0 || row >= rows){
row = (row+rows)%rows;
public double getValue(int row, int col) {
if (row < 0 || row >= rows) {
row = (row + rows) % rows;
}
if(col < 0 || col >= cols){
col = (col+cols)%cols;
if (col < 0 || col >= cols) {
col = (col + cols) % cols;
}
return values[row][col];
}

public void addValue(Tile t, double val){
public void addValue(Tile t, double val) {
values[t.getRow()][t.getCol()] += val;
}

public boolean addElement(Tile t,double value, double decreaseFactor, double k){
public boolean addElement(Tile t, double value, double decreaseFactor,
double k) {
return addElement(t, value, decreaseFactor, k, true);
}

public boolean addElement(Tile t, double value, double decreaseFactor,
double k, boolean ignoreWater) {
Queue<Tile> tiles = new LinkedList<>();
Queue<Double> vals = new LinkedList<>();
Set<Tile> alreadyUsed = new HashSet<>();
tiles.add(t);
vals.add(value);
while(!tiles.isEmpty()){
while (!tiles.isEmpty()) {
Tile n = tiles.poll();
double val = vals.poll();
alreadyUsed.add(n);
addValue(n, val);
if(val*k > 1){
for(Tile no : ants.possibleNextStates(n)){
if(!alreadyUsed.contains(no) && ants.isPassable(no)){
tiles.add(no);
vals.add(val/decreaseFactor);
if (val * k > 1) {
if (ignoreWater) {
for (Tile no : ants.possibleNextStates(n)) {
if (!alreadyUsed.contains(no) && (ants.isPassable(no))) {
tiles.add(no);
vals.add(val / decreaseFactor);
}
}
}else{
for (Tile no : ants.getNeighbors(n)) {
if (!alreadyUsed.contains(no) && (ants.isPassable(no))) {
tiles.add(no);
vals.add(val / decreaseFactor);
}
}
}
}
}
return true;
}

public void init(Ants ants){
public void init(Ants ants) {

}

public String toString(){
public String toString() {
StringBuffer sb = new StringBuffer();
for(int i=0;i<ants.getRows(); i++){
for(int j=0;j<ants.getCols(); j++){
sb.append(String.format("%2.0f", getValue(i,j)/100.0f)+" ");
for (int i = 0; i < ants.getRows(); i++) {
for (int j = 0; j < ants.getCols(); j++) {
sb.append(String.format("%2.0f", getValue(i, j) / 100.0f) + " ");
}
sb.append("\n");
}
return sb.toString();
}

public void finishRound(){
public void finishRound() {

}

public Aim getBestDirection(Tile t){
public Aim getBestDirection(Tile t) {
int row = t.getRow();
int col = t.getCol();
double bestValue = -Double.MAX_VALUE;
Aim bestDirection = null;
for(Aim a : Aim.values()){
int newR = (row + a.getRowDelta()+rows)%rows;
int newC = (col + a.getColDelta()+cols)%cols;
if(ants.isPassable(new Tile(newR,newC))){
for (Aim a : Aim.values()) {
int newR = (row + a.getRowDelta() + rows) % rows;
int newC = (col + a.getColDelta() + cols) % cols;
if (ants.isPassable(new Tile(newR, newC))) {
double n = values[newR][newC];
if(n>bestValue){
if (n > bestValue) {
bestValue = n;
bestDirection = a;
}
}
}
if(bestValue > values[row][col]){
if (bestValue > values[row][col]) {
return bestDirection;
}
return null;
}

public void print(PrintStream ps, int rowstart, int rowend, int colStart, int colEnd){
for(int r = rowstart; r < rowend; r++){
public void print(PrintStream ps, int rowstart, int rowend, int colStart,
int colEnd) {
for (int r = rowstart; r < rowend; r++) {
StringBuffer sb = new StringBuffer();
for(int c = colStart;c < colEnd; c++){
sb.append(String.format("%2.0f", getValue(r,c))+" ");
for (int c = colStart; c < colEnd; c++) {
sb.append(String.format("%2.0f", getValue(r, c)) + " ");
}
ps.println(sb);
}
Expand Down
Loading

0 comments on commit bad7c16

Please sign in to comment.