add forgotten changes in bitboard-switch branch (git is so... difficult)
This commit is contained in:
@@ -103,18 +103,19 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move)
|
||||
/**
|
||||
* pos_next_legal() - get next legal move in position.
|
||||
* @pos: position
|
||||
* @start: &int, starting position in move list
|
||||
* @movelist: &pseudo-legal movelist
|
||||
* @start: &int, starting position in @movelist
|
||||
*
|
||||
* Get next valid move in @pos move list, from move @start, or MOVE_NONE.
|
||||
* @start is set to next non-checked move in pseudo-legal list.
|
||||
* Position pseudo-legal moves must be already calculated before calling this function.
|
||||
*
|
||||
* @return: move, or -1 if no move.
|
||||
* @return: move, or MOVE_NONE if no move.
|
||||
*/
|
||||
move_t pos_next_legal(const pos_t *pos, int *start)
|
||||
move_t pos_next_legal(const pos_t *pos, movelist_t *movelist, int *start)
|
||||
{
|
||||
const int nmoves = pos->moves.nmoves;
|
||||
const move_t *moves = pos->moves.move;
|
||||
const int nmoves = movelist->nmoves;
|
||||
const move_t *moves = movelist->move;
|
||||
move_t move;
|
||||
|
||||
while (*start < nmoves) {
|
||||
@@ -127,20 +128,20 @@ move_t pos_next_legal(const pos_t *pos, int *start)
|
||||
/**
|
||||
* pos_all_legal() - get the list of legal moves from pseudo-legal.
|
||||
* @pos: position
|
||||
* @dest: destination &movelist_t
|
||||
* @movelist: &pseudo-legal movelist_t
|
||||
* @dest: &destination movelist_t
|
||||
*
|
||||
* The pseudo-legal moves must be already calculated before calling this function.
|
||||
* No check is done on @dest limits.
|
||||
*
|
||||
* @Return: @dest
|
||||
*/
|
||||
movelist_t *pos_all_legal(const pos_t *pos, movelist_t *dest)
|
||||
movelist_t *pos_all_legal(const pos_t *pos, movelist_t *movelist, movelist_t *dest)
|
||||
{
|
||||
int tmp = dest->nmoves = 0;
|
||||
move_t move;
|
||||
//int tmp = 0;
|
||||
|
||||
while ((move = pos_next_legal(pos, &tmp)) != MOVE_NONE)
|
||||
while ((move = pos_next_legal(pos, movelist, &tmp)) != MOVE_NONE)
|
||||
dest->move[dest->nmoves++] = move;
|
||||
return dest;
|
||||
}
|
||||
@@ -148,9 +149,10 @@ movelist_t *pos_all_legal(const pos_t *pos, movelist_t *dest)
|
||||
/**
|
||||
* pos_gen_pseudomoves() - generate position pseudo-legal moves
|
||||
* @pos: position
|
||||
* @movelist: &movelist_t array to store pseudo-moves
|
||||
*
|
||||
* Generate all @pos pseudo moves for player-to-move.
|
||||
* The @pos->moves table is filled with the moves.
|
||||
* @movelist is filled with the moves.
|
||||
*
|
||||
* Only a few validity checks are done here (i.e. moves are not generated):
|
||||
* - castling, if king is in check
|
||||
@@ -167,7 +169,7 @@ movelist_t *pos_all_legal(const pos_t *pos, movelist_t *dest)
|
||||
*
|
||||
* @Return: The total number of moves.
|
||||
*/
|
||||
int pos_gen_pseudomoves(pos_t *pos)
|
||||
int pos_gen_pseudomoves(pos_t *pos, movelist_t *movelist)
|
||||
{
|
||||
color_t us = pos->turn;
|
||||
color_t them = OPPONENT(us);
|
||||
@@ -181,43 +183,43 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
bitboard_t movebits, from_pawns;
|
||||
bitboard_t tmp1, tmp2;
|
||||
|
||||
move_t *moves = pos->moves.move;
|
||||
int nmoves = pos->moves.nmoves;
|
||||
move_t *moves = movelist->move;
|
||||
int *nmoves = &movelist->nmoves;
|
||||
int from, to;
|
||||
|
||||
bug_on(nmoves != 0);
|
||||
*nmoves = 0;
|
||||
|
||||
/* king - MUST BE FIRST (we stop if doubler check) */
|
||||
from = pos->king[us];
|
||||
movebits = bb_king_moves(not_my_pieces, from);
|
||||
bit_for_each64(to, tmp1, movebits & empty) {
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
moves[(*nmoves)++] = move_make(from, to);
|
||||
}
|
||||
bit_for_each64(to, tmp1, movebits & enemy_pieces) {
|
||||
moves[nmoves++] = move_make_capture(from, to);
|
||||
moves[(*nmoves)++] = move_make_capture(from, to);
|
||||
}
|
||||
|
||||
if (popcount64(pos->checkers) > 1) /* double check, we stop here */
|
||||
return (pos->moves.nmoves = nmoves);
|
||||
return *nmoves;
|
||||
|
||||
/* sliding pieces */
|
||||
bit_for_each64(from, tmp1, pos->bb[us][BISHOP] | pos->bb[us][QUEEN]) {
|
||||
movebits = hyperbola_bishop_moves(occ, from) & not_my_pieces;
|
||||
bit_for_each64(to, tmp2, movebits & empty) {
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
moves[(*nmoves)++] = move_make(from, to);
|
||||
}
|
||||
bit_for_each64(to, tmp2, movebits & enemy_pieces) {
|
||||
moves[nmoves++] = move_make_capture(from, to);
|
||||
moves[(*nmoves)++] = move_make_capture(from, to);
|
||||
}
|
||||
}
|
||||
bit_for_each64(from, tmp1, pos->bb[us][ROOK] | pos->bb[us][QUEEN]) {
|
||||
// printf("rook=%d/%s\n", from, sq_to_string(from));
|
||||
movebits = hyperbola_rook_moves(occ, from) & not_my_pieces;
|
||||
bit_for_each64(to, tmp2, movebits & empty) {
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
moves[(*nmoves)++] = move_make(from, to);
|
||||
}
|
||||
bit_for_each64(to, tmp2, movebits & enemy_pieces) {
|
||||
moves[nmoves++] = move_make_capture(from, to);
|
||||
moves[(*nmoves)++] = move_make_capture(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,10 +227,10 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
bit_for_each64(from, tmp1, pos->bb[us][KNIGHT]) {
|
||||
movebits = bb_knight_moves(not_my_pieces, from);
|
||||
bit_for_each64(to, tmp2, movebits & empty) {
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
moves[(*nmoves)++] = move_make(from, to);
|
||||
}
|
||||
bit_for_each64(to, tmp2, movebits & enemy_pieces) {
|
||||
moves[nmoves++] = move_make_capture(from, to);
|
||||
moves[(*nmoves)++] = move_make_capture(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,13 +243,13 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push_up(to, them); /* reverse push */
|
||||
//printf("push %d->%d %s->%s", from, to, sq_to_string(from), sq_to_string(to));
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
moves[(*nmoves)++] = move_make(from, to);
|
||||
}
|
||||
/* possible second push */
|
||||
movebits = pawn_shift_up(movebits & rel_rank3, us) & empty;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push_up(pawn_push_up(to, them), them);
|
||||
moves[nmoves++] = move_make_flags(from, to, M_DPUSH);
|
||||
moves[(*nmoves)++] = move_make_flags(from, to, M_DPUSH);
|
||||
}
|
||||
|
||||
/* pawn: ranks 2-6 captures left */
|
||||
@@ -255,14 +257,14 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
movebits = pawn_shift_upleft(from_pawns, us) & enemy_pieces;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push_upleft(to, them); /* reverse capture */
|
||||
moves[nmoves++] = move_make_capture(from, to);
|
||||
moves[(*nmoves)++] = move_make_capture(from, to);
|
||||
}
|
||||
/* pawn: ranks 2-6 captures right */
|
||||
from_pawns = pos->bb[us][PAWN] & ~rel_rank7; // & ~rel_fileh;
|
||||
movebits = pawn_shift_upright(from_pawns, us) & enemy_pieces;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push_upright(to, them);
|
||||
moves[nmoves++] = move_make_capture(from, to);
|
||||
moves[(*nmoves)++] = move_make_capture(from, to);
|
||||
}
|
||||
|
||||
/* pawn: en-passant */
|
||||
@@ -271,7 +273,7 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
from_pawns = pos->bb[us][PAWN]
|
||||
& (pawn_shift_upleft(movebits, them) | pawn_shift_upright(movebits, them));
|
||||
bit_for_each64(from, tmp1, from_pawns) {
|
||||
moves[nmoves++] = move_make_enpassant(from, to);
|
||||
moves[(*nmoves)++] = move_make_enpassant(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,30 +281,30 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
movebits = pawn_shift_up(pos->bb[us][PAWN] & rel_rank7, us) & empty;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push_up(to, them); /* reverse push */
|
||||
moves[nmoves++] = move_make_promote(from, to, QUEEN);
|
||||
moves[nmoves++] = move_make_promote(from, to, ROOK);
|
||||
moves[nmoves++] = move_make_promote(from, to, BISHOP);
|
||||
moves[nmoves++] = move_make_promote(from, to, KNIGHT);
|
||||
moves[(*nmoves)++] = move_make_promote(from, to, QUEEN);
|
||||
moves[(*nmoves)++] = move_make_promote(from, to, ROOK);
|
||||
moves[(*nmoves)++] = move_make_promote(from, to, BISHOP);
|
||||
moves[(*nmoves)++] = move_make_promote(from, to, KNIGHT);
|
||||
}
|
||||
/* pawn promotion: rank 7 captures left */
|
||||
from_pawns = pos->bb[us][PAWN] & rel_rank7; // & ~rel_filea;
|
||||
movebits = pawn_shift_upleft(from_pawns, us) & enemy_pieces;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push_upleft(to, them); /* reverse capture */
|
||||
moves[nmoves++] = move_make_promote_capture(from, to, QUEEN);
|
||||
moves[nmoves++] = move_make_promote_capture(from, to, ROOK);
|
||||
moves[nmoves++] = move_make_promote_capture(from, to, BISHOP);
|
||||
moves[nmoves++] = move_make_promote_capture(from, to, KNIGHT);
|
||||
moves[(*nmoves)++] = move_make_promote_capture(from, to, QUEEN);
|
||||
moves[(*nmoves)++] = move_make_promote_capture(from, to, ROOK);
|
||||
moves[(*nmoves)++] = move_make_promote_capture(from, to, BISHOP);
|
||||
moves[(*nmoves)++] = move_make_promote_capture(from, to, KNIGHT);
|
||||
}
|
||||
/* pawn: rank 7 captures right */
|
||||
from_pawns = pos->bb[us][PAWN] & rel_rank7; // & ~rel_fileh;
|
||||
movebits = pawn_shift_upright(from_pawns, us) & enemy_pieces;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push_upright(to, them); /* reverse capture */
|
||||
moves[nmoves++] = move_make_promote_capture(from, to, QUEEN);
|
||||
moves[nmoves++] = move_make_promote_capture(from, to, ROOK);
|
||||
moves[nmoves++] = move_make_promote_capture(from, to, BISHOP);
|
||||
moves[nmoves++] = move_make_promote_capture(from, to, KNIGHT);
|
||||
moves[(*nmoves)++] = move_make_promote_capture(from, to, QUEEN);
|
||||
moves[(*nmoves)++] = move_make_promote_capture(from, to, ROOK);
|
||||
moves[(*nmoves)++] = move_make_promote_capture(from, to, BISHOP);
|
||||
moves[(*nmoves)++] = move_make_promote_capture(from, to, KNIGHT);
|
||||
}
|
||||
|
||||
/* castle - Attention ! Castling flags are assumed correct
|
||||
@@ -319,14 +321,14 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
bitboard_t occmask = rel_rank1 & (FILE_Fbb | FILE_Gbb);
|
||||
if (!(occ & occmask) &&
|
||||
!sq_attackers(pos, occ, from+1, them)) { /* f1/f8 */
|
||||
moves[nmoves++] = move_make_flags(from, from + 2, M_CASTLE_K);
|
||||
moves[(*nmoves)++] = move_make_flags(from, from + 2, M_CASTLE_K);
|
||||
}
|
||||
}
|
||||
if (can_ooo(pos->castle, us)) {
|
||||
bitboard_t occmask = rel_rank1 & (FILE_Bbb | FILE_Cbb | FILE_Dbb);
|
||||
if (!(occ & occmask) &&
|
||||
!sq_attackers(pos, occ, from-1, them)) { /* d1/d8 */
|
||||
moves[nmoves++] = move_make_flags(from, from - 2, M_CASTLE_Q);
|
||||
moves[(*nmoves)++] = move_make_flags(from, from - 2, M_CASTLE_Q);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -341,5 +343,5 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
* add function per piece, and type, for easier debug
|
||||
*
|
||||
*/
|
||||
return (pos->moves.nmoves = nmoves);
|
||||
return *nmoves;
|
||||
}
|
||||
|
@@ -22,8 +22,8 @@
|
||||
#include "move.h"
|
||||
|
||||
bool pseudo_is_legal(const pos_t *pos, const move_t move);
|
||||
move_t pos_next_legal(const pos_t *pos, int *start);
|
||||
movelist_t *pos_all_legal(const pos_t *pos, movelist_t *dest);
|
||||
int pos_gen_pseudomoves(pos_t *pos);
|
||||
move_t pos_next_legal(const pos_t *pos, movelist_t *movelist, int *start);
|
||||
movelist_t *pos_all_legal(const pos_t *pos, movelist_t *movelist, movelist_t *dest);
|
||||
int pos_gen_pseudomoves(pos_t *pos, movelist_t *movelist);
|
||||
|
||||
#endif /* MOVEGEN_H */
|
||||
|
@@ -35,8 +35,8 @@
|
||||
typedef s32 move_t;
|
||||
|
||||
/* special move_t values */
|
||||
#define MOVE_NONE (-1)
|
||||
#define MOVE_NULL (0) /* hack: from = to = A1 */
|
||||
#define MOVE_NONE ((move_t) -1)
|
||||
#define MOVE_NULL ((move_t) 0) /* hack: from = to = A1 */
|
||||
|
||||
enum {
|
||||
M_OFF_FROM = 0,
|
||||
|
@@ -89,7 +89,7 @@ pos_t *pos_clear(pos_t *pos)
|
||||
pos->castle = 0;
|
||||
pos->clock_50 = 0;
|
||||
pos->plycount = 0;
|
||||
pos->captured = NO_PIECE;
|
||||
//pos->captured = NO_PIECE;
|
||||
|
||||
for (square_t sq = A1; sq <= H8; ++sq)
|
||||
pos->board[sq] = EMPTY;
|
||||
@@ -105,8 +105,7 @@ pos_t *pos_clear(pos_t *pos)
|
||||
pos->pinners = 0;
|
||||
pos->blockers = 0;
|
||||
|
||||
//pos->moves.curmove = 0;
|
||||
pos->moves.nmoves = 0;
|
||||
//pos->moves.nmoves = 0;
|
||||
return pos;
|
||||
}
|
||||
|
||||
@@ -134,8 +133,8 @@ bool pos_cmp(const pos_t *pos1, const pos_t *pos2)
|
||||
goto end;
|
||||
if (warn_on(_cmpf(plycount)))
|
||||
goto end;
|
||||
if (warn_on(_cmpf(captured)))
|
||||
goto end;
|
||||
//if (warn_on(_cmpf(captured)))
|
||||
// goto end;
|
||||
|
||||
for (square_t sq = A1; sq <= H8; ++sq)
|
||||
if (warn_on(_cmpf(board[sq])))
|
||||
@@ -157,11 +156,13 @@ bool pos_cmp(const pos_t *pos1, const pos_t *pos2)
|
||||
if (warn_on(_cmpf(blockers)))
|
||||
goto end;
|
||||
|
||||
if (warn_on(_cmpf(moves.nmoves)))
|
||||
goto end;
|
||||
for (int i = 0; i < pos1->moves.nmoves; ++i)
|
||||
if (warn_on(_cmpf(moves.move[i])))
|
||||
goto end;
|
||||
/*
|
||||
* if (warn_on(_cmpf(moves.nmoves)))
|
||||
* goto end;
|
||||
* for (int i = 0; i < pos1->moves.nmoves; ++i)
|
||||
* if (warn_on(_cmpf(moves.move[i])))
|
||||
* goto end;
|
||||
*/
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
|
@@ -41,7 +41,10 @@ typedef struct __pos_s {
|
||||
castle_rights_t castle;
|
||||
u16 clock_50;
|
||||
u16 plycount; /* plies so far, start from 1 */
|
||||
piece_t captured; /* only for move_undo */
|
||||
//piece_t captured; /* only for move_undo */
|
||||
bitboard_t checkers; /* opponent checkers */
|
||||
bitboard_t pinners; /* opponent pinners */
|
||||
bitboard_t blockers; /* pieces blocking pin */
|
||||
);
|
||||
|
||||
piece_t board[BOARDSIZE];
|
||||
@@ -49,10 +52,7 @@ typedef struct __pos_s {
|
||||
bitboard_t bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */
|
||||
//bitboard_t controlled[2]; /* unsure */
|
||||
square_t king[2]; /* dup with bb, faster retrieval */
|
||||
bitboard_t checkers; /* opponent checkers */
|
||||
bitboard_t pinners; /* opponent pinners */
|
||||
bitboard_t blockers; /* pieces blocking pin */
|
||||
movelist_t moves;
|
||||
//movelist_t moves;
|
||||
} pos_t;
|
||||
|
||||
typedef struct state_s state_t;
|
||||
|
254
src/search.c
254
src/search.c
@@ -1,6 +1,6 @@
|
||||
/* search.c - search good moves.
|
||||
*
|
||||
* Copyright (C) 2023 Bruno Raoult ("br")
|
||||
* Copyright (C) 2023-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -11,14 +11,62 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <br.h>
|
||||
#include <list.h>
|
||||
#include "debug.h"
|
||||
#include "brlib.h"
|
||||
|
||||
#include "move.h"
|
||||
#include "eval.h"
|
||||
#include "search.h"
|
||||
#include "position.h"
|
||||
#include "move-gen.h"
|
||||
#include "move-do.h"
|
||||
|
||||
//#include "move.h"
|
||||
//#include "eval.h"
|
||||
//#include "search.h"
|
||||
|
||||
/**
|
||||
* perft() - Perform perft on position
|
||||
* @pos: &position to search
|
||||
* @depth: Wanted depth.
|
||||
* @ply: Depth level where perft is consolidated.
|
||||
*
|
||||
* Print perftCalculate the negamax value of @pos. This is an extensive search, with
|
||||
* absolutely no cutoff.
|
||||
*
|
||||
* @return: The @pos negamax evaluation.
|
||||
*/
|
||||
u64 perft(pos_t *pos, int depth, int ply)
|
||||
{
|
||||
int movetmp = 0, nodes = 0, subnodes, nmove = 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_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) {
|
||||
|
||||
state_t state;
|
||||
move = legal.move[nmove];
|
||||
move_do(pos, move, &state);
|
||||
subnodes = perft(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("Total: %d\n", nodes);
|
||||
return nodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* negamax() - search position negamax.
|
||||
@@ -31,32 +79,34 @@
|
||||
*
|
||||
* @return: The @pos negamax evaluation.
|
||||
*/
|
||||
eval_t negamax(pos_t *pos, int depth, int color)
|
||||
{
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
eval_t best = EVAL_MIN, score;
|
||||
|
||||
pos->node_count++;
|
||||
if (depth == 0) {
|
||||
moves_gen_all_nomoves(pos);
|
||||
score = eval(pos) * color;
|
||||
return score;
|
||||
}
|
||||
moves_gen_all(pos);
|
||||
list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
newpos = move_do(pos, move);
|
||||
score = -negamax(newpos, depth - 1, -color);
|
||||
pos->node_count += newpos->node_count;
|
||||
move->negamax = score;
|
||||
if (score > best) {
|
||||
best = score;
|
||||
pos->bestmove = move;
|
||||
}
|
||||
move_undo(newpos, move);
|
||||
}
|
||||
return best;
|
||||
}
|
||||
/*
|
||||
* eval_t negamax(pos_t *pos, int depth, int color)
|
||||
* {
|
||||
* move_t *move;
|
||||
* pos_t *newpos;
|
||||
* eval_t best = EVAL_MIN, score;
|
||||
*
|
||||
* pos->node_count++;
|
||||
* if (depth == 0) {
|
||||
* moves_gen_all_nomoves(pos);
|
||||
* score = eval(pos) * color;
|
||||
* return score;
|
||||
* }
|
||||
* moves_gen_all(pos);
|
||||
* list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
* newpos = move_do(pos, move);
|
||||
* score = -negamax(newpos, depth - 1, -color);
|
||||
* pos->node_count += newpos->node_count;
|
||||
* move->negamax = score;
|
||||
* if (score > best) {
|
||||
* best = score;
|
||||
* pos->bestmove = move;
|
||||
* }
|
||||
* move_undo(newpos, move);
|
||||
* }
|
||||
* return best;
|
||||
* }
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
@@ -74,74 +124,76 @@ eval_t negamax(pos_t *pos, int depth, int color)
|
||||
*
|
||||
* @return: The @pos PVS evaluation.
|
||||
*/
|
||||
eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color)
|
||||
{
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
eval_t score = EVAL_INVALID;
|
||||
bool firstchild = true;
|
||||
|
||||
pos->node_count++;
|
||||
|
||||
if (depth == 0) {
|
||||
//return quiesce(p, alpha, beta); /* leaf node */
|
||||
moves_gen_all_nomoves(pos);
|
||||
score = eval(pos) * color;
|
||||
log_f(2, "Terminal: depth=%d ", depth);
|
||||
log_f(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
|
||||
return score;
|
||||
}
|
||||
|
||||
moves_gen_all(pos);
|
||||
//moves_print(pos, M_PR_EVAL);
|
||||
/* do the full search for first child */
|
||||
//move = list_first_entry_or_null(&pos->moves[pos->turn], move_t, list);
|
||||
|
||||
list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
newpos = move_do(pos, move);
|
||||
log(2, "%.*s", 5 - depth, " ");
|
||||
if (firstchild) { /* first child */
|
||||
score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
|
||||
log_f(2, "First child depth=%d move=", depth);
|
||||
//move_print(0, move, 0);
|
||||
log(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
|
||||
pos->bestmove = move;
|
||||
} else {
|
||||
/* search with a null window */
|
||||
score = -pvs(newpos, depth - 1, -alpha - 1, -alpha, -color);
|
||||
log_f(2, "Other child depth=%d move=", depth);
|
||||
//move_print(0, move, 0);
|
||||
log_f(2, "score=%d alpha=%d beta=%d ", score, alpha, beta);
|
||||
/* for fail-soft: if (score > alpha && score < beta) */
|
||||
if (score > alpha) {
|
||||
/* if failed high, do a full re-search */
|
||||
log_f(2, "doing full search.");
|
||||
score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
|
||||
}
|
||||
log(2, "\n");
|
||||
}
|
||||
pos->node_count += newpos->node_count;
|
||||
move_undo(newpos, move);
|
||||
if (score >= beta) { /* fail-hard hard beta cut-off */
|
||||
log(2, "%.*s", 5 - depth, " ");
|
||||
log_f(2, "depth=%d score=%d alpha=%d beta=%d beta cut-off.\n",
|
||||
depth, score, alpha, beta);
|
||||
return beta;
|
||||
}
|
||||
if (score > alpha) {
|
||||
log(2, "%.*s", 5 - depth, " ");
|
||||
log_f(2, "depth=%d setting new alpha from %d to %d\n",
|
||||
depth, alpha, score);
|
||||
alpha = score;
|
||||
pos->bestmove = move;
|
||||
}
|
||||
move->pos = NULL;
|
||||
move->negamax = score;
|
||||
firstchild = false;
|
||||
}
|
||||
|
||||
return alpha;
|
||||
}
|
||||
/*
|
||||
* eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color)
|
||||
* {
|
||||
* move_t *move;
|
||||
* pos_t *newpos;
|
||||
* eval_t score = EVAL_INVALID;
|
||||
* bool firstchild = true;
|
||||
*
|
||||
* pos->node_count++;
|
||||
*
|
||||
* if (depth == 0) {
|
||||
* //return quiesce(p, alpha, beta); /\* leaf node *\/
|
||||
* moves_gen_all_nomoves(pos);
|
||||
* score = eval(pos) * color;
|
||||
* log_f(2, "Terminal: depth=%d ", depth);
|
||||
* log_f(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
|
||||
* return score;
|
||||
* }
|
||||
*
|
||||
* moves_gen_all(pos);
|
||||
* //moves_print(pos, M_PR_EVAL);
|
||||
* /\* do the full search for first child *\/
|
||||
* //move = list_first_entry_or_null(&pos->moves[pos->turn], move_t, list);
|
||||
*
|
||||
* list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
* newpos = move_do(pos, move);
|
||||
* log(2, "%.*s", 5 - depth, " ");
|
||||
* if (firstchild) { /\* first child *\/
|
||||
* score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
|
||||
* log_f(2, "First child depth=%d move=", depth);
|
||||
* //move_print(0, move, 0);
|
||||
* log(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
|
||||
* pos->bestmove = move;
|
||||
* } else {
|
||||
* /\* search with a null window *\/
|
||||
* score = -pvs(newpos, depth - 1, -alpha - 1, -alpha, -color);
|
||||
* log_f(2, "Other child depth=%d move=", depth);
|
||||
* //move_print(0, move, 0);
|
||||
* log_f(2, "score=%d alpha=%d beta=%d ", score, alpha, beta);
|
||||
* /\* for fail-soft: if (score > alpha && score < beta) *\/
|
||||
* if (score > alpha) {
|
||||
* /\* if failed high, do a full re-search *\/
|
||||
* log_f(2, "doing full search.");
|
||||
* score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
|
||||
* }
|
||||
* log(2, "\n");
|
||||
* }
|
||||
* pos->node_count += newpos->node_count;
|
||||
* move_undo(newpos, move);
|
||||
* if (score >= beta) { /\* fail-hard hard beta cut-off *\/
|
||||
* log(2, "%.*s", 5 - depth, " ");
|
||||
* log_f(2, "depth=%d score=%d alpha=%d beta=%d beta cut-off.\n",
|
||||
* depth, score, alpha, beta);
|
||||
* return beta;
|
||||
* }
|
||||
* if (score > alpha) {
|
||||
* log(2, "%.*s", 5 - depth, " ");
|
||||
* log_f(2, "depth=%d setting new alpha from %d to %d\n",
|
||||
* depth, alpha, score);
|
||||
* alpha = score;
|
||||
* pos->bestmove = move;
|
||||
* }
|
||||
* move->pos = NULL;
|
||||
* move->negamax = score;
|
||||
* firstchild = false;
|
||||
* }
|
||||
*
|
||||
* return alpha;
|
||||
* }
|
||||
*/
|
||||
|
||||
/*
|
||||
* int negascout (pos_t *pos, int depth, int alpha, int beta )
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/* search.h - search for perfect move.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
#include "position.h"
|
||||
|
||||
eval_t negamax(pos_t *pos, int depth, int color);
|
||||
eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color);
|
||||
//eval_t negamax(pos_t *pos, int depth, int color);
|
||||
//eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color);
|
||||
|
||||
u64 perft(pos_t *pos, int depth, int ply);
|
||||
|
||||
#endif /* SEARCH_H */
|
||||
|
Reference in New Issue
Block a user