Created
December 7, 2010 20:40
-
-
Save dizzi/732371 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pal; | |
public class Utils { | |
public static long setBit(int pos, long qword){ | |
long tmp = 1; | |
tmp <<= pos; | |
qword|=tmp; | |
return qword; | |
} | |
public static long clearBit(int pos, long qword){ | |
long tmp = 1; | |
tmp <<= pos; | |
qword&=~tmp; | |
return qword; | |
} | |
public static long getBit(int pos, long qword){ | |
return (qword>>pos)&1; | |
} | |
public static int pos2num(String pos){ | |
int a = ((byte)pos.charAt(0))-0x60; | |
int b = ((byte)pos.charAt(1))-0x30; | |
return (a-1+Main.edgeSize*(b-1)); | |
} | |
/**@param pos - already shifted position according to direction | |
* @param direction - 1 left, 2 right, 0 up/down */ | |
public static boolean inBoard(int pos, int direction){ | |
//upper and lower edges | |
if(pos>Main.boardSize||pos<0){ | |
return false; | |
} | |
if(direction!=Main.none){ | |
if(direction==Main.left){ | |
if ((pos+1)%Main.edgeSize==0) | |
return false; | |
else | |
return true; | |
}else if(direction==Main.right){ | |
if (Math.abs(pos-1)%Main.edgeSize==Main.edgeSize-1) | |
return false; | |
else | |
return true; | |
} | |
assert(false); | |
} | |
return true; | |
} | |
/** | |
* xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx | |
* 32 24 16 8 0 | |
* --WP--||--BR--||--WR--||--BK--||--WK--| | |
* | |
*/ | |
public static int getWhiteKing(int status){ | |
return status=0x3f&(status>>0); | |
} | |
public static int getWhiteRook(int status){ | |
return status=0x3f&(status>>6); | |
} | |
public static int getBlackKing(int status){ | |
return status=0x3f&(status>>12); | |
} | |
public static int getBlackRook(int status){ | |
return status=0x3f&(status>>18); | |
} | |
public static int getWhitePawn(int status){ | |
return status=0x3f&(status>>24); | |
} | |
public static int setWhiteKing(int status, int position){ | |
status &= 0xFFFFFFC0; | |
return status=status|(position<<0); | |
} | |
public static int setWhiteRook(int status, int position){ | |
status &= 0xFFFFF03F; | |
return status=status|(position<<6); | |
} | |
public static int setBlackKing(int status, int position){ | |
status &= 0xFFFC0FFF; | |
return status=status|(position<<12); | |
} | |
public static int setBlackRook(int status, int position){ | |
status &= 0xFF03FFFF; | |
return status=status|(position<<18); | |
} | |
public static int setWhitePawn(int status, int position){ | |
status &= 0xC0FFFFFF; | |
return status=status|(position<<24); | |
} | |
public static boolean isChess(int status){ | |
if((status>>31)==0)return false;else return true; | |
} | |
public static int setChess(int status, boolean chess){ | |
if(chess) | |
return status|0x80000000; | |
else | |
return status&0x7fffffff; | |
} | |
public static int getPieceMoveTo(int move){ | |
return move&0x0F; | |
} | |
public static int getPieceMoveFrom(int move){ | |
return (move>>8)&0x0F; | |
} | |
public static int getPieceMoveKind(int move){ | |
return move>>16; | |
} | |
public static int setPieceMoveTo(int move, int to){ | |
move&=0xFFFFFF00; | |
return move|to; | |
} | |
public static int setPieceMoveFrom(int move, int from){ | |
move&=0xFFFF00FF; | |
return (from<<8)|move; | |
} | |
public static int setPieceMoveKind(int move, int kind){ | |
move&=0xFF00FFFF; | |
return (kind<<16)|move; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment