Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python A.I.悬挂游戏的慢速索引功能_Python_Pygame_Artificial Intelligence - Fatal编程技术网

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

Python A.I.悬挂游戏的慢速索引功能,python,pygame,artificial-intelligence,Python,Pygame,Artificial Intelligence,我已经确定了我在我的纸牌游戏中用于A.I.的代码,该代码非常慢,在处理过程中会挂起游戏。问题是它有效,我想不出更好的方法。在不涉及游戏规则的情况下,我提供了有问题的代码以及一些上下文 通过使用print语句,我缩小了问题代码的范围,以便对\u abs\u list()进行排序。打印此功能时,会有一个长时间的暂停 我对编写代码不熟悉,欢迎提出任何建议 values_list = [[9, 3, 10, 5, 2], [0, -6, 1, -4, -7], [8, 2, 9, 4, 1], [0,

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

通过使用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)

由于没有包含sort\u abs\u list\u hand()的方法定义,因此很难说到底是什么原因导致了速度缓慢

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