ai* create_ai(board* b, char player_c) { ai* a = (ai*)malloc(sizeof(ai)); future_move *fm = create_future_move(-1,-1); a->moves_len = 0; a->b = board_cp(b); a->player_c = player_c; a->current_player_c = FIRST_PLAYER; compute_moves(fm, a->b, player_c, MAX_DEEP, (player_c == FIRST_PLAYER)); a->moves = fm->moves; a->moves_len = fm->moves_len; free(fm); fm = NULL; return a; }
Pawn::Pawn () { compute_moves (); }
Bishop::Bishop () { compute_moves (); }
int compute_moves(future_move* self, board* b, char player, int deep, short is_ia) { if (deep < 1) { return 0; } int i, moves_c_len = 0, col = -1, row = -1; if (self->points != 0) { /* if AI does not pass the turn */ put_piece_by_colrow(b, self->col, self->row, player); } char **moves_c = NULL; moves_c = (char**)malloc(sizeof(char*)*MAX_POSSIBLE_MOVES); for (i=0; i<MAX_POSSIBLE_MOVES; i++) { moves_c[i] = NULL; } board* b2 = NULL; future_move* fm = NULL; moves_c_len = get_possible_moves(b, player, &moves_c); if (moves_c_len == 0) { /* pass */ fm = create_future_move(-1,-1); ADD_FUTURE_MOVE(self, fm); } else { for (i=0; i<moves_c_len; i++) { convert_square(moves_c[i], &col, &row); /* create a future move with good points/weight and col/row */ fm = create_future_move(col, row); fm->points = play(b, player, moves_c[i]); /* simule the move */ put_piece(b, moves_c[i], EMPTY_C); /* remove the piece */ fm->weight = M_WEIGHT(*fm); ADD_FUTURE_MOVE(self, fm); } } for (i=0; i<(self->moves_len); i++) { b2 = board_cp(b); self->weight += (-1*(!is_ia))*compute_moves((self->moves)[i], b2, OTHER_PLAYER(player), deep-1, !is_ia); free(b2); b2 = NULL; } for (i=0; i<moves_c_len; i++) { if (moves_c[i] != NULL) { free(moves_c[i]); moves_c[i] = NULL; } } free(moves_c); moves_c = NULL; return self->weight; }