📄 validate.c
字号:
// Get the opponents king position.
if (piececolour == WHITE)
{ opp_king_pos[0] = bl_king_pos[0];
opp_king_pos[1] = bl_king_pos[1];
}
else
{ opp_king_pos[0] = wh_king_pos[0];
opp_king_pos[1] = wh_king_pos[1];
}
// King can move in any direction but
// only by 1 square.
val_north (from,piececolour,1);
val_south (from,piececolour,1);
val_east (from,piececolour,1);
val_west (from,piececolour,1);
val_northeast (from,piececolour,1);
val_northwest (from,piececolour,1);
val_southeast (from,piececolour,1);
val_southwest (from,piececolour,1);
// King Castle.
if (piececolour & 0x08)
{ tmp = bl_base_cont;
row = MAX_ROW;
}
else
{ tmp = wh_base_cont;
row = MIN_ROW;
}
if ((tmp & 0x1F)== 0x11)
{ // Can Castle QueenSide.
validmovemask[row] |= 0x04;
}
if ((tmp & 0xF0)==0x90)
{// Can Castle KingSide.
validmovemask[row] |= 0x40;
}
}
/************************************************************************
***** Directional Validation Functions *****
*******************************************/
VOID val_north (LOC from,BOOL piececolour,BOOL depth)
// Validate Squares directly above the current position.
{ COORD row = from[0],col = from[1];
BOOL piecefound = FALSE;
while ((row++ < MAX_ROW) && (!piecefound) && (depth--))
{ if (board[row][col] == EMPTY)
{ // Empty Square.
validmovemask[row] |= (1<<col);
}
else if (is_opp_piece(row,col,piececolour))
{ // Opponents Piece.
validmovemask[row] |= (1<<col);
capturemask[row] |= (1<<col);
if (capturemask[opp_king_pos[0]] & (1 << opp_king_pos[1])) kingcapture = TRUE;
piecefound = TRUE;
}
else
{ //Our Piece.
piecefound = TRUE;
}
}
}
VOID val_south (LOC from,BOOL piececolour,BOOL depth)
{ // Validate directly down from the current location.
CHAR row = from[0], col = from[1];
BOOL piecefound = FALSE;
while ((row-- > MIN_ROW) && (!piecefound) && (depth--))
{ if (board[row][col] == EMPTY)
{ // Empty Square.
validmovemask[row] |= (1<<col);
}
else if (is_opp_piece(row,col,piececolour))
{ // Opponents Piece.
validmovemask[row] |= (1<<col);
capturemask[row] |= (1<<col);
if (capturemask[opp_king_pos[0]] & (1 << opp_king_pos[1])) kingcapture = TRUE;
piecefound = TRUE;
}
else
{ //Our Piece.
piecefound = TRUE;
}
}
}
VOID val_east (LOC from,BOOL piececolour,BOOL depth)
{ // Validate directly to the right of the current location.
COORD row = from[0],col = from[1];
BOOL piecefound = FALSE;
while ((col++ < MAX_COL) && (!piecefound) && (depth--))
{ if (board[row][col] == EMPTY)
{ // Empty Square.
validmovemask[row] |= (1<<col);
}
else if (is_opp_piece(row,col,piececolour))
{ // Opponents Piece.
validmovemask[row] |= (1<<col);
capturemask[row] |= (1<<col);
if (capturemask[opp_king_pos[0]] & (1 << opp_king_pos[1])) kingcapture = TRUE;
piecefound = TRUE;
}
else
{ //Our Piece.
piecefound = TRUE;
}
}
}
VOID val_west (LOC from,BOOL piececolour,BOOL depth)
{ // Validate directly to the left of the current location.
CHAR row = from[0],col = from[1];
BOOL piecefound = FALSE;
while ((col-- > MIN_COL) && (!piecefound) && (depth--))
{ if (board[row][col] == EMPTY)
{ // Empty Square.
validmovemask[row] |= (1<<col);
}
else if (is_opp_piece(row,col,piececolour))
{ // Opponents Piece.
validmovemask[row] |= (1<<col);
capturemask[row] |= (1<<col);
if (capturemask[opp_king_pos[0]] & (1 << opp_king_pos[1])) kingcapture = TRUE;
piecefound = TRUE;
}
else
{ //Our Piece.
piecefound = TRUE;
}
}
}
VOID val_northwest (LOC from,BOOL piececolour,BOOL depth)
{ // Validate diagonally left and up from the current location.
// This corresponds to an index increment of 7.
COORD row = from[0], col = from[1];
BOOL piecefound = FALSE;
while ((col-- > MIN_COL) && (row++ < MAX_ROW) && (!piecefound) && (depth--))
{ // We haven't found a piece, are still in scope and within search depth.
if (board[row][col] == EMPTY)
{// Empty Square.
validmovemask[row] |= (1<<col);
}
else if ((board[row][col] & 0x08) ^ piececolour)
{// Opponents Piece.
validmovemask[row] |= (1<<col);
capturemask[row] |= (1<<col);
if (capturemask[opp_king_pos[0]] & (1 << opp_king_pos[1])) kingcapture = TRUE;
piecefound = TRUE;
}
else
{// Our Piece.
piecefound = TRUE;
}
}
}
VOID val_northeast(LOC from,BOOL piececolour,BOOL depth)
{ // Validate diagonally right and up from the current location.
// This corresponds to an index increment of 9;
COORD row = from[0], col = from[1];
BOOL piecefound = FALSE;
while ((col++ < MAX_COL) && (row++ < MAX_ROW) && (!piecefound) && (depth--))
{ // We haven't found a piece, are still in scope and within search depth.
if (board[row][col] == EMPTY)
{// Empty Square.
validmovemask[row] |= (1<<col);
}
else if ((board[row][col] & 0x08) ^ piececolour)
{// Opponents Piece.
validmovemask[row] |= (1<<col);
capturemask[row] |= (1<<col);
if (capturemask[opp_king_pos[0]] & (1 << opp_king_pos[1])) kingcapture = TRUE;
piecefound = TRUE;
}
else
{// Our Piece.
piecefound = TRUE;
}
}
}
VOID val_southeast (LOC from,BOOL piececolour,BOOL depth)
{ // Validate diagonally right and down from the current location.
// This translates as an index decrement of 7.
COORD row = from[0], col = from[1];
BOOL piecefound = FALSE;
while ((col++ < MAX_COL) && (row-- > MIN_ROW) && (!piecefound) && (depth--))
{ // We haven't found a piece, are still in scope and within search depth.
if (board[row][col] == EMPTY)
{// Empty Square.
validmovemask[row] |= (1<<col);
}
else if ((board[row][col] & 0x08) ^ piececolour)
{// Opponents Piece.
validmovemask[row] |= (1<<col);
capturemask[row] |= (1<<col);
if (capturemask[opp_king_pos[0]] & (1 << opp_king_pos[1])) kingcapture = TRUE;
piecefound = TRUE;
}
else
{// Our Piece.
piecefound = TRUE;
}
}
}
VOID val_southwest(LOC from,BOOL piececolour,BOOL depth)
{// Validate Left and Down from the current location.
// This corresponds to an index subtraction of 9.
COORD row = from[0], col = from[1];
BOOL piecefound = FALSE;
while ((col-- > MIN_COL) && (row-- > MIN_ROW) && (!piecefound) && (depth--))
{ // We haven't found a piece, are still in scope and within search depth.
if (board[row][col] == EMPTY)
{// Empty Square.
validmovemask[row] |= (1<<col);
}
else if ((board[row][col] & 0x08) ^ piececolour)
{// Opponents Piece.
validmovemask[row] |= (1<<col);
capturemask[row] |= (1<<col);
if (capturemask[opp_king_pos[0]] & (1 << opp_king_pos[1])) kingcapture = TRUE;
piecefound = TRUE;
}
else
{// Our Piece.
piecefound = TRUE;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -