is_legal: fix check+pinned and knight check; perft-test + perft2()

This commit is contained in:
2024-03-26 17:37:18 +01:00
parent 7637bdad10
commit 08ba989170
7 changed files with 457 additions and 87 deletions

View File

@@ -40,6 +40,7 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move)
square_t from = move_from(move), to = move_to(move), king = pos->king[us], sq;
piece_type_t piece = PIECE(pos->board[from]);
bitboard_t kingbb = pos->bb[us][KING], tmp;
u64 pinned = mask(from) & pos->blockers & pos->bb[us][ALL_PIECES];
/* (1) - King
* For castling, we already tested intermediate squares attacks
@@ -53,14 +54,22 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move)
/* (2) - King is in check
* Double-check is already handled, as only K moves were generated.
* Here, allowed moves are only on King-attacker line, including
* attacker.We can move a piece on
* in pseudo move generation, so we only care destination square here.
* Here, allowed dest squares are only on King-checker line, or on checker
* square.
* attacker.
* Special cases:
* e.p., legal if the taken pawn is giving check
* pinned piece: always illegal
*/
if (pos->checkers) {
if (popcount64(pos->checkers) == 1) { /* one checker */
square_t checker = ctz64(pos->checkers);
bitboard_t between = bb_between[king][checker];
if (pinned)
return false;
if (is_enpassant(move)) {
return pawn_push_up(pos->en_passant, them) == checker;
}
bitboard_t between = bb_between[king][checker] | pos->checkers;
return mask(to) & between;
}
return false; /* double check */
@@ -69,7 +78,8 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move)
/* (3) - pinned pieces
* We verify here that pinned piece P stays on line King-P.
*/
if (mask(from) & pos->blockers & pos->bb[us][ALL_PIECES]) {
//if (mask(from) & pos->blockers & pos->bb[us][ALL_PIECES]) {
if (pinned) {
bitboard_t line = bb_line[from][king];
return line & mask(to); /* to is not on pin line */
}

View File

@@ -18,10 +18,11 @@
#include "position.h"
#include "move-gen.h"
#include "move-do.h"
#include "search.h"
#include "attack.h"
//#include "move.h"
//#include "eval.h"
//#include "search.h"
/**
* perft() - Perform perft on position
@@ -36,22 +37,22 @@
*/
u64 perft(pos_t *pos, int depth, int ply)
{
int movetmp = 0, nodes = 0, subnodes, nmove = 0;
int subnodes, nmove = 0;
u64 nodes = 0;
movelist_t pseudo = { .nmoves = 0 }, legal = { .nmoves = 0 };
move_t move;
if (depth == 0)
return 1;
pos->checkers = pos_checkers(pos, pos->turn);
pos->pinners = pos_king_pinners(pos, pos->turn);
pos->blockers = pos_king_blockers(pos, pos->turn, pos->pinners);
pos_set_pinners_blockers(pos);
pos_gen_pseudomoves(pos, &pseudo);
pos_all_legal(pos, &pseudo, &legal);
//for (nmove = 0; nmove < legal.nmoves; ++nmove ) {
while ((move = pos_next_legal(pos, &pseudo, &movetmp)) != MOVE_NONE) {
for (nmove = 0; nmove < legal.nmoves; ++nmove ) {
//while ((move = pos_next_legal(pos, &pseudo, &movetmp)) != MOVE_NONE) {
//printf("depth=%d movetmp=%d\n", depth, movetmp);
state_t state;
move = legal.move[nmove];
move_do(pos, move, &state);
@@ -64,7 +65,45 @@ u64 perft(pos_t *pos, int depth, int ply)
move_undo(pos, move, &state);
}
if (ply == 1)
printf("Total: %d\n", nodes);
printf("\nTotal: %lu\n", nodes);
return nodes;
}
u64 perft2(pos_t *pos, int depth, int ply)
{
int subnodes, nmove = 0;
u64 nodes = 0;
movelist_t pseudo = { .nmoves = 0 };
move_t move;
if (is_in_check(pos, OPPONENT(pos->turn)))
return 0;
if (depth == 0)
return 1;
pos->checkers = pos_checkers(pos, pos->turn);
pos->pinners = pos_king_pinners(pos, pos->turn);
pos->blockers = pos_king_blockers(pos, pos->turn, pos->pinners);
pos_gen_pseudomoves(pos, &pseudo);
//pos_all_legal(pos, &pseudo, &legal);
for (nmove = 0; nmove < pseudo.nmoves; ++nmove ) {
//while ((move = pos_next_legal(pos, &pseudo, &movetmp)) != MOVE_NONE) {
//printf("depth=%d movetmp=%d\n", depth, movetmp);
state_t state;
move = pseudo.move[nmove];
move_do(pos, move, &state);
subnodes = perft2(pos, depth - 1, ply + 1);
if (ply == 1) {
char movestr[8];
printf("%s: %d\n", move_str(movestr, move, 0), subnodes);
}
nodes += subnodes;
move_undo(pos, move, &state);
}
if (ply == 1)
printf("\nTotal: %lu\n", nodes);
return nodes;
}

View File

@@ -20,5 +20,6 @@
//eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color);
u64 perft(pos_t *pos, int depth, int ply);
u64 perft2(pos_t *pos, int depth, int ply);
#endif /* SEARCH_H */