def alpha_beta(current_board, current_player, remaining_depth, alpha, beta, maximizing_player): if remaining_depth == 0 or is_terminal_node(current_board, current_player): return eval_board(current_board, current_player) if maximizing_player: v = min_eval_board for y in range(board_size): for x in range(board_size): legal, _ = is_legal_move(current_board, x, y, current_player) if legal: board_temp, total_piece_taken = execute_move( copy.deepcopy(current_board), x, y, current_player) v = max( v, alpha_beta(board_temp, current_player, remaining_depth - 1, alpha, beta, False)) alpha = max(alpha, v) if beta <= alpha: break return v else: # minimizingPlayer v = max_eval_board for y in range(board_size): for x in range(board_size): legal, _ = is_legal_move(current_board, x, y, current_player) if legal: board_temp, total_piece_taken = execute_move( copy.deepcopy(current_board), x, y, current_player) v = min( v, alpha_beta(board_temp, current_player, remaining_depth - 1, alpha, beta, True)) beta = min(beta, v) if beta <= alpha: break return v
def is_legal_move(current_board, x, y, player): if not (0 <= x < board_size and 0 <= y < board_size): return False, 'Out of range' if current_board[y][x] != '0': return False, 'Box is filled' _, total_piece_taken = execute_move(copy.deepcopy(current_board), x, y, player) if total_piece_taken == 0: return False, 'No piece taken!' return True, 'OK'
def minimax(current_board, current_player, remaining_depth, maximizing_player): if remaining_depth == 0 or is_terminal_node(current_board, current_player): return eval_board(current_board, current_player) legal_moves = get_list_legal_moves(current_board, current_player) if maximizing_player: best_value = min_eval_board for x, y in legal_moves: board_temp, _ = execute_move(copy.deepcopy(current_board), x, y, current_player) v = minimax(board_temp, current_player, remaining_depth - 1, False) best_value = max(best_value, v) else: # minimizing_player best_value = max_eval_board for x, y in legal_moves: board_temp, _ = execute_move(copy.deepcopy(current_board), x, y, current_player) v = minimax(board_temp, current_player, remaining_depth - 1, True) best_value = min(best_value, v) return best_value
def best_move(current_board, current_player, cpu_mode, deepest_depth): points, max_points = 0, 0 x_move_to, y_move_to = -1, -1 legal_moves = get_list_legal_moves(current_board, current_player) for x, y in legal_moves: board_temp, _ = execute_move(copy.deepcopy(current_board), x, y, current_player) if cpu_mode == 1: points = minimax(board_temp, current_player, deepest_depth, True) elif cpu_mode == 2: points = alpha_beta(current_board, current_player, deepest_depth, min_eval_board, max_eval_board, True) elif cpu_mode == 3: return random_bot(current_board, current_player) if points > max_points: max_points = points x_move_to, y_move_to = x, y return x_move_to, y_move_to
print() print_board() print('Player cannot play! Game ended!') print('Score User: '******'1'))) print('Score AI : ' + str(eval_board(board, '2'))) sys.exit() else: print('No moves, Player ' + player + 'skipped') continue elif player == '1': # 1st AI's turn x_move_to, y_move_to = best_move(board, player, cpu_mode1, deepest_depth) if (x_move_to, y_move_to) != (-1, -1): board, total_piece_taken = execute_move( board, x_move_to, y_move_to, player) print('1st AI played (X Y): ' + str(x_move_to) + ' ' + str(y_move_to)) print('# of pieces taken: ' + str(total_piece_taken)) else: # 2nd AI's turn x_move_to, y_move_to = best_move(board, player, cpu_mode2, deepest_depth) if (x_move_to, y_move_to) != (-1, -1): board, total_piece_taken = execute_move( board, x_move_to, y_move_to, player) print('2nd AI played (X Y): ' + str(x_move_to) + ' ' + str(y_move_to)) print('# of pieces taken: ' + str(total_piece_taken)) else: