001 package ai;
002 import java.util.*;
003
004 import core.*;
005 import rules.*;
006
007 /**
008 * The PawnWeightedHeuristic computes a bunch of values to consider,
009 * then ignores most of them because they don't actually help.
010 */
011 public class PawnWeightedHeuristic implements Heuristic {
012
013 /**
014 * @see Heuristic
015 */
016 public int heuristic(GameState currentState) {
017 int total = 0;
018 Board board = currentState.getBoard();
019 Color c = currentState.getCurrentColor();
020 Map<Color, Integer> numPawns = new EnumMap<Color, Integer>(Color.class);
021 Map<Color, Integer> totalPawnPush = new EnumMap<Color, Integer>(Color.class);
022 for(Color pc: Color.colors){
023 numPawns.put(pc, 0);
024 totalPawnPush.put(pc, 0);
025 }
026 for(int x = 0; x < 8; x++){
027 for(int y = 0; y < 8; y++){
028 Piece p = board.get(x, y);
029 Color pc = p.getColor();
030 if (p instanceof Pawn){
031 numPawns.put(pc, numPawns.get(pc)+1);
032 totalPawnPush.put(pc, totalPawnPush.get(pc) + y);
033 }
034 }
035 }
036 totalPawnPush.put(Color.WHITE, 7*numPawns.get(Color.WHITE) -
037 totalPawnPush.get(Color.WHITE));
038
039 for(Color pc: Color.colors) {
040 int colorValue = (numPawns.get(pc) * -100 -
041 totalPawnPush.get(pc));
042 total += (c == pc ? 1 : -1) * colorValue;
043 }
044 return total;
045 }
046 }