#include "alphabeta_search.h" int value1(int side[]) { int v = side[houses]; if (v > 24) /* win */ return 100; if (v == 24) /* draw or better */ return 50; return v; } int value(int thisside[], int otherside[]) { return value1(thisside) - value1(otherside); } int alpha_beta_bestmove(int depth, int thisside[], int otherside[], int *vp) { int ts1[BOARD_SIZE], os1[BOARD_SIZE]; int best_move = -1; int alpha = MIN_INFINITY; int current_value; for (int move = 0; move < houses; move++) { memcpy(ts1, thisside, BOARD_ARRAY_SIZE); memcpy(os1, otherside, BOARD_ARRAY_SIZE); if (plyfull(move, ts1, os1)) { current_value = value(ts1, os1); if (depth > 0 && current_value <= 50 && current_value >= -50) { current_value = -helper_alpha_beta_search(depth - 1, -MAX_INFINITY, -alpha, os1, ts1); } if (current_value > alpha) { best_move = move; alpha = current_value; } } } *vp = alpha; return best_move; } int helper_alpha_beta_search(int depth, int alpha, int beta, int thisside[], int otherside[]) { int ts1[BOARD_SIZE], os1[BOARD_SIZE]; for (int move = 0; move < houses; move++) { memcpy(ts1, thisside, BOARD_ARRAY_SIZE); memcpy(os1, otherside, BOARD_ARRAY_SIZE); if (plyfull(move, ts1, os1)) { int current_value = value(ts1, os1); if (depth > 0 && current_value <= 50 && current_value >= -50) { current_value = -helper_alpha_beta_search(depth - 1, -beta, -alpha, os1, ts1); } if (current_value > alpha) { if (current_value >= beta) { return current_value; } alpha = current_value; } } } return alpha; }