Skip to content

Commit

Permalink
Cleanup board.x evaluate.x search.x transposition.x
Browse files Browse the repository at this point in the history
Final work for #84

ELO   | 3.22 +- 3.63 (95%)
SPRT  | 10.0+0.1s Threads=1 Hash=8MB
LLR   | 2.95 (-2.94, 2.94) [-3.00, 1.00]
Games | N: 14480 W: 3059 L: 2925 D: 8496
http://chess.grantnet.us/viewTest/3447/

NO FUNCTIONAL CHANGE

BENCH : 7,065,845
  • Loading branch information
AndyGrant committed Jun 28, 2019
1 parent d6a2ca3 commit cd88fb0
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 353 deletions.
5 changes: 5 additions & 0 deletions src/bitboards.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ int relativeSquare32(int colour, int sq) {
return 4 * relativeRankOf(colour, sq) + mirrorFile(fileOf(sq));
}

uint64_t squaresOfMatchingColour(int sq) {
assert(0 <= sq && sq < SQUARE_NB);
return testBit(WHITE_SQUARES, sq) ? WHITE_SQUARES : BLACK_SQUARES;
}

int frontmost(int colour, uint64_t b) {
assert(0 <= colour && colour < COLOUR_NB);
return colour == WHITE ? getmsb(b) : getlsb(b);
Expand Down
1 change: 1 addition & 0 deletions src/bitboards.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ int rankOf(int sq);
int relativeRankOf(int colour, int sq);
int square(int rank, int file);
int relativeSquare32(int colour, int sq);
uint64_t squaresOfMatchingColour(int sq);

int frontmost(int colour, uint64_t bb);
int backmost(int colour, uint64_t bb);
Expand Down
116 changes: 55 additions & 61 deletions src/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,60 @@ void printBoard(Board *board) {
printf("\n%s\n\n", fen);
}

int boardHasNonPawnMaterial(Board *board, int turn) {
uint64_t friendly = board->colours[turn];
uint64_t kings = board->pieces[KING];
uint64_t pawns = board->pieces[PAWN];
return (friendly & (kings | pawns)) != friendly;
}

int boardIsDrawn(Board *board, int height) {

// Drawn if any of the three possible cases
return boardDrawnByFiftyMoveRule(board)
|| boardDrawnByRepetition(board, height)
|| boardDrawnByInsufficientMaterial(board);
}

int boardDrawnByFiftyMoveRule(Board *board) {

// Fifty move rule triggered. BUG: We do not account for the case
// when the fifty move rule occurs as checkmate is delivered, which
// should not be considered a drawn position, but a checkmated one.
return board->halfMoveCounter > 99;
}

int boardDrawnByRepetition(Board *board, int height) {

int reps = 0;

// Look through hash histories for our moves
for (int i = board->numMoves - 2; i >= 0; i -= 2) {

// No draw can occur before a zeroing move
if (i < board->numMoves - board->halfMoveCounter)
break;

// Check for matching hash with a two fold after the root,
// or a three fold which occurs in part before the root move
if ( board->history[i] == board->hash
&& (i > board->numMoves - height || ++reps == 2))
return 1;
}

return 0;
}

int boardDrawnByInsufficientMaterial(Board *board) {

// Check for KvK, KvN, KvB, and KvNN.

return !(board->pieces[PAWN] | board->pieces[ROOK] | board->pieces[QUEEN])
&& (!several(board->colours[WHITE]) || !several(board->colours[BLACK]))
&& ( !several(board->pieces[KNIGHT] | board->pieces[BISHOP])
|| (!board->pieces[BISHOP] && popcount(board->pieces[KNIGHT]) <= 2));
}

uint64_t perft(Board *board, int depth) {

Undo undo[1];
Expand Down Expand Up @@ -335,64 +389,4 @@ void runBenchmark(Thread *threads, int depth) {
printf("Time : %dms\n", (int)(end - start));
printf("Nodes : %"PRIu64"\n", nodes);
printf("NPS : %d\n", (int)(nodes / ((end - start) / 1000.0)));
}

int boardIsDrawn(Board *board, int height) {

// Drawn if any of the three possible cases
return drawnByFiftyMoveRule(board)
|| drawnByRepetition(board, height)
|| drawnByInsufficientMaterial(board);
}

int drawnByFiftyMoveRule(Board *board) {

// Fifty move rule triggered. BUG: We do not account for the case
// when the fifty move rule occurs as checkmate is delivered, which
// should not be considered a drawn position, but a checkmated one.
return board->halfMoveCounter > 99;
}

int drawnByRepetition(Board *board, int height) {

int reps = 0;

// Look through hash histories for our moves
for (int i = board->numMoves - 2; i >= 0; i -= 2) {

// No draw can occur before a zeroing move
if (i < board->numMoves - board->halfMoveCounter)
break;

// Check for matching hash with a two fold after the root,
// or a three fold which occurs in part before the root move
if ( board->history[i] == board->hash
&& (i > board->numMoves - height || ++reps == 2))
return 1;
}

return 0;
}

int drawnByInsufficientMaterial(Board *board) {

// No draw by insufficient material with pawns, rooks, or queens
if (board->pieces[PAWN] | board->pieces[ROOK] | board->pieces[QUEEN])
return 0;

// Check for KvK, KvN, KvB, and KvKNN (White is K)
if (!several(board->colours[WHITE])) {
if ( !several(board->pieces[KNIGHT] | board->pieces[BISHOP])
||(!board->pieces[BISHOP] && popcount(board->pieces[KNIGHT]) <= 2))
return 1;
}

// Check for KvK, KvN, KvB, and KvKNN (Black is K)
else if (!several(board->colours[BLACK])) {
if ( !several(board->pieces[KNIGHT] | board->pieces[BISHOP])
||(!board->pieces[BISHOP] && popcount(board->pieces[KNIGHT]) <= 2))
return 1;
}

return 0;
}
}
13 changes: 6 additions & 7 deletions src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ struct Undo {
};

void squareToString(int sq, char *str);

void boardFromFEN(Board *board, const char *fen, int chess960);
void boardToFEN(Board *board, char *fen);

void printBoard(Board *board);
int boardHasNonPawnMaterial(Board *board, int turn);
int boardIsDrawn(Board *board, int height);
int boardDrawnByFiftyMoveRule(Board *board);
int boardDrawnByRepetition(Board *board, int height);
int boardDrawnByInsufficientMaterial(Board *board);

uint64_t perft(Board *board, int depth);
void runBenchmark(Thread *threads, int depth);

int boardIsDrawn(Board *board, int height);
int drawnByFiftyMoveRule(Board *board);
int drawnByRepetition(Board *board, int height);
int drawnByInsufficientMaterial(Board *board);
Loading

0 comments on commit cd88fb0

Please sign in to comment.