Skip to content

Commit

Permalink
style, draw claim
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniogarro committed Dec 5, 2014
1 parent 5494ce5 commit 7681287
Show file tree
Hide file tree
Showing 19 changed files with 1,926 additions and 1,941 deletions.
144 changes: 72 additions & 72 deletions attacked.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,84 +32,84 @@

int AttackingPieces(const BOARD *board, SQUARE square, COLOR attacking_color, SQUARE *attacking_sqs)
{
int attackers = 0;
SQUARE attacking_sq = 0;

PIECE N_attacker, B_attacker, R_attacker, Q_attacker, K_attacker;

if(attacking_color){
for(int i = 0; w_pawn_capture[i]; i++){
attacking_sq = square - w_pawn_capture[i];
if(IN_BOARD(attacking_sq) && board->squares[attacking_sq] == W_PAWN){
if(attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
}
}
N_attacker = W_KNIGHT, B_attacker = W_BISHOP, R_attacker = W_ROOK,
Q_attacker = W_QUEEN, K_attacker = W_KING;
}else{
for(int i = 0; b_pawn_capture[i]; i++){
attacking_sq = square - b_pawn_capture[i];
if(IN_BOARD(attacking_sq) && board->squares[attacking_sq] == B_PAWN){
if(attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
}
}
N_attacker = B_KNIGHT, B_attacker = B_BISHOP, R_attacker = B_ROOK,
Q_attacker = B_QUEEN, K_attacker = B_KING;
int attackers = 0;
SQUARE attacking_sq = 0;

PIECE N_attacker, B_attacker, R_attacker, Q_attacker, K_attacker;

if (attacking_color) {
for (int i = 0; w_pawn_capture[i]; i++) {
attacking_sq = square - w_pawn_capture[i];
if (IN_BOARD(attacking_sq) && board->squares[attacking_sq] == W_PAWN) {
if (attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
}
}
N_attacker = W_KNIGHT, B_attacker = W_BISHOP, R_attacker = W_ROOK,
Q_attacker = W_QUEEN, K_attacker = W_KING;
} else {
for (int i = 0; b_pawn_capture[i]; i++) {
attacking_sq = square - b_pawn_capture[i];
if (IN_BOARD(attacking_sq) && board->squares[attacking_sq] == B_PAWN) {
if (attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
}
}
N_attacker = B_KNIGHT, B_attacker = B_BISHOP, R_attacker = B_ROOK,
Q_attacker = B_QUEEN, K_attacker = B_KING;
}

for(int i = 0; knight_delta[i]; i++){
attacking_sq = square + knight_delta[i];
if(IN_BOARD(attacking_sq)){
if(board->squares[attacking_sq] == N_attacker){
if(attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
}
}
for (int i = 0; knight_delta[i]; i++) {
attacking_sq = square + knight_delta[i];
if (IN_BOARD(attacking_sq)) {
if (board->squares[attacking_sq] == N_attacker) {
if (attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
}
}
}

for(int i = 0; bishop_delta[i]; i++){
for(attacking_sq = square + bishop_delta[i]; IN_BOARD(attacking_sq); attacking_sq += bishop_delta[i]){
if(board->squares[attacking_sq] == EMPTY){
continue;
}else if(board->squares[attacking_sq] == B_attacker) {
if(attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
break;
}else if(board->squares[attacking_sq] == Q_attacker){
if(attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
break;
}else break; /*Piece other than bishop/queen, or different color, stop sliding*/
}
for (int i = 0; bishop_delta[i]; i++) {
for (attacking_sq = square + bishop_delta[i]; IN_BOARD(attacking_sq); attacking_sq += bishop_delta[i]) {
if (board->squares[attacking_sq] == EMPTY) {
continue;
} else if (board->squares[attacking_sq] == B_attacker) {
if (attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
break;
} else if (board->squares[attacking_sq] == Q_attacker) {
if (attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
break;
} else break; /*Piece other than bishop/queen, or different color, stop sliding*/
}
}

for(int i = 0; rook_delta[i]; i++){
for(attacking_sq = square + rook_delta[i]; IN_BOARD(attacking_sq); attacking_sq += rook_delta[i]){
if(board->squares[attacking_sq] == EMPTY){
continue;
}else if(board->squares[attacking_sq] == R_attacker){
if(attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
break;
}else if(board->squares[attacking_sq] == Q_attacker){
if(attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
break;
}else break;
}
for (int i = 0; rook_delta[i]; i++) {
for (attacking_sq = square + rook_delta[i]; IN_BOARD(attacking_sq); attacking_sq += rook_delta[i]) {
if (board->squares[attacking_sq] == EMPTY) {
continue;
} else if (board->squares[attacking_sq] == R_attacker) {
if (attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
break;
} else if (board->squares[attacking_sq] == Q_attacker) {
if (attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
break;
} else break;
}
for(int i = 0; king_delta[i]; i++){
attacking_sq = square + king_delta[i];
if(IN_BOARD(attacking_sq)){
if(board->squares[attacking_sq] == K_attacker){
if(attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
break;
}
}
}

for (int i = 0; king_delta[i]; i++) {
attacking_sq = square + king_delta[i];
if (IN_BOARD(attacking_sq)) {
if (board->squares[attacking_sq] == K_attacker) {
if (attacking_sqs) attacking_sqs[attackers++] = attacking_sq;
else return 1;
break;
}
}
return attackers;
}
return attackers;
}
156 changes: 78 additions & 78 deletions board.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,98 +34,98 @@

void InitBoard(BOARD *board)
{
unsigned long long init[4]={0x12345ULL,0x23456ULL,0x34567ULL,0x45678ULL};
unsigned long long length = 4;
init_by_array64(init, length);
board->zobrist_key = 0;
for(SQUARE sq = 0; sq < BOARDSIZE; sq++) {
board->squares[sq] = EMPTY;
/*Note j = 1; we want zobrist keys for EMPTY to be 0:*/
for (int j = 1; j <= 0xF; j++){
zobkeys.zob_pieces[j][sq] = genrand64_int64();
}
zobkeys.zob_enpass[sq] = genrand64_int64();
unsigned long long init[4]={0x12345ULL,0x23456ULL,0x34567ULL,0x45678ULL};
unsigned long long length = 4;
init_by_array64(init, length);
board->zobrist_key = 0;
for (SQUARE sq = 0; sq < BOARDSIZE; sq++) {
board->squares[sq] = EMPTY;
/*Note j = 1; we want zobrist keys for EMPTY to be 0:*/
for (int j = 1; j <= 0xF; j++) {
zobkeys.zob_pieces[j][sq] = genrand64_int64();
}
for(int i = 0; i < 4; i++) zobkeys.zob_castle[i] = genrand64_int64();
zobkeys.zob_side = genrand64_int64();
board->wk_castle = 0;
board->bk_castle = 0;
board->wq_castle = 0;
board->bq_castle = 0;
board->en_passant = INVALID_SQ; /*there's no en passant pawn initialy.*/
board->pawn_material[0] = 0, board->pawn_material[1] = 0;
board->piece_material[0] = 0, board->piece_material[1] = 0;
board->bishops[0] = 0, board->bishops[1] = 0;
zobkeys.zob_enpass[sq] = genrand64_int64();
}
for (int i = 0; i < 4; i++) zobkeys.zob_castle[i] = genrand64_int64();
zobkeys.zob_side = genrand64_int64();
board->wk_castle = 0;
board->bk_castle = 0;
board->wq_castle = 0;
board->bq_castle = 0;
board->en_passant = INVALID_SQ; /*there's no en passant pawn initialy.*/
board->pawn_material[0] = 0, board->pawn_material[1] = 0;
board->piece_material[0] = 0, board->piece_material[1] = 0;
board->bishops[0] = 0, board->bishops[1] = 0;
}

void InitZobrist(BOARD *board)
{
board->zobrist_key = 0;
for (int i = 0; i < BOARDSIZE; i++){
if(IN_BOARD(i)) board->zobrist_key ^= zobkeys.zob_pieces[board->squares[i]][i];
}
if(board->wk_castle) board->zobrist_key ^= zobkeys.zob_castle[0];
if(board->wq_castle) board->zobrist_key ^= zobkeys.zob_castle[1];
if(board->bk_castle) board->zobrist_key ^= zobkeys.zob_castle[2];
if(board->bq_castle) board->zobrist_key ^= zobkeys.zob_castle[3];
board->zobrist_key = 0;
for (int i = 0; i < BOARDSIZE; i++) {
if (IN_BOARD(i)) board->zobrist_key ^= zobkeys.zob_pieces[board->squares[i]][i];
}
if (board->wk_castle) board->zobrist_key ^= zobkeys.zob_castle[0];
if (board->wq_castle) board->zobrist_key ^= zobkeys.zob_castle[1];
if (board->bk_castle) board->zobrist_key ^= zobkeys.zob_castle[2];
if (board->bq_castle) board->zobrist_key ^= zobkeys.zob_castle[3];

if(IN_BOARD(board->en_passant)) board->zobrist_key ^= zobkeys.zob_enpass[board->en_passant];
if(board->white_to_move) board->zobrist_key ^= zobkeys.zob_side;
if (IN_BOARD(board->en_passant)) board->zobrist_key ^= zobkeys.zob_enpass[board->en_passant];
if (board->white_to_move) board->zobrist_key ^= zobkeys.zob_side;
}

void InitMaterial(BOARD *board)
{
board->pawn_material[0] = 0, board->pawn_material[1] = 0;
board->piece_material[0] = 0, board->piece_material[1] = 0;
board->pawns[0] = 0, board->pawns[1] = 0;
board->bishops[0] = 0, board->bishops[1] = 0;
for (int sq = 0; sq < BOARDSIZE; sq++){
PIECE p = board->squares[sq];
if(IN_BOARD(sq) && p != EMPTY && p != W_KING && p != B_KING){
if(p == W_PAWN || p == B_PAWN){
board->pawn_material[GET_COLOR(p)] += Value(p);
board->pawns[GET_COLOR(p)] = BitboardSet(sq, board->pawns[GET_COLOR(p)]);
}else{
if(p == W_BISHOP || p == B_BISHOP) board->bishops[GET_COLOR(p)]++;
board->piece_material[GET_COLOR(p)] += Value(p);
}
}
board->pawn_material[0] = 0, board->pawn_material[1] = 0;
board->piece_material[0] = 0, board->piece_material[1] = 0;
board->pawns[0] = 0, board->pawns[1] = 0;
board->bishops[0] = 0, board->bishops[1] = 0;
for (int sq = 0; sq < BOARDSIZE; sq++) {
PIECE p = board->squares[sq];
if (IN_BOARD(sq) && p != EMPTY && p != W_KING && p != B_KING) {
if (p == W_PAWN || p == B_PAWN) {
board->pawn_material[GET_COLOR(p)] += Value(p);
board->pawns[GET_COLOR(p)] = BitboardSet(sq, board->pawns[GET_COLOR(p)]);
} else {
if (p == W_BISHOP || p == B_BISHOP) board->bishops[GET_COLOR(p)]++;
board->piece_material[GET_COLOR(p)] += Value(p);
}
}
}
}

void PrintBoard(const BOARD *board)
{
printf("\n");
printf("\n+--+--+--+--+--+--+--+--+ %c %c%c%c%c +--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+\n",
(board->white_to_move+1?'W':'B'),
(board->wq_castle?'Q':' '), (board->wk_castle?'K':' '),
(board->bq_castle?'q':' '), (board->bk_castle?'k':' '));

for(int i = 0x70; i >= 0; i++){
if(!(i&0x88)){
printf("| %c", PieceToChar(board->squares[i]));
}else{
printf("| ");
for(int j = 0; j<8; j++) printf(" %c ", (board->pawns[0] & (1ull << (ROW(i-1) >> 1) << j)) ? 'p' : ' ');
printf(" ");
for(int j = 0; j<8; j++) printf(" %c ", (board->pawns[1] & (1ull << (ROW(i-1) >> 1) << j)) ? 'P' : ' ');
printf("\n+--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+\n");
i -= 0x19;
}
printf("\n");
printf("\n+--+--+--+--+--+--+--+--+ %c %c%c%c%c +--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+\n",
(board->white_to_move+1?'W':'B'),
(board->wq_castle?'Q':' '), (board->wk_castle?'K':' '),
(board->bq_castle?'q':' '), (board->bk_castle?'k':' '));

for (int i = 0x70; i >= 0; i++) {
if (!(i&0x88)) {
printf("| %c", PieceToChar(board->squares[i]));
} else {
printf("| ");
for (int j = 0; j<8; j++) printf(" %c ", (board->pawns[0] & (1ull << (ROW(i-1) >> 1) << j)) ? 'p' : ' ');
printf(" ");
for (int j = 0; j<8; j++) printf(" %c ", (board->pawns[1] & (1ull << (ROW(i-1) >> 1) << j)) ? 'P' : ' ');
printf("\n+--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+\n");
i -= 0x19;
}
printf("Ply: %i (%i) EnPassant: %x\n", board->ply, board->rev_plies[board->ply], board->en_passant);
printf("Material: W: %i %i; B: %i %i; Zobrist: %Lu; Stage: %f PawnStage: %f\n",
board->piece_material[1], board->pawn_material[1],
board->piece_material[0], board->pawn_material[0],
board->zobrist_key, GameStage(board), PawnStage(board));
printf("BB: %llx , %llx; Passers: %llx ranks %i; %llx ranks %i ",
board->pawns[0], board->pawns[1],
WPassedPawns(board->pawns[1], board->pawns[0]),
DotProduct(WPassedPawns(board->pawns[1], board->pawns[0]), WRANKS),
BPassedPawns(board->pawns[0], board->pawns[1]),
DotProduct(BPassedPawns(board->pawns[0], board->pawns[1]), BRANKS));
printf("Doubled: %i %i; Eval: %i\n",
BitCount(DoubledPawns(board->pawns[1])),
BitCount(DoubledPawns(board->pawns[0])),
StaticEval(board, 0));
}
printf("Ply: %i (%i) EnPassant: %x\n", board->ply, board->rev_plies[board->ply], board->en_passant);
printf("Material: W: %i %i; B: %i %i; Zobrist: %Lu; Stage: %f PawnStage: %f\n",
board->piece_material[1], board->pawn_material[1],
board->piece_material[0], board->pawn_material[0],
board->zobrist_key, GameStage(board), PawnStage(board));
printf("BB: %llx , %llx; Passers: %llx ranks %i; %llx ranks %i ",
board->pawns[0], board->pawns[1],
WPassedPawns(board->pawns[1], board->pawns[0]),
DotProduct(WPassedPawns(board->pawns[1], board->pawns[0]), WRANKS),
BPassedPawns(board->pawns[0], board->pawns[1]),
DotProduct(BPassedPawns(board->pawns[0], board->pawns[1]), BRANKS));
printf("Doubled: %i %i; Eval: %i\n",
BitCount(DoubledPawns(board->pawns[1])),
BitCount(DoubledPawns(board->pawns[0])),
StaticEval(board));
}
Loading

0 comments on commit 7681287

Please sign in to comment.