📄 chess.java
字号:
// first check for possible pawn captures:
for (int delta=-1; delta<= 1; delta += 2) {
move_offset = square_index + side_index * 10 + delta;
int target = b[move_offset];
if ((target <= -1 && target != 7 && piece > 0) ||
(target >= 1 && target != 7 && piece < 0)) {
// kluge: count pawn control more:
control[square_index + side_index * delta] += 1.25f;
}
}
}
// Note: no break here: we want pawns to use move table also:
case ChessPosition.KNIGHT:
case ChessPosition.BISHOP:
case ChessPosition.ROOK:
case ChessPosition.KING:
case ChessPosition.QUEEN:
{
move_index = piece; if (move_index < 0) move_index = -move_index;
move_index = index[move_index];
//System.out.println("move_index="+move_index);
next_square = square_index + pieceMovementTable[move_index];
outer:
while (true) {
inner:
while (true) {
if (next_square > 99) break inner;
if (next_square < 22) break inner;
if (b[next_square] == 7) break inner;
control[next_square] += 1;
// the next statement should be augmented for x-ray analysis:
if (side_index < 0 && b[next_square] < 0) break inner;
if (side_index > 0 && b[next_square] > 0 && b[next_square] != 7) break inner;
// NOTE: prevents calculating guarding:
//if (b[next_square] != 0) break inner;
if (piece_type == ChessPosition.PAWN &&
(square_index / 10 == 3)) break inner;
if (piece_type == ChessPosition.KNIGHT) break inner;
if (piece_type == ChessPosition.KING) break inner;
next_square += pieceMovementTable[move_index];
}
move_index += 1;
if (pieceMovementTable[move_index] == 0) break outer;
next_square = square_index + pieceMovementTable[move_index];
}
}
}
}
// System.out.println("Human control:");
// for (int i=99; i>=22; i--) {
// if (b[i] == 7 && b[i+1]==7) System.out.println();
// if (b[i] != 7) System.out.print(humanControl[i]);
// }
// System.out.println();
// System.out.println("Computer control:");
// for (int i=99; i>=22; i--) {
// if (b[i] == 7 && b[i+1]==7) System.out.println();
// if (b[i] != 7) System.out.print(computerControl[i]);
// }
// System.out.println();
// System.exit(1);
}
static class aMove {
int from;
int to;
}
private static aMove [] possibleMoveList = new aMove[255];
static {
for (int i=0; i<255; i++) possibleMoveList[i] = new aMove();
}
private int calcPossibleMoves(ChessPosition pos, boolean player) {
//System.out.println("calcPossibleMoves()");
int [] b = pos.board;
int count = 0;
for (int i=22; i<100; i++) {
int board_val = b[i];
//System.out.println(board_val);
if (board_val == 7) continue;
// computer pieces will be negative:
if ((board_val < 0 && !player) || (board_val > 0 && player)) {
int num = calcPieceMoves(pos, i);
for (int j=0; j<num; j++) {
if (b[piece_moves[j]] != 7) {
//System.out.println("count="+count+", i="+i);
possibleMoveList[count].from = i;
possibleMoveList[count].to = piece_moves[j];
// System.out.println("possible move: player="+player+
// ", from="+i+", to="+piece_moves[j]);
count++;
}
}
// TBD: castle logic, etc. (page 159)
}
}
return count;
}
private int calcPieceMoves(ChessPosition pos, int square_index) {
int [] b = pos.board;
int piece = b[square_index];
int piece_type = piece;
if (piece_type < 0) piece_type = -piece_type;
int count = 0, side_index, move_offset, temp, next_square;
int piece_index = index[piece_type];
int move_index = pieceMovementTable[piece_index];
if (piece < 0) side_index = -1;
else side_index = 1;
switch (piece_type) {
case ChessPosition.PAWN:
{
// first check for possible pawn captures:
for (int delta=-1; delta<= 1; delta += 2) {
move_offset = square_index + side_index * 10 + delta;
int target = b[move_offset];
if ((target <= -1 && target != 7 && piece > 0) ||
(target >= 1 && target != 7 && piece < 0)) {
piece_moves[count++] = square_index + side_index * delta;
}
}
// check for initial pawn move of 2 squares forward:
move_offset = square_index + side_index * 20;
if (piece > 0) temp = 3; else temp = 8;
if (b[move_offset] == 0 &&
(square_index / 10) == temp &&
((piece < 0 && b[square_index - 10]==0) ||
(piece > 0 && b[square_index + 10]==0))) {
piece_moves[count++] = square_index + side_index * 20;
}
// try to move forward 1 square:
move_offset = square_index + side_index * 10;
if (b[move_offset] == 0) {
piece_moves[count++] = move_offset;
}
}
break;
case ChessPosition.KNIGHT:
case ChessPosition.BISHOP:
case ChessPosition.ROOK:
case ChessPosition.KING:
case ChessPosition.QUEEN:
{
move_index = piece; if (move_index < 0) move_index = -move_index;
move_index = index[move_index];
//System.out.println("move_index="+move_index);
next_square = square_index + pieceMovementTable[move_index];
outer:
while (true) {
inner:
while (true) {
if (next_square > 99) break inner;
if (next_square < 22) break inner;
if (b[next_square] == 7) break inner;
// check for piece on the same side:
if (side_index < 0 && b[next_square] < 0) break inner;
if (side_index >0 && b[next_square] > 0) break inner;
piece_moves[count++] = next_square;
if (b[next_square] != 0) break inner;
if (piece_type == ChessPosition.KNIGHT) break inner;
if (piece_type == ChessPosition.KING) break inner;
next_square += pieceMovementTable[move_index];
}
move_index += 1;
if (pieceMovementTable[move_index] == 0) break outer;
next_square = square_index + pieceMovementTable[move_index];
}
}
}
return count;
}
private static int [] piece_moves = new int[255];
private static int [] initialBoard = {
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
4, 2, 3, 5, 9, 3, 2, 4, 7, 7, // white pieces
1, 1, 1, 1, 1, 1, 1, 1, 7, 7, // white pawns
0, 0, 0, 0, 0, 0, 0, 0, 7, 7, // 8 blank squares, 2 off board
0, 0, 0, 0, 0, 0, 0, 0, 7, 7, // 8 blank squares, 2 off board
0, 0, 0, 0, 0, 0, 0, 0, 7, 7, // 8 blank squares, 2 off board
0, 0, 0, 0, 0, 0, 0, 0, 7, 7, // 8 blank squares, 2 off board
-1,-1,-1,-1,-1,-1,-1,-1, 7, 7, // black pawns
-4,-2,-3,-5,-9,-3,-2,-4, 7, 7, // black pieces
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
};
private static int [] index = {
0, 12, 15, 10, 1, 6, 0, 0, 0, 6
};
private static int [] pieceMovementTable = {
0, -1, 1, 10, -10, 0, -1, 1, 10, -10, -9, -11, 9,
11, 0, 8, -8, 12, -12, 19, -19, 21, -21, 0, 10, 20,
0, 0, 0, 0, 0, 0, 0, 0
};
private static int [] value = {
0, 1, 3, 3, 5, 9, 0, 0, 0, 12
};
private static int [] blackSquares = {
22, 24, 26, 28, 33, 35, 37, 39,
42, 44, 46, 48, 53, 55, 57, 59,
62, 64, 66, 68, 73, 75, 77, 79,
82, 84, 86, 88, 93, 95, 97, 99
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -