Commits

Vincent Fiack committed 1a1b56a

better hungry, no crash if an ant dies

Comments (0)

Files changed (2)

src/main/java/fr/vfiack/ants/AntsBot.java

 package fr.vfiack.ants;
 
 import java.io.FileWriter;
+import java.io.PrintWriter;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
 		this.future = new HashMap<Tile, Ant>(current.size()); 
 		
 		//1. remove dead ants
+		List<Tile> deads = new ArrayList<Tile>();
 		for(Tile tile: current.keySet()) {
 			if(ants.getIlk(tile) != Ilk.MY_ANT)
-				current.remove(tile);
+				deads.add(tile);
+		}
+		for(Tile dead: deads) {
+			current.remove(dead);
 		}
 
 		//2. add new ants
 			return new Hungry();
 		
 		//alternate other strategies
-		if(current.size() %2 == 1)
+		if(current.size() %3 == 1)
 			return new Explorer();
-		return new Random();
+		else if(current.size() %3 == 2)
+			return new Hungry();
+		else
+			return new Random();
 	}
 	
 	@Override
 	public void doTurn() {
-		Ants ants = getAnts();
-
-		log("*** turn: " + turn++ + " (" + ants.getTimeRemaining() + " ms)");
-		updateMyAnts();
-		log("update ants: " + current + " (" + ants.getTimeRemaining() + " ms)");
-				
-		for(Entry<Tile, Ant> entry: current.entrySet()) {
-			Tile tile = entry.getKey();
-			Ant ant = entry.getValue();
-			Aim aim = ant.getNextMove(this, tile);
-			Tile destination = ants.getTile(tile, aim);
-			if(isAvailable(destination)) {
-				future.put(destination, ant);
-				log("issueOrder: " + tile + ", " + aim);
-				ants.issueOrder(tile, aim);
-			} else {
-				future.put(tile, ant);
+		try {
+			
+			Ants ants = getAnts();
+	
+			log("*** turn: " + turn++ + " (" + ants.getTimeRemaining() + " ms)");
+			updateMyAnts();
+			log("update ants: " + current + " (" + ants.getTimeRemaining() + " ms)");
+					
+			for(Entry<Tile, Ant> entry: current.entrySet()) {
+				Tile tile = entry.getKey();
+				Ant ant = entry.getValue();
+				Aim aim = ant.getNextMove(this, tile);
+				Tile destination = ants.getTile(tile, aim);
+				if(isAvailable(destination)) {
+					future.put(destination, ant);
+					log("issueOrder: " + tile + ", " + aim);
+					ants.issueOrder(tile, aim);
+				} else {
+					future.put(tile, ant);
+				}
+	
+				log("moves: " + future + " (" + ants.getTimeRemaining() + " ms)");
+	
 			}
-
-			log("moves: " + future + " (" + ants.getTimeRemaining() + " ms)");
-
-		}		
+		} catch(Throwable t) {
+			log(t);
+		}
 	}
 	
 	private void log(String message) {
 			//rien
 		}
 	}
+	
+	private void log(Throwable t) {
+		try {
+			FileWriter fw = new FileWriter("e:/botlog.txt", true);
+			fw.write("===================\n");
+			t.printStackTrace(new PrintWriter(fw));
+			fw.write("===================\n");
+			fw.close();
+		} catch(Exception e) {
+			//rien
+		}
+	}
 }

src/main/java/fr/vfiack/ants/strategies/Hungry.java

 package fr.vfiack.ants.strategies;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
 import fr.vfiack.ants.AntsBot;
 
 public class Hungry extends Explorer {
-	
+	private Tile target;
+	private List<Tile> trail = new ArrayList<Tile>();
+	private List<Tile> unreachable = new ArrayList<Tile>();
 	
 	public Aim getNextMove(AntsBot bot, Tile current) {
 		Set<Tile> foods = bot.getAnts().getFoodTiles();
 		
 		//search for the closer food tile
 		Tile selected = null;
-		int minDistance = Integer.MAX_VALUE;
+		int selectedDistance = Integer.MAX_VALUE;
 		for(Tile food: foods) {
+			//don't target the unreachable
+			if(unreachable.contains(food))
+				continue;
+			
 			int distance = bot.getAnts().getDistance(current, food);
-			if(distance < minDistance) {
+			if(distance < selectedDistance) {
 				selected = food;
-				minDistance = distance;
+				selectedDistance = distance;
 			}
 		}
 		
-		//food found, try to reach it
-		if(selected != null) {
-			List<Aim> directions = bot.getAnts().getDirections(current, selected);
+		//wow, new food found, different from old target
+		if(selected != null && selected.equals(target)) {
+			//only change if clearly closer
+			if(selectedDistance+2 < bot.getAnts().getDistance(current, target));
+			
+			target = selected;
+			trail.clear();
+		}
+		
+		//food targeted, try to reach it
+		if(target != null) {
+			List<Aim> directions = bot.getAnts().getDirections(current, target);
 			for(Aim aim: directions) {
 				Tile next = bot.getAnts().getTile(current, aim);
-				if(bot.isAvailable(next))
+				if(bot.isAvailable(next)) {
+					if(trail.contains(next)) {
+						unreachable.add(target);
+					}
 					return aim;
+				}
+					
 			}
 		}
 		
 		//no available food, go exploring
 		return super.getNextMove(bot, current);
-
 	}
 }