Skip to content

Instantly share code, notes, and snippets.

@dizzi
Created January 24, 2011 17:17
Show Gist options
  • Save dizzi/793549 to your computer and use it in GitHub Desktop.
Save dizzi/793549 to your computer and use it in GitHub Desktop.
package pal;
public class MapGenerator {
public static long[] king = new long[50];
public static long[] rook = new long[50];
public static long[] pawn = new long[50];
public static long[] queen = new long[50];
public static long[] kingMap = {
0x182L, 0x385L, 0x70aL, 0xe14L, 0x1c28L, 0x3850L, 0x3020L, 0xc103L, 0x1c287L, 0x3850eL,
0x70a1cL, 0xe1438L, 0x1c2870L, 0x181060L, 0x608180L, 0xe14380L, 0x1c28700L, 0x3850e00L, 0x70a1c00L, 0xe143800L,
0xc083000L, 0x3040c000L, 0x70a1c000L, 0xe1438000L, 0x1c2870000L, 0x3850e0000L, 0x70a1c0000L, 0x604180000L, 0x1820600000L, 0x3850e00000L,
0x70a1c00000L, 0xe143800000L, 0x1c287000000L, 0x3850e000000L, 0x3020c000000L, 0xc1030000000L, 0x1c2870000000L, 0x3850e0000000L, 0x70a1c0000000L, 0xe14380000000L,
0x1c28700000000L, 0x1810600000000L, 0x2081800000000L, 0x2143800000000L, 0x287000000000L, 0x50e000000000L, 0xa1c000000000L, 0x1438000000000L, 0x830000000000L};
public static long[] rookMap = {
0x408102040feL, 0x8102040817dL, 0x10204081027bL, 0x204081020477L, 0x40810204086fL, 0x81020408105fL, 0x102040810203fL, 0x40810207f01L, 0x8102040be82L, 0x102040813d84L,
0x204081023b88L, 0x408102043790L, 0x810204082fa0L, 0x1020408101fc0L, 0x408103f8081L, 0x810205f4102L, 0x1020409ec204L, 0x2040811dc408L, 0x4081021bc810L, 0x81020417d020L,
0x10204080fe040L, 0x4081fc04081L, 0x8102fa08102L, 0x10204f610204L, 0x20408ee20408L, 0x40810de40810L, 0x81020be81020L, 0x1020407f02040L, 0x40fe0204081L, 0x817d0408102L,
0x1027b0810204L, 0x204771020408L, 0x4086f2040810L, 0x8105f4081020L, 0x10203f8102040L, 0x7f010204081L, 0xbe820408102L, 0x13d840810204L, 0x23b881020408L, 0x437902040810L,
0x82fa04081020L, 0x101fc08102040L, 0x1f80810204081L, 0x1f41020408102L, 0x1ec2040810204L, 0x1dc4081020408L, 0x1bc8102040810L, 0x17d0204081020L, 0xfe0408102040L};
public static long[] pawnMap = {
0x0L, 0x0L, 0x0L, 0x0L, 0x0L, 0x0L, 0x0L, 0x204000L, 0x408000L, 0x810000L,
0x1020000L, 0x2040000L, 0x4080000L, 0x8100000L, 0x200000L, 0x400000L, 0x800000L, 0x1000000L, 0x2000000L, 0x4000000L,
0x8000000L, 0x10000000L, 0x20000000L, 0x40000000L, 0x80000000L, 0x100000000L, 0x200000000L, 0x400000000L, 0x800000000L, 0x1000000000L,
0x2000000000L, 0x4000000000L, 0x8000000000L, 0x10000000000L, 0x20000000000L, 0x40000000000L, 0x80000000000L, 0x100000000000L, 0x200000000000L, 0x400000000000L,
0x800000000000L, 0x1000000000000L, 0x2000000000000L, 0x0L, 0x0L, 0x0L, 0x0L, 0x0L, 0x0L};
public static long[] queenMap = {
0x10509112141feL, 0xa12224283fdL, 0x10244485477bL, 0x2040892a8e77L, 0x408112551c6fL, 0x810a248a385fL, 0x106144914303fL, 0x848890a0ff01L, 0x109112141fe87L, 0x122242a3bd8eL,
0x204495473b9cL, 0x40892a8e37b8L, 0x8512451c2ff0L, 0x10a248a181fe0L, 0x4448507f8081L, 0x8890a0ff4382L, 0x1112151dec715L, 0x224aa39dce2aL, 0x4495471bdc54L, 0x89228e17f8a8L,
0x112450c0ff150L, 0x24283fc04081L, 0x48507fa1c102L, 0x90a8ef638a84L, 0x12551cee71549L, 0x4aa38dee2a92L, 0x91470bfc5524L, 0x1228607f8aa48L, 0x141fe0204081L, 0x283fd0e08102L,
0x5477b1c54204L, 0xa8e7738aa488L, 0x151c6f7154911L, 0xa385fe2a9222L, 0x14303fc552444L, 0xff010204081L, 0x1fe870408102L, 0x3bd8e2a10204L, 0x73b9c5524408L, 0xe37b8aa48890L,
0x1c2ff15491521L, 0x181fe2a922a42L, 0x1f80810204081L, 0x1f43820408102L, 0x1ec7150810204L, 0x1dce2a9220408L, 0x1bdc552444810L, 0x17f8aa48a90a0L, 0xff1549153141L};
public static long[] pawnAttackMap = {
0x0L, 0x0L, 0x0L, 0x0L, 0x0L, 0x0L, 0x0L, 0x8000L, 0x14000L, 0x28000L,
0x50000L, 0xa0000L, 0x140000L, 0x80000L, 0x400000L, 0xa00000L, 0x1400000L, 0x2800000L, 0x5000000L, 0xa000000L,
0x4000000L, 0x20000000L, 0x50000000L, 0xa0000000L, 0x140000000L, 0x280000000L, 0x500000000L, 0x200000000L, 0x1000000000L, 0x2800000000L,
0x5000000000L, 0xa000000000L, 0x14000000000L, 0x28000000000L, 0x10000000000L, 0x80000000000L, 0x140000000000L, 0x280000000000L, 0x500000000000L, 0xa00000000000L,
0x1400000000000L, 0x800000000000L, 0x0L, 0x2000000000000L, 0x0L, 0x0L, 0x0L, 0x0L, 0x0L};
public static void printMap(long map) {
long mask = 0x0000000000001L;
for (int i = 0; i < Main.boardSize; i++) {
if ((map & mask) == 0)
System.out.print("0 ");
else
System.out.print("1 ");
if (i % Main.edgeSize == Main.edgeSize - 1)
System.out.println();
mask <<= 1;
}
}
public static long generateKingMap(int pos) {
if(pos<0)return 0;
if(true)
return MapGenerator.kingMap[pos];
long board = 0;
if (king[pos] == 0) {
if (Utils.inBoard(pos - 1, Main.left)) // left
board = Utils.setBit(pos - 1, board);
if (Utils.inBoard(pos + 1, Main.right)) // Main.right
board = Utils.setBit(pos + 1, board);
if (Utils.inBoard(pos + Main.edgeSize, Main.none)) // up
board = Utils.setBit(pos + Main.edgeSize, board);
if (Utils.inBoard(pos - Main.edgeSize, Main.none)) // down
board = Utils.setBit(pos - Main.edgeSize, board);
if (Utils.inBoard(pos + Main.edgeSize - 1, Main.left)) // up-left
board = Utils.setBit(pos + Main.edgeSize - 1, board);
if (Utils.inBoard(pos - Main.edgeSize - 1, Main.left)) // down-left
board = Utils.setBit(pos - Main.edgeSize - 1, board);
if (Utils.inBoard(pos + Main.edgeSize + 1, Main.right)) // up-right
board = Utils.setBit(pos + Main.edgeSize + 1, board);
int b = pos - Main.edgeSize < 0 ? -1 : 1;// hack na spatne znamenko
// pri
// prechode pres 0
if (Utils.inBoard(pos - Main.edgeSize + b, Main.right)) // down-right
board = Utils.setBit(pos - Main.edgeSize + b, board);
king[pos] = board;
return board;
} else {
return king[pos];
}
}
private static int getPosition(int col, int row) {
return row * Main.edgeSize + col;
}
public static long generatePawnAttackMap(int pos) {
if(pos<0)return 0;
if(true)
return MapGenerator.pawnAttackMap[pos];
long board = 0;
if (pos < Main.edgeSize) // not possible
return board;
if (Utils.inBoard(pos + Main.edgeSize + 1, Main.right)) // up-left
board = Utils.setBit(pos + Main.edgeSize + 1, board);
if (Utils.inBoard(pos + Main.edgeSize - 1, Main.left)) // up-left
board = Utils.setBit(pos + Main.edgeSize - 1, board);
return board;
}
public static long generateRiderMap(int pos) {
if (pos < 0)
return 0;
long board = 0;
if ((pos % Main.edgeSize) + 2 < Main.edgeSize) {
board = Utils.setBit(pos + 2 + Main.edgeSize, board);
board = Utils.setBit(pos + 2 - Main.edgeSize, board);
}
if ((pos % Main.edgeSize) - 2 > 0 ) {
board = Utils.setBit(pos - 2 + Main.edgeSize, board);
board = Utils.setBit(pos - 2 - Main.edgeSize, board);
}
if ((pos % Main.edgeSize) + 1 < Main.edgeSize) {
board = Utils.setBit(pos + 1 + 2 * Main.edgeSize, board);
board = Utils.setBit(pos + 1 - 2 * Main.edgeSize, board);
}
if ((pos % Main.edgeSize) - 1 > 0) {
board = Utils.setBit(pos - 1 + 2 * Main.edgeSize, board);
board = Utils.setBit(pos - 1 - 2 * Main.edgeSize, board);
}
return board;
}
public static long generateRookMap(int pos) {
if(pos<0)return 0;
if (pos > Main.boardSize)
return 0;
if(true)
return MapGenerator.rookMap[pos];
long board = 0;
if (rook[pos] == 0) {
int col = pos % Main.edgeSize;
int row = pos / Main.edgeSize;
// System.out.println("pos:"+pos+" col:"+col+" row:"+row);
for (int i = 0; i < Main.edgeSize; i++) {
board = Utils.setBit(col + i * Main.edgeSize, board);
board = Utils.setBit(i + row * Main.edgeSize, board);
}
board = Utils.clearBit(pos, board);
MapGenerator.rook[pos] = board;
return board;
} else {
return rook[pos];
}
}
public static long generatePawnQueenMap(int pos, boolean pawn) {
if(pos<0)return 0;
if (pos > Main.boardSize)
return 0;
if(true){
if(pawn)
return MapGenerator.pawnMap[pos];
else
return MapGenerator.queenMap[pos];
}
long board = 0;
if (pawn) {
if (MapGenerator.pawn[pos] == 0) {
if (pos < Main.edgeSize) // not possible
return board;
if (pos >= Main.edgeSize && pos < 2 * Main.edgeSize) { // starting
// position
board = Utils.setBit(pos + Main.edgeSize, board);
board = Utils.setBit(pos + 2 * Main.edgeSize, board);
} else {
if (Utils.inBoard(pos + Main.edgeSize, Main.none))
board = Utils.setBit(pos + Main.edgeSize, board);
}
MapGenerator.pawn[pos] = board;
return board;
} else {
return MapGenerator.pawn[pos];
}
} else { // queen map
if (MapGenerator.queen[pos] == 0) {
// exactly same as for rook
int col = pos % Main.edgeSize;
int row = pos / Main.edgeSize;
// System.out.println("pos:"+pos+" col:"+col+" row:"+row);
for (int i = 0; i < Main.edgeSize; i++) {
board = Utils.setBit(col + i * Main.edgeSize, board);
board = Utils.setBit(i + row * Main.edgeSize, board);
if ((i <= row && i <= col)) {
board = Utils.setBit(getPosition(col - i, row - i), board);
}
if ((i < Main.edgeSize - row && i < Main.edgeSize - col)) {
board = Utils.setBit(getPosition(col + i, row + i), board);
}
if (i <= row && i <= col)
board = Utils.setBit(getPosition(col + i, row - i), board);
if ((i < Main.edgeSize - row && i <= col))
board = Utils.setBit(getPosition(col - i, row + i), board);
}
board = Utils.clearBit(pos, board);
MapGenerator.queen[pos] = board;
return board;
} else {
return queen[pos];
}
}
}
static long generateValidRookMap(long whites, long blacks, int rookPosition, int color) {
if(rookPosition<0)return 0;
if (rookPosition > Main.boardSize)
return 0;
long ours = 0;
long theirs = 0;
if (color == Main.white) {
ours = whites;
theirs = blacks;
} else {
theirs = whites;
ours = blacks;
}
long map = generateRookMap(rookPosition);
long tmp = map;
// if((map&ours)!=0){
// map = ~(ours) & map;
// map = MapGenerator.sweepRookMoves(rookPosition, map, 0);
// }
if((tmp&(theirs|ours))!=0){
map = ~(theirs|ours) & map;
map = MapGenerator.sweepRookMoves(rookPosition, map, theirs);
}
return map;
}
static long generateValidQueenMap(long whites, long blacks, int queenPosition, int color) {
if(queenPosition<0)return 0;
// reuse rooks algo
if (queenPosition > Main.boardSize)
return 0;
long ours = 0;
long theirs = 0;
if (color == Main.white) {
ours = whites;
theirs = blacks;
} else {
theirs = whites;
ours = blacks;
}
long map = generatePawnQueenMap(queenPosition, false);
long tmp = map;
// if((map&ours)!=0){
// map = ~(ours) & map;
// map = MapGenerator.sweepQueenMoves(queenPosition, map, 0);
// }
if((tmp&(theirs|ours))!=0){
map = ~(theirs|ours) & map;
map = MapGenerator.sweepQueenMoves(queenPosition, map, theirs);
}
return map;
}
/** @param rookPos
* - rooks position on chess board
* @param validRookAttack
* - rooks map without friendly pieces (zeros where friendly are
* placed)
* @return */
static long sweepRookMoves(int rookPos, long validRookAttack, long enemies) {
if(rookPos<0)return 0;
boolean clL, clR, clU, clD;
clL = clR = clU = clD = false;
int col = rookPos % Main.edgeSize;
int row = rookPos / Main.edgeSize;
// from the piece go to every direction,
// if 0 set flag and set all remaining fields in given direction to 0
// too
int tmp = 0;
for (int i = 0; i < Main.edgeSize; i++) {
// check direction down
if ((i + row + 1) < Main.edgeSize) {
tmp = col + (i + row + 1) * Main.edgeSize;
if (clD) // if flag set, just clear
validRookAttack = Utils.clearBit(tmp, validRookAttack);
else if (Utils.getBit(tmp, validRookAttack) == 0) {
if (Utils.getBit(tmp, enemies) == 1)
validRookAttack = Utils.setBit(tmp, validRookAttack);
clD = true;
}
}
// check direction up
if ((row - i - 1) >= 0) {
tmp = col + (row - i - 1) * Main.edgeSize;
if (clU)
validRookAttack = Utils.clearBit(tmp, validRookAttack);
else if (Utils.getBit(tmp, validRookAttack) == 0) {
if (Utils.getBit(tmp, enemies) == 1)
validRookAttack = Utils.setBit(tmp, validRookAttack);
clU = true;
}
}
// check direction left
if ((col - i - 1) >= 0) {
tmp = col - i - 1 + row * Main.edgeSize;
if (clL)
validRookAttack = Utils.clearBit(tmp, validRookAttack);
else if (Utils.getBit(tmp, validRookAttack) == 0) {
if (Utils.getBit(tmp, enemies) == 1)
validRookAttack = Utils.setBit(tmp, validRookAttack);
clL = true;
}
}
// check direction right
if ((col + i + 1) < Main.edgeSize) {
tmp = col + i + 1 + row * Main.edgeSize;
if (clR)
validRookAttack = Utils.clearBit(tmp, validRookAttack);
else if (Utils.getBit(tmp, validRookAttack) == 0) {
if (Utils.getBit(tmp, enemies) == 1)
validRookAttack = Utils.setBit(tmp, validRookAttack);
clR = true;
}
}
}
return validRookAttack;
}
/** @param queenPos
* - rooks position on chess board
* @param validQueenAttack
* - rooks map without friendly pieces (zeros where friendly are
* placed)
* @return */
static long sweepQueenMoves(int queenPos, long validQueenAttack, long enemies) {
if(queenPos<0)return 0;
boolean clL, clR, clU, clD;
boolean clLa, clRa, clUa, clDa;
clL = clR = clU = clD = false;
clLa = clRa = clUa = clDa = false;
validQueenAttack = Utils.setBit(queenPos, validQueenAttack);
// MapGenerator.printMap(enemies);
int col = queenPos % Main.edgeSize;
int row = queenPos / Main.edgeSize;
// from the piece go to every direction,
// if 0 set flag and set all remaining fields in given direction to 0
// too
int tmp = 0;
for (int i = 0; i < Main.edgeSize; i++) {
// if flag (clD) is set, clear bits, if not set and map element is
// zero
// set flag (all following will be zero, if its first zero and enemy
// is present
// set is as one - so we have attacks too
if ((i <= row && i <= col)) {
tmp = getPosition(col - i, row - i);
if (clDa)
validQueenAttack = Utils.clearBit(tmp, validQueenAttack);
else if (0 == Utils.getBit(tmp, validQueenAttack)) {
if ((Utils.getBit(tmp, enemies) == 1))
validQueenAttack = Utils.setBit(tmp, validQueenAttack);
clDa = true;
}
}
if ((i < Main.edgeSize - row && i < Main.edgeSize - col)) {
tmp = getPosition(col + i, row + i);
if (clUa)
validQueenAttack = Utils.clearBit(tmp, validQueenAttack);
else if (0 == Utils.getBit(tmp, validQueenAttack)) {
if ((Utils.getBit(tmp, enemies) == 1))
validQueenAttack = Utils.setBit(tmp, validQueenAttack);
clUa = true;
}
}
if (i <= row && i <= col) {
tmp = getPosition(col + i, row - i);
if (clRa)
validQueenAttack = Utils.clearBit(tmp, validQueenAttack);
else if (0 == Utils.getBit(tmp, validQueenAttack)) {
if ((Utils.getBit(tmp, enemies) == 1))
validQueenAttack = Utils.setBit(tmp, validQueenAttack);
clRa = true;
}
}
if ((i < Main.edgeSize - row && i <= col)) {
tmp = getPosition(col - i, row + i);
if (clLa)
validQueenAttack = Utils.clearBit(tmp, validQueenAttack);
else if (0 == Utils.getBit(tmp, validQueenAttack)) {
if ((Utils.getBit(tmp, enemies) == 1))
validQueenAttack = Utils.setBit(tmp, validQueenAttack);
clLa = true;
}
}
// check direction down
if ((i + row + 1) < Main.edgeSize) {
tmp = col + (i + row + 1) * Main.edgeSize;
if (clD)
validQueenAttack = Utils.clearBit(tmp, validQueenAttack);
else if (Utils.getBit(tmp, validQueenAttack) == 0) {
if (Utils.getBit(tmp, enemies) == 1)
validQueenAttack = Utils.setBit(tmp, validQueenAttack);
clD = true;
}
}
// check direction up
if ((row - i - 1) >= 0) {
tmp = col + (row - i - 1) * Main.edgeSize;
if (clU)
validQueenAttack = Utils.clearBit(tmp, validQueenAttack);
else if (Utils.getBit(tmp, validQueenAttack) == 0) {
if (Utils.getBit(tmp, enemies) == 1)
validQueenAttack = Utils.setBit(tmp, validQueenAttack);
clU = true;
}
}
// check direction left
if ((col - i - 1) >= 0) {
tmp = col - i - 1 + row * Main.edgeSize;
if (clL)
validQueenAttack = Utils.clearBit(tmp, validQueenAttack);
else if (Utils.getBit(tmp, validQueenAttack) == 0) {
if (Utils.getBit(tmp, enemies) == 1)
validQueenAttack = Utils.setBit(tmp, validQueenAttack);
clL = true;
}
}
// check direction right
if ((col + i + 1) < Main.edgeSize) {
tmp = col + i + 1 + row * Main.edgeSize;
if (clR)
validQueenAttack = Utils.clearBit(tmp, validQueenAttack);
else if (Utils.getBit(tmp, validQueenAttack) == 0) {
if (Utils.getBit(tmp, enemies) == 1)
validQueenAttack = Utils.setBit(tmp, validQueenAttack);
clR = true;
}
}
}
validQueenAttack = Utils.clearBit(queenPos, validQueenAttack);
return validQueenAttack;
}
public static void main(String[] args) throws Exception {
//
// MapGenerator.printMap(MapGenerator.generatePawnQueenMap(18, false));
// System.out.println();
// MapGenerator.printMap(MapGenerator.generatePawnQueenMap(45, false));
// System.out.println();
// MapGenerator.printMap(MapGenerator.generatePawnQueenMap(1, false));
// System.out.println();
MapGenerator.printMap(MapGenerator.generatePawnQueenMap(16, false));
System.out.println();
long temp = MapGenerator.generatePawnQueenMap(16, false);
temp = temp & ~0x0E0600;
long hah = MapGenerator.sweepQueenMoves(13, temp, 0x0);
MapGenerator.printMap(hah);
// MapGenerator.printMap(MapGenerator.generatePawnAttackMap(8));
// MapGenerator.printMap(MapGenerator.generatePawnAttackMap(7));
// System.out.println();
// MapGenerator.printMap(MapGenerator.generatePawnAttackMap(40));
MapGenerator.printMap(MapGenerator.generateRookMap(0));
System.out.println();
// String last = "0x%xL";
// String normal = "0x%xL, ";
//
// String pattern=normal;
// System.out.println("public static long[] kingMap = {");
// for(int i = 0; i<49; i++){
// if(i==48)pattern=last;
// System.out.print(String.format(pattern, MapGenerator.generateKingMap(i)));
// if((i+1)%10==0)
// System.out.println();
// }
// System.out.println("};");
//
// pattern=normal;
// System.out.println("public static long[] rookMap = {");
// for(int i = 0; i<49; i++){
// if(i==48)pattern=last;
// System.out.print(String.format(pattern, MapGenerator.generateRookMap(i)));
// if((i+1)%10==0)
// System.out.println();
// }
// System.out.println("};");
//
// pattern=normal;
// System.out.println("public static long[] pawnMap = {");
// for(int i = 0; i<49; i++){
// if(i==48)pattern=last;
// System.out.print(String.format(pattern, MapGenerator.generatePawnQueenMap(i, true)));
// if((i+1)%10==0)
// System.out.println();
// }
// System.out.println("};");
//
// pattern=normal;
// System.out.println("public static long[] queenMap = {");
// for(int i = 0; i<49; i++){
// if(i==48)pattern=last;
// System.out.print(String.format(pattern, MapGenerator.generatePawnQueenMap(i, false)));
// if((i+1)%10==0)
// System.out.println();
// }
// System.out.println("};");
//
// pattern=normal;
// System.out.println("public static long[] pawnAttackMap = {");
// for(int i = 0; i<49; i++){
// if(i==48)pattern=last;
// System.out.print(String.format(pattern, MapGenerator.generatePawnAttackMap(i)));
// if((i+1)%10==0)
// System.out.println();
// }
// System.out.println("};");
MapGenerator.printMap(MapGenerator.generateRiderMap(25));
System.out.println();
MapGenerator.printMap(MapGenerator.generateRiderMap(4));
System.out.println();
MapGenerator.printMap(MapGenerator.generateRiderMap(0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment