void engine_make_move () { byte *move; movstack_trunc (); cancel_move = FALSE; move = engine_search (&cur_pos); if (cancel_move) return; if (!move) { move_fwrite_nak ("Nice try", engine_fout); return; } movstack_push (cur_pos.board, move); if (game_stateful) { void *newstate = game_newstate (&cur_pos, move); statestack_push (newstate); cur_pos.state = statestack_peek (); } move_apply (cur_pos.board, move); cur_pos.num_moves++; move_fwrite_ack (move, engine_fout); cur_pos.player = cur_pos.player == WHITE ? BLACK : WHITE; }
void engine_back_move () { byte *move = movstack_back (); if (game_stateful) cur_pos.state = statestack_back (); if (!move) { move_fwrite_nak (NULL, engine_fout); return; } move_apply (cur_pos.board, move); cur_pos.num_moves--; cur_pos.player = cur_pos.player == WHITE ? BLACK : WHITE; move_fwrite_ack (move, engine_fout); }
void engine_take_move (char *line) { byte *move = move_read (line); movstack_trunc (); movstack_push (cur_pos.board, move); if (game_stateful) { void *newstate = game_newstate (&cur_pos, move); statestack_push (newstate); cur_pos.state = statestack_peek (); } move_apply (cur_pos.board, move); cur_pos.num_moves++; cur_pos.player = cur_pos.player == WHITE ? BLACK : WHITE; }
static void search_create_tree(search_configuration *conf, position *pos, search_node *node, unsigned int depth) { move *m; movelist ml; unsigned int i; /* check parameters */ ASSERT(conf != (search_configuration *)0); ASSERT(pos != (position *)0); ASSERT(node != (search_node *)0); /* update node counter */ conf->result->numnodes++; if (depth < conf->precalcdepth) { switch (move_generate(pos, &ml)) { case MOVESLEFT: { for (i = 0; i < ML_SIZE(ml); i++) { m = &ML_ENTRY(ml, i); move_apply(pos, m); node->children[i] = (search_node *)w_malloc(sizeof(search_node)); memcpy(&(node->children[i]->m), m, sizeof(move)); search_create_tree(conf, pos, node->children[i], depth + 1); move_rollback(pos, m); } node->numchildren = ML_SIZE(ml); break; } case STALEMATE: { node->rating = RATING_INIT_INF; node->numchildren = 0; break; } case CHECKMATE: { if (pos->player == WHITE) { node->rating = RATING_INIT_BLACK; } else { node->rating = RATING_INIT_WHITE; } node->numchildren = 0; break; } default: { /* invalid return value from move generator */ ASSERT(0); } } } else { switch (move_movesleft(pos)) { case MOVESLEFT: { memcpy(&(node->pos), pos, sizeof(position)); node->rating = RATING_INIT_INF; node->nextleaf = conf->nextleaf; conf->nextleaf = node; conf->numleafnodes++; break; } case STALEMATE: { node->rating = RATING_INIT_INF; break; } case CHECKMATE: { if (pos->player == WHITE) { node->rating = RATING_INIT_BLACK; } else { node->rating = RATING_INIT_WHITE; } break; } default: { /* invalid return value from move generator */ ASSERT(0); } } node->numchildren = 0; } }
static rating_t search_mainsearch_recurse(position *pos, unsigned int depth, unsigned int maxdepth, uint64 *numnodes) { move *m; movelist ml; unsigned int i; rating_t r, bestrating; /* check parameters */ ASSERT(pos != (position *)0); ASSERT(depth <= maxdepth); /* update node counter */ (*numnodes)++; if (depth < maxdepth) { switch (move_generate(pos, &ml)) { case MOVESLEFT: { if (pos->player == WHITE) { /* apply moves one by one and go into recursion */ bestrating = RATING_INIT_BLACK; for (i = 0; i < ML_SIZE(ml); i++) { m = &ML_ENTRY(ml, i); move_apply(pos, m); r = search_mainsearch_recurse(pos, depth + 1, maxdepth, numnodes); if (r > bestrating) /* get max */ { bestrating = r; } move_rollback(pos, m); } } else { /* apply moves one by one and go into recursion */ bestrating = RATING_INIT_WHITE; for (i = 0; i < ML_SIZE(ml); i++) { m = &ML_ENTRY(ml, i); move_apply(pos, m); r = search_mainsearch_recurse(pos, depth + 1, maxdepth, numnodes); if (r < bestrating) /* get min */ { bestrating = r; } move_rollback(pos, m); } } RATING_UPDATE(bestrating); return(bestrating); } case STALEMATE: { return(RATING_INIT_INF); } case CHECKMATE: { if (pos->player == WHITE) { return(RATING_INIT_BLACK); } else { return(RATING_INIT_WHITE); } } default: { /* invalid return value from move generator */ ASSERT(0); } } } else { switch (move_movesleft(pos)) { case MOVESLEFT: { return(RATING_INIT_INF); } case STALEMATE: { return(RATING_INIT_INF); } case CHECKMATE: { if (pos->player == WHITE) { return(RATING_INIT_BLACK); } else { return(RATING_INIT_WHITE); } } default: { /* invalid return value from move generator */ ASSERT(0); } } } /* keep compiler happy */ return(RATING_INIT_INF); }