/* Function returns 1 if opponent's king is checked * c = 1 (white) or -1 (black) */ int is_check(board * brd, int c) { move * t = malloc(sizeof(move)); init_move(&t); generate_all_moves(c, brd, &t); move * t_ = t; while(t_->next != NULL) { if( t_->x * c == -6) { free_moves(&t); return 1; } t_=t_->next; } free_moves(&t); return 0; }
/* Returns 1 if player c has valid moves left. * c = 1 (white) or -1 (black) * If king is in check and no valid moves left, that implies checkmate */ int has_moves(board * brd, int c) { move * t = malloc(sizeof(move)); init_move(&t); generate_all_moves(c, brd, &t); move * t_ = t; while(t_->next != NULL) { if(apply_move(brd, &t_, c)) { undo_move(brd, &t_); free_moves(&t); return 1; } t_ = t_->next; } free_moves(&t); return 0; }
static void free_stack(t_stack *stack) { t_ilist *tmp; t_ilist *tmp_bis; tmp = stack->a_begin; while (tmp) { tmp_bis = tmp->next; free(tmp); tmp = tmp_bis; } tmp = stack->b_begin; while (tmp) { tmp_bis = tmp->next; free(tmp); tmp = tmp_bis; } free_moves(stack); free(stack); }