package mitzi;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class BasicMoveComparator implements Comparator<IMove> {

	/**
	 * saves the actual board, where the moves should be compared
	 */
	private IPosition board;
	
	/**
	 * map, which maps a move to its value. Initial size set to 35 to prevent 
	 */
	private Map<IMove, Integer> move_values = new HashMap<IMove, Integer>(35,1);

	/**
	 * contains values for move comparison (for source piece)
	 */
	private static final Map<Piece, Integer> src_values = new HashMap<Piece, Integer>(6,1);
	static {
		// initialize src_values
		src_values.put(Piece.QUEEN, 5);
		src_values.put(Piece.ROOK, 4);
		src_values.put(Piece.BISHOP, 3);
		src_values.put(Piece.KNIGHT, 2);
		src_values.put(Piece.PAWN, 1);
		src_values.put(Piece.KING, 0);
	}

	/**
	 * contains values for move comparison (for dest piece)
	 */
	private static final Map<Piece, Integer> dest_values = new HashMap<Piece, Integer>(6,1);
	static {
		// initialize dest_values
		dest_values.put(Piece.QUEEN, 500);
		dest_values.put(Piece.ROOK, 400);
		dest_values.put(Piece.BISHOP, 300);
		dest_values.put(Piece.KNIGHT, 200);
		dest_values.put(Piece.PAWN, 100);
		dest_values.put(null, 0);
	}

	public BasicMoveComparator(IPosition board) {
		this.board = board;
	}

	/**
	 * Grades an IMove by the following system: Capturing counts most, then
	 * moving a piece.
	 * 
	 * Among all captures, capturing a more valuable piece counts more. Among
	 * moves of pieces, moving a more valuable piece counts more.
	 * 
	 * Ignoring special situations like en passant and castling.
	 * 
	 * @param move
	 */
	private void computeValue(IMove move) {
		int value = 0;

		// moved figure
		Piece src_piece = board.getPieceFromBoard(move.getFromSquare());
		value += src_values.get(src_piece);

		// captured figure
		Piece dest_piece = board.getPieceFromBoard(move.getToSquare());
		value += dest_values.get(dest_piece);

		move_values.put(move, value);
	}

	/**
	 * compares two moves by there value.
	 */
	@Override
	public int compare(IMove m1, IMove m2) {
		if (!move_values.containsKey(m1))
			computeValue(m1);
		if (!move_values.containsKey(m2))
			computeValue(m2);

		return Integer.compare(move_values.get(m1), move_values.get(m2));
	}

}

@@ -251,17 +251,16 @@
  board;%0A
-%09
 %0A%09/**%0A%09 
@@ -331,17 +331,16 @@
  prevent
- 
 %0A%09 */%0A%09p
@@ -411,16 +411,17 @@
 ger%3E(35,
+ 
 1);%0A%0A%09/*
@@ -465,27 +465,8 @@
 ison
- (for source piece)
 %0A%09 *
@@ -493,729 +493,62 @@
 nal 
-Map%3CPiece, Integer%3E src_values = new HashMap%3CPiece, Integer%3E(6,1);%0A%09static %7B%0A%09%09// initialize src_values%0A%09%09src_values.put(Piece.QUEEN, 5);%0A%09%09src_values.put(Piece.ROOK, 4);%0A%09%09src_values.put(Piece.BISHOP, 3);%0A%09%09src_values.put(Piece.KNIGHT, 2);%0A%09%09src_values.put(Piece.PAWN, 1);%0A%09%09src_values.put(Piece.KING, 0);%0A%09%7D%0A%0A%09/**%0A%09 * contains values for move comparison (for dest piece)%0A%09 */%0A%09private static final Map%3CPiece, Integer%3E dest_values = new HashMap%3CPiece, Integer%3E(6,1);%0A%09static %7B%0A%09%09// initialize dest_values%0A%09%09dest_values.put(Piece.QUEEN, 500);%0A%09%09dest_values.put(Piece.ROOK, 400);%0A%09%09dest_values.put(Piece.BISHOP, 300);%0A%09%09dest_values.put(Piece.KNIGHT, 200);%0A%09%09dest_values.put(Piece.PAWN, 100);%0A%09%09dest_values.put(null, 0);%0A%09%7D
+int%5B%5D piece_values = %7B 100, 500, 325, 325, 975, 000 %7D;
 %0A%0A%09p
@@ -650,219 +650,23 @@
  by 
-the following system: Capturing counts most, then%0A%09 * moving a piece.%0A%09 * %0A%09 * Among all captures, capturing a more valuable piece counts more. Among%0A%09 * moves of pieces, moving a more valuable piece counts more
+some heuristics
 .%0A%09 
@@ -903,46 +903,8 @@
 ());
-%0A%09%09value += src_values.get(src_piece);
 %0A%0A%09%09
@@ -992,47 +992,266 @@
 ));%0A
-%09%09value += dest_values.get(dest_piece);
+%0A%09%09if (dest_piece != null) %7B%0A%09%09%09// try to get advantage in exchange%0A%09%09%09value += (piece_values%5Bdest_piece.ordinal()%5D - piece_values%5Bsrc_piece%0A%09%09%09%09%09.ordinal()%5D);%0A%09%09%7D else %7B%0A%09%09%09// move with more powerful pieces%0A%09%09%09value += piece_values%5Bsrc_piece.ordinal()%5D;%0A%09%09%7D
 %0A%0A%09%09

make move comparator more useful in quiescence search