int minimax(t_grid *grid, int max, int i) { int ret; int tmp; ret = 0; while (i < grid->columns) { if (grid->map[0][i] == '.') { put_piece(grid, i + 1, 'X'); if (check_winner(grid, 'X') != 0) { remove_piece(grid, i + 1); return (i); } tmp = min(grid, 3, 0); if (tmp > max) { tmp = max; ret = i; } remove_piece(grid, i + 1); } i++; } return (ret); }
int min(t_grid *grid, int depth, int i) { int min; int tmp; min = 3000; if (depth <= 0) return (eval(grid)); while (i < grid->columns) { if (grid->map[0][i] == '.') { put_piece(grid, i + 1, 'O'); if (check_winner(grid, 'O') != 0) { remove_piece(grid, i + 1); return (-3000); } tmp = max(grid, depth - 1, 0); if (tmp < min) min = tmp; remove_piece(grid, i + 1); } i++; } return (min); }
int max(t_grid *grid, int depth, int i) { int max; int tmp; max = -3000; if (depth <= 0) return (eval(grid)); while (i < grid->columns) { if (grid->map[0][i] == '.') { put_piece(grid, i + 1, 'X'); if (check_winner(grid, 'X') != 0) { remove_piece(grid, i + 1); return (3000); } tmp = min(grid, depth - 1, 0); if (tmp > max) max = tmp; remove_piece(grid, i + 1); } i++; } return (max); }
void pack_dbf(dbhead_t *dbh) { long out_off, in_off; int rec_cnt, new_cnt; char *cp; if ((cp = (char *)malloc(dbh->db_rlen)) == NULL) { return; } in_off = out_off = dbh->db_hlen; new_cnt = 0; rec_cnt = dbh->db_records; while (rec_cnt > 0) { if (get_piece(dbh, in_off, cp, dbh->db_rlen) < 0) break; if (*cp != DELETED_RECORD) { /* write the record into the file */ if (put_piece(dbh, out_off, cp, dbh->db_rlen) < 0) break; out_off += dbh->db_rlen; new_cnt++; } in_off += dbh->db_rlen; rec_cnt--; } free(cp); if (rec_cnt == 0) dbh->db_records = new_cnt; }
/* * put a record to the database */ long put_dbf_record(dbhead_t *dbh, long rec_num, char *cp) { long offset; if (rec_num == 0) { rec_num = dbh->db_records; } if (rec_num > dbh->db_records) { return 0L; } /* go to the correct spot on the file */ offset = dbh->db_hlen + (rec_num - 1) * dbh->db_rlen; if (put_piece(dbh, offset, cp, dbh->db_rlen) != dbh->db_rlen) { rec_num = -1; } return rec_num; }
void pack_dbf(dbhead_t *dbh) { long out_off, in_off; int rec_cnt, new_cnt; char *cp; if ((cp = (char *)malloc(dbh->db_rlen)) == NULL) { return; } in_off = out_off = dbh->db_hlen; new_cnt = 0; rec_cnt = dbh->db_records; while (rec_cnt > 0) { if (get_piece(dbh, in_off, cp, dbh->db_rlen) < 0) break; if (*cp != DELETED_RECORD) { /* write the record into the file */ if (put_piece(dbh, out_off, cp, dbh->db_rlen) < 0) break; out_off += dbh->db_rlen; new_cnt++; } in_off += dbh->db_rlen; rec_cnt--; } free(cp); /* Try to truncate the file to the right size. */ if (ftruncate(dbh->db_fd, out_off) != 0) { TSRMLS_FETCH(); php_error_docref(NULL TSRMLS_CC, E_WARNING, "dbase_pack() couldn't truncate the file to the right size. Some deleted records may still be left in there."); } if (rec_cnt == 0) dbh->db_records = new_cnt; }
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; }
int set_pos(struct Position* pos, char* fen) { u32 piece, pt, sq, pc, tsq = 0, index = 0; char c; pos->state->pos_key = 0ULL; pos->state->pawn_key = 0ULL; for (sq = 0; sq < 64; ++sq) castle_perms[sq] = 15; while (tsq < 64) { sq = tsq ^ 56; c = fen[index++]; if (c == ' ') { break; } else if (c > '0' && c < '9') { tsq += (c - '0'); } else if (c == '/') { continue; } else { piece = get_piece_from_char(c); pt = piece & 7; pc = piece >> 3; put_piece(pos, sq, pt, pc); if (pt == KING) { pos->king_sq[pc] = sq; castle_perms[sq] = pc == WHITE ? 12 : 3; } else if (pt == ROOK && !is_frc) { if (sq == H1 || sq == H8) { castling_rook_pos[pc][KINGSIDE] = sq; castle_perms[sq] = pc == WHITE ? 14 : 11; } else if (sq == A1 || sq == A8) { castling_rook_pos[pc][QUEENSIDE] = sq; castle_perms[sq] = pc == WHITE ? 13 : 7; } } ++tsq; } } ++index; pos->stm = fen[index] == 'w' ? WHITE : BLACK; index += 2; int file, rank, color; while ((c = fen[index++]) != ' ') { if (c == '-') { ++index; break; } else if (!is_frc) { pos->state->castling_rights |= get_cr_from_char(c); } else { if (c >= 'a' && c <= 'z') { color = BLACK; rank = RANK_8; file = c - 'a'; } else if (c >= 'A' && c <= 'Z') { color = WHITE; rank = RANK_1; file = c - 'A'; } u32 cr = get_cr_from_char(c); if (cr != -1) { u64 r_bb = pos->bb[ROOK] & pos->bb[color]; int ksq = pos->king_sq[color]; int kside_sq, qside_sq = 0; while (r_bb) { kside_sq = bitscan(r_bb); r_bb &= r_bb - 1; if (is_queenside(ksq, kside_sq)) qside_sq = kside_sq; if (qside_sq && kside_sq != qside_sq) break; } if (cr & (WKC | BKC)) { castling_rook_pos[color][KINGSIDE] = kside_sq; castle_perms[kside_sq] = color == WHITE ? 14 : 11; pos->state->castling_rights |= color == WHITE ? WKC : BKC; } else if (cr & (WQC | BQC)) { castling_rook_pos[color][QUEENSIDE] = qside_sq; castle_perms[qside_sq] = color == WHITE ? 13 : 7; pos->state->castling_rights |= color == WHITE ? WQC : BQC; } } else { sq = get_sq(rank, file); if (is_kingside(pos->king_sq[color], sq)) { castling_rook_pos[color][KINGSIDE] = sq; castle_perms[sq] = color == WHITE ? 14 : 11; pos->state->castling_rights |= color == WHITE ? WKC : BKC; } else if (is_queenside(pos->king_sq[color], sq)) { castling_rook_pos[color][QUEENSIDE] = sq; castle_perms[sq] = color == WHITE ? 13 : 7; pos->state->castling_rights |= color == WHITE ? WQC : BQC; } } } } pos->state->pos_key ^= castle_keys[pos->state->castling_rights]; u32 ep_sq = 0; if ((c = fen[index++]) != '-') { ep_sq = (c - 'a') + ((fen[index++] - '1') << 3); pos->state->pos_key ^= psq_keys[0][0][ep_sq]; pos->state->ep_sq_bb = BB(ep_sq); } ++index; u32 x = 0; while ((c = fen[index++]) != '\n' && c != ' ' && c != '\0') x = x * 10 + (c - '0'); pos->state->fifty_moves = x; x = 0; while ((c = fen[index++]) != '\n' && c != ' ' && c != '\0') x = x * 10 + (c - '0'); pos->state->full_moves = x; return index; }