ChessBoolean chess_position_move_is_legal(const ChessPosition* position, ChessMove move) { ChessMoveGenerator generator; ChessMove legalMove; chess_move_generator_init(&generator, position); while ((legalMove = chess_move_generator_next(&generator))) { if (legalMove == move) return CHESS_TRUE; } return CHESS_FALSE; }
ChessResult chess_position_check_result(const ChessPosition* position) { ChessMoveGenerator generator; chess_move_generator_init(&generator, position); if (chess_move_generator_next(&generator) > 0) return CHESS_RESULT_NONE; if (!chess_position_is_check(position)) return CHESS_RESULT_DRAW; /* Stalemate */ return (position->to_move == CHESS_COLOR_WHITE) ? CHESS_RESULT_BLACK_WINS : CHESS_RESULT_WHITE_WINS; }
static void list_moves(const ChessGameIterator* iter) { ChessMoveGenerator generator; ChessMove move; char buf[16]; chess_move_generator_init(&generator, &iter->position); while ((move = chess_move_generator_next(&generator))) { chess_print_move_san(move, &iter->position, buf); printf("%s ", buf); } putchar('\n'); }
void chess_generate_moves(const ChessPosition* position, ChessArray* moves) { ChessMoveGenerator generator; ChessMove move; chess_move_generator_init(&generator, position); while ((move = chess_move_generator_next(&generator))) { if (!move_is_legal(position, move)) continue; chess_array_push(moves, &move); } }