package mitzi;

import java.util.Locale;

public final class PieceHelper {

	public static final int WHITE = 0;
	public static final int BLACK = 10;

	public static final int PAWN = 1;
	public static final int ROOK = 2;
	public static final int KNIGHT = 3;
	public static final int BISHOP = 4;
	public static final int QUEEN = 5;
	public static final int KING = 6;

	public static final String[] ALGEBRAIC_NAMES = { "P", "R", "N", "B", "Q",
			"K" };

	private PieceHelper() {
	};

	public static int pieceValue(final int pieceType, final int pieceColor) {
		return (pieceType + pieceColor);
	}

	public static int pieceColor(final int pieceValue) {
		return (pieceValue / 10) * 10;
	}

	public static int pieceType(final int pieceValue) {
		return pieceValue % 10;
	}

	public static boolean isWhite(final int pieceValue) {
		return pieceColor(pieceValue) == WHITE;
	}

	public static boolean isBlack(final int pieceValue) {
		return pieceColor(pieceValue) == BLACK;
	}

	public static int pieceOppositeColor(final int pieceValue) {
		return Math.abs(pieceColor(pieceValue) - 10);
	}

	public static String toString(final int pieceValue) {
		return toString(pieceValue, false);
	}

	public static String toString(final int pieceValue,
			final boolean omitPawnLetter) {

		int piece_type = pieceType(pieceValue);
		int piece_color = pieceColor(pieceValue);

		if (omitPawnLetter && piece_type == PAWN) {
			return "";
		} else if (piece_color == BLACK) {
			return ALGEBRAIC_NAMES[piece_type - 1].toLowerCase(Locale.ENGLISH);
		} else {
			return ALGEBRAIC_NAMES[piece_type - 1];
		}
	}

}

@@ -73,298 +73,8 @@
  %7B%0A%0A
-%09public static final int WHITE = 0;%0A%09public static final int BLACK = 10;%0A%0A%09public static final int PAWN = 1;%0A%09public static final int ROOK = 2;%0A%09public static final int KNIGHT = 3;%0A%09public static final int BISHOP = 4;%0A%09public static final int QUEEN = 5;%0A%09public static final int KING = 6;%0A%0A
 %09pub
@@ -183,16 +183,20 @@
 () %7B%0A%09%7D;
+%0A%09%0A%09
 %0A%0A%09publi
@@ -208,41 +208,38 @@
 tic 
-int pieceValue(final int pieceTyp
+String toString(final Side sid
 e, f
@@ -243,30 +243,27 @@
 , final 
-int
+Piece
  piece
-Color
 ) %7B%0A%09%09re
@@ -271,368 +271,193 @@
 urn 
-(pieceType + pieceColor);%0A%09%7D%0A%0A%09public static int pieceColor(final int pieceValue) %7B%0A%09%09return (pieceValue / 10) * 10;%0A%09%7D%0A%0A%09public static int pieceType(final int pieceValue) %7B%0A%09%09return pieceValue %25 10;%0A%09%7D%0A%0A%09public static boolean isWhite(final int pieceValue
+toString(side, piece, false);%0A%09%7D%0A%0A%09public static String toString(final Side side, final Piece piece,%0A%09%09%09final boolean omitPawnLetter
 ) %7B%0A
+%0A
 %09%09
-return pieceColor(pieceValue) == WHITE;%0A%09%7D%0A%0A%09public static boolean isBlack(final int pieceValue
+if (omitPawnLetter && piece == Piece.PAWN
 ) %7B%0A
+%09
 %09%09re
@@ -465,214 +465,119 @@
 urn 
-pieceColor(pieceValue) == BLACK;%0A%09%7D%0A%0A%09public static int pieceOppositeColor(final int pieceValue) %7B%0A%09%09return Math.abs(pieceColor(pieceValue) - 10);%0A%09%7D%0A%0A%09public static String toString(final int pieceValue)
+%22%22;%0A%09%09%7D else if (side == Side.BLACK) %7B%0A%09%09%09return pieceToString(piece).toLowerCase(Locale.ENGLISH);%0A%09%09%7D else
  %7B%0A
+%09
 %09%09re
@@ -573,33 +573,38 @@
 lse %7B%0A%09%09%09return 
-t
+pieceT
 oString(pieceVal
@@ -604,34 +604,27 @@
 iece
-Value, false);
+);%0A%09%09%7D
 %0A%09%7D%0A%0A%09p
-ublic
+rivate
  sta
@@ -626,33 +626,38 @@
 e static String 
-t
+pieceT
 oString(final in
@@ -658,192 +658,112 @@
 nal 
-int pieceValue,%0A%09%09%09final boolean omitPawnLetter
+Piece piece
 ) %7B%0A
-%0A
 %09%09
-int piece_type = pieceType(pieceValue);%0A%09%09int piece_color = pieceColor(pieceValue);%0A%0A%09%09if (omitPawnLetter && piece_type == PAWN) %7B
+switch (piece) %7B%0A%09%09case PAWN:%0A%09%09%09return %22P%22;%0A%09%09case ROOK:%0A%09%09%09return %22R%22;%0A%09%09case KNIGHT:
 %0A%09%09%09
@@ -774,177 +774,102 @@
 rn %22
+N
 %22;%0A%09%09
-%7D else if (piece_color == BLACK) %7B%0A%09%09%09return ALGEBRAIC_NAMES%5Bpiece_type - 1%5D.toLowerCase(Locale.ENGLISH);%0A%09%09%7D else %7B%0A%09%09%09return ALGEBRAIC_NAMES%5Bpiece_type - 1%5D
+case BISHOP:%0A%09%09%09return %22B%22;%0A%09%09case QUEEN:%0A%09%09%09return %22Q%22;%0A%09%09default:%0A%09%09%09return %22K%22
 ;%0A%09%09%7D%0A
+%0A
 %09%7D%0A%0A

enumify PieceHelper