A.I.悬挂游戏的缓慢索引功能

2024-05-23 16:38:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经确定了我在我的纸牌游戏中用于人工智能的代码,该代码非常慢,并且在处理过程中挂起游戏。问题是它很管用,我想不出更好的方法。在不涉及游戏规则的情况下,我提供了有问题的代码以及一些上下文

使用print语句,我缩小了有问题的代码范围,以便对\u abs\u list()进行排序。当打印出这个函数时,会有一个很长的停顿

我是新来写代码任何建议都欢迎

values_list = [[9, 3, 10, 5, 2], [0, -6, 1, -4, -7], [8, 2, 9, 4, 1], 
[0, -6, 1, -4, -7], [9, 3, 10, 5, 2]]
number_needed_for_20 = -1
ai hand = [10, 1, 9, 10]
ai_piles = [1, 7, 0, 5, 8]

def ai_grind():                                               
    """Creates a 2D list of values using the number needed for twenty"""
    list = []
    for x in range(5):
        list.append([])
        for y in range(5):
            list[x].append(values_list[x][y] - (number_needed_for_20))
    return list

def ai_grind_abs():                                                        
    """Returns a 2D list of absolute values that will be indexed"""
    list = []
    for x in range(5): 
        list.append([])
        for y in range(5):
            list[x].append(abs(ai_grind()[x][y]))
    return list

def abs_list_pile():      
    """Returns pile positions of the min values"""
    list = []
    for x in range(5):
        list.append(ai_grind_abs()[x].index(min(ai_grind_abs()[x])))
    return list

def abs_list_hand():
    """A list of min values for hand position"""
    list = []
    for x in range(5): 
        list.append(min(ai_grind_abs()[x]))
    return list

def sort_abs_list(): # problematic                               
    """Returns position of pile and hand cards"""
    # finds hand position
    a = abs_list_hand().index(min(sort_abs_list_hand()))
    # finds pile position
    b = abs_list_pile()[a]

def ai_hand_to_pile():
    """Moves a card from the hand to the pile"""
    card = ai_hand[sort_abs_list()[0]]
    ai_piles[sort_abs_list()[1]].append(card)
    ai_hand.remove(card)

Tags: of代码infordefrangeabsmin
1条回答
网友
1楼 · 发布于 2024-05-23 16:38:17

因为没有包含sort\u abs\u list\u hand()的方法定义,所以很难说到底是什么导致了缓慢

但是。看起来你做了很多嵌套循环,并且丢掉了一些中间结果。尝试在ai\u grind\u abs()中的循环之前将ai\u grind()的结果保存在变量中,而不是在循环中调用它。在abs\u list\u pile()循环之前,可以对ai\u grind\u abs()的返回值执行相同的操作

相关问题 更多 >