diff --git a/src/brchess.c b/src/brchess.c index 0b5e6cd..a9d76a4 100644 --- a/src/brchess.c +++ b/src/brchess.c @@ -29,6 +29,7 @@ #include "move.h" #include "fen.h" #include "eval.h" +#include "search.h" struct command { char *name; /* User printable name */ @@ -62,6 +63,8 @@ int do_eval(pos_t *, char*); int do_move(pos_t *, char*); int do_quit(pos_t *, char*); int do_debug(pos_t *, char*); +int do_depth(pos_t *, char*); +int do_search(pos_t *, char*); struct command commands[] = { { "help", do_help, "Display this text" }, @@ -78,10 +81,13 @@ struct command commands[] = { { "eval", do_eval, "Eval current position" }, { "do_move", do_move, "execute nth move on current position" }, { "debug", do_debug, "Set log level to LEVEL" }, + { "depth", do_depth, "Set search depth to N" }, + { "search", do_search, "Search best move" }, { NULL, (int(*)()) NULL, NULL } }; static int done=0; +static int depth=1; int brchess(pos_t *pos) { @@ -377,7 +383,7 @@ int do_debug(__unused pos_t *pos, __unused char *arg) not present. */ int do_help(__unused pos_t *pos, __unused char *arg) { - register int i; + int i; int printed = 0; for (i = 0; commands[i].name; i++) { @@ -407,6 +413,23 @@ int do_help(__unused pos_t *pos, __unused char *arg) return 0; } +int do_depth(__unused pos_t *pos, char *arg) +{ + depth = atoi(arg); + printf("depth = %d\n", depth); + return 1; + +} + +int do_search(pos_t *pos, __unused char *arg) +{ + printf("++++++++\nnegamax=%d\n", negamax(pos, depth, pos->turn==WHITE? 1:-1)); + printf("best="); + move_print(0, pos->bestmove, 0); + printf("score=%d\n", pos->bestmove->negamax); + return 1; +} + #ifdef BIN_brchess /** main() * options: @@ -430,7 +453,7 @@ int main(int ac, char **av) moves_pool_init(); pos_pool_init(); pos = pos_get(); - debug_init(1, stderr); + debug_init(1, stderr, true); while ((opt = getopt(ac, av, "d:f:")) != -1) { switch (opt) { diff --git a/src/search.c b/src/search.c index 65e86c2..6d78d4f 100644 --- a/src/search.c +++ b/src/search.c @@ -24,28 +24,41 @@ * negamax() - the negamax tree. * */ -eval_t negamax(pos_t *pos, int depth) +eval_t negamax(pos_t *pos, int depth, int color) { - move_t *move, *bestmove; + move_t *move, bestmove; pos_t *newpos; eval_t best = EVAL_MIN, score; + printf("depth=%d\n", depth); moves_gen_all(pos); - if (depth == 0) - return eval(pos) * pos->turn == WHITE? 1: -1; - + //pos_check(pos); + if (depth == 0) { + score = eval(pos); + printf("evalnega=%d turn=%d color=%d", score, pos->turn, color); + score *= color; + printf(" --> evalnega=%d\n", score); + return score; + } + moves_print(pos, 0); list_for_each_entry(move, &pos->moves[pos->turn], list) { newpos = move_do(pos, move); - score = -negamax(newpos, depth - 1 ); - if(score > best) { + score = -negamax(newpos, depth - 1, -color); + move->negamax = score; + printf("move="); + move_print(0, move, 0); + printf("score=%d\n", score); + + if (score > best) { best = score; - bestmove = move; + pos->bestmove = move; # ifdef DEBUG_SEARCH - log_f(2, "depth=%d best move=", best); - move_print(0, bestmove, M_PR_LONG); - log_f(2, " eval=%d\n", best); + printf("depth=%d best move=", depth); + move_print(0, &bestmove, 0); + printf(" eval=%d\n", best); # endif } + move_undo(newpos, move); } return best; } diff --git a/src/search.h b/src/search.h index 4269f78..50b89c9 100644 --- a/src/search.h +++ b/src/search.h @@ -20,6 +20,6 @@ #define EVAL_MIN INT_MIN #define EVAL_MAX INT_MAX -eval_t negamax(pos_t *pos, int depth); +eval_t negamax(pos_t *pos, int depth, int color); #endif /* SEARCH_H */