📄 move.cpp
字号:
// move.cpp
// includes
#include <cctype>
#include <cstring>
#include "move.h"
// constants
static const int promote_piece[4] =
{
(1 << 4), (1 << 5), (1 << 6), ((1 << 5) | (1 << 6))
};
// functions
// move_is_ok()
bool move_is_ok(int move)
{
if(move < 0 || move >= 65536)
return false;
if(move == 0)
return false;
if(move == 11)
return false;
return true;
}
// move_promote()
int move_promote(int move)
{
int code, piece;
code = (move >> 12) & 3;
piece = promote_piece[code];
if(SQUARE_RANK(MOVE_TO(move)) == (0xB))
{
piece |= (1 << 0);
}
else
{
piece |= (1 << 1);
}
return piece;
}
// move_order()
int move_order(int move)
{
return ((move & 07777) << 2) | ((move >> 12) & 3);
}
// move_is_capture()
bool move_is_capture(int move, const board_t *board)
{
return MOVE_IS_EN_PASSANT(move) || board->square[MOVE_TO(move)] != 0;
}
// move_is_under_promote()
bool move_is_under_promote(int move)
{
return MOVE_IS_PROMOTE(move) && (move &(0xF << 12)) != ((2 << 14) | (3 << 12));
}
// move_is_tactical()
bool move_is_tactical(int move, const board_t *board)
{
return (move &(1 << 15)) != 0 || board->square[MOVE_TO(move)] != 0;
}
// move_capture()
int move_capture(int move, const board_t *board)
{
if(MOVE_IS_EN_PASSANT(move))
{
return PAWN_OPP(board->square[MOVE_FROM(move)]);
}
return board->square[MOVE_TO(move)];
}
// move_to_string()
bool move_to_string(int move, char string [], int size)
{
if(size < 6)
return false;
// null move
if(move == 11)
{
strcpy(string, null_move_string);
return true;
}
// normal moves
square_to_string(MOVE_FROM(move), &string[0], 3);
square_to_string(MOVE_TO(move), &string[2], 3);
// promotes
if(MOVE_IS_PROMOTE(move))
{
string[4] = tolower(piece_to_char(move_promote(move)));
string[5] = '\0';
}
return true;
}
// move_from_string()
int move_from_string(const char string [], const board_t *board)
{
char tmp_string[3];
int from, to;
int move;
int piece, delta;
// from
tmp_string[0] = string[0];
tmp_string[1] = string[1];
tmp_string[2] = '\0';
from = square_from_string(tmp_string);
if(from == 0)
return 0;
// to
tmp_string[0] = string[2];
tmp_string[1] = string[3];
tmp_string[2] = '\0';
to = square_from_string(tmp_string);
if(to == 0)
return 0;
move = MOVE_MAKE(from, to);
// promote
switch(string[4])
{
case '\0': // not a promotion
break;
case 'n':
move |= ((2 << 14) | (0 << 12));
break;
case 'b':
move |= ((2 << 14) | (1 << 12));
break;
case 'r':
move |= ((2 << 14) | (2 << 12));
break;
case 'q':
move |= ((2 << 14) | (3 << 12));
break;
default:
return 0;
}
// flags
piece = board->square[from];
if(PIECE_IS_PAWN(piece))
{
if(to == board->ep_square)
move |= (3 << 14);
}
else if(PIECE_IS_KING(piece))
{
delta = to - from;
if(delta == +2 || delta == -2)
move |= (1 << 14);
}
return move;
}
// end of move.cpp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -