我有一个函数(很大,你不需要阅读它,特别是因为它不是英文的,它依赖于其他函数),我有一个参数“strat”。这个函数基本上是人工智能(它是游戏中的机器人,井字游戏),并且我想用参数“strat”过滤函数具有的参数。我的意思是这个功能有 6 种“可能性”的行动,通过选择不同的策略我想缩小这些。如果 str = 'basic' 那么它只会读取参数 3、5 和 6 但如果我写 'normal' 它会读取所有内容。这意味着该函数接收参数并按顺序执行,它检查第一个满足要求的,如果不满足则跳转到下一个。#数字
def chose(tab,n,strat):
#1 Victory
if strat != 'basic':
if identificar_tuplos_pertencentes(obter_coluna(tab, 1), n) is True:
if obter_coluna(tab, 1)[0] == n and obter_coluna(tab, 1)[1] == n and obter_coluna(tab, 1)[2] == 0:
return marcar_posicao(tab,n,7)
elif obter_coluna(tab, 1)[0] == n and obter_coluna(tab, 1)[1] == 0 and obter_coluna(tab, 1)[2] == n:
return marcar_posicao(tab,n,4)
elif obter_coluna(tab, 1)[0] == 0 and obter_coluna(tab, 1)[1] == n and obter_coluna(tab, 1)[2] == n:
return marcar_posicao(tab,n,1)
elif identificar_tuplos_pertencentes(obter_coluna(tab, 2), n) is True:
if obter_coluna(tab, 2)[0] == n and obter_coluna(tab, 2)[1] == n and obter_coluna(tab, 2)[2] == 0:
return marcar_posicao(tab,n,8)
elif obter_coluna(tab, 2)[0] == n and obter_coluna(tab, 2)[1] == 0 and obter_coluna(tab, 2)[2] == n:
return marcar_posicao(tab,n,5)
elif obter_coluna(tab, 2)[0] == 0 and obter_coluna(tab, 2)[1] == n and obter_coluna(tab, 2)[2] == n:
return marcar_posicao(tab,n,2)
elif identificar_tuplos_pertencentes(obter_coluna(tab, 3), n) is True:
if obter_coluna(tab, 3)[0] == n and obter_coluna(tab, 3)[1] == n and obter_coluna(tab, 3)[2] == 0:
return marcar_posicao(tab,n,9)
elif obter_coluna(tab, 3)[0] == n and obter_coluna(tab, 3)[1] == 0 and obter_coluna(tab, 3)[2] == n:
return marcar_posicao(tab,n,6)
elif obter_coluna(tab, 3)[0] == 0 and obter_coluna(tab, 3)[1] == n and obter_coluna(tab, 3)[2] == n:
return marcar_posicao(tab,n,3)
#Determinar Linhas
elif identificar_tuplos_pertencentes(obter_linha(tab, 1), n) is True:
if obter_linha(tab, 1)[0] == n and obter_linha(tab, 1)[1] == n and obter_linha(tab, 1)[2] == 0:
return marcar_posicao(tab,n,3)
elif obter_linha(tab, 1)[0] == n and obter_linha(tab, 1)[1] == 0 and obter_linha(tab, 1)[2] == n:
return marcar_posicao(tab,n,2)
elif obter_linha(tab, 1)[0] == 0 and obter_linha(tab, 1)[1] == n and obter_linha(tab, 1)[2] == n:
return marcar_posicao(tab,n,1)
elif identificar_tuplos_pertencentes(obter_linha(tab, 2), n) is True:
if obter_linha(tab, 2)[0] == n and obter_linha(tab, 2)[1] == n and obter_linha(tab, 2)[2] == 0:
return marcar_posicao(tab,n,6)
elif obter_linha(tab, 2)[0] == n and obter_linha(tab, 2)[1] == 0 and obter_linha(tab, 2)[2] == n:
return marcar_posicao(tab,n,5)
elif obter_linha(tab, 2)[0] == 0 and obter_linha(tab, 2)[1] == n and obter_linha(tab, 2)[2] == n:
return marcar_posicao(tab,n,4)
elif identificar_tuplos_pertencentes(obter_linha(tab, 3), n) is True:
if obter_linha(tab, 3)[0] == n and obter_linha(tab, 3)[1] == n and obter_linha(tab, 3)[2] == 0:
return marcar_posicao(tab,n,9)
elif obter_linha(tab, 3)[0] == n and obter_linha(tab, 3)[1] == 0 and obter_linha(tab, 3)[2] == n:
return marcar_posicao(tab,n,8)
elif obter_linha(tab, 3)[0] == 0 and obter_linha(tab, 3)[1] == n and obter_linha(tab, 3)[2] == n:
return marcar_posicao(tab,n,7)
#Determinar nas diagonais
elif identificar_tuplos_pertencentes(obter_diagonal(tab, 1), n) is True:
if obter_diagonal(tab, 1)[0] == n and obter_diagonal(tab, 1)[1] == n and obter_diagonal(tab, 1)[2] == 0:
return marcar_posicao(tab,n,9)
elif obter_diagonal(tab, 1)[0] == n and obter_diagonal(tab, 1)[1] == 0 and obter_diagonal(tab, 1)[2] == n:
return marcar_posicao(tab,n,5)
elif obter_diagonal(tab, 1)[0] == 0 and obter_diagonal(tab, 1)[1] == n and obter_diagonal(tab, 1)[2] == n:
return marcar_posicao(tab,n,1)
elif identificar_tuplos_pertencentes(obter_diagonal(tab, 2), n) is True:
if obter_diagonal(tab, 2)[0] == n and obter_diagonal(tab, 2)[1] == n and obter_diagonal(tab, 2)[2] == 0:
return marcar_posicao(tab,n,3)
elif obter_diagonal(tab, 2)[0] == n and obter_diagonal(tab, 2)[1] == 0 and obter_diagonal(tab, 2)[2] == n:
return marcar_posicao(tab,n,5)
elif obter_diagonal(tab, 2)[0] == 0 and obter_diagonal(tab, 2)[1] == n and obter_diagonal(tab, 2)[2] == n:
return marcar_posicao(tab,n,7)
#2 Block
elif strat != 'basic':
if identificar_tuplos_nao_pertencentes(obter_coluna(tab, 1), n) is True:
if obter_coluna(tab, 1)[0] == -n and obter_coluna(tab, 1)[1] == -n and obter_coluna(tab, 1)[2] == 0:
return marcar_posicao(tab,n,7)
elif obter_coluna(tab, 1)[0] == -n and obter_coluna(tab, 1)[1] == 0 and obter_coluna(tab, 1)[2] == -n:
return marcar_posicao(tab,n,4)
elif obter_coluna(tab, 1)[0] == 0 and obter_coluna(tab, 1)[1] == -n and obter_coluna(tab, 1)[2] == -n:
return marcar_posicao(tab,n,1)
elif identificar_tuplos_nao_pertencentes(obter_coluna(tab, 2), n) is True:
if obter_coluna(tab, 2)[0] == -n and obter_coluna(tab, 2)[1] == -n and obter_coluna(tab, 2)[2] == 0:
return marcar_posicao(tab,n,8)
elif obter_coluna(tab, 2)[0] == -n and obter_coluna(tab, 2)[1] == 0 and obter_coluna(tab, 2)[2] == -n:
return marcar_posicao(tab,n,5)
elif obter_coluna(tab, 2)[0] == 0 and obter_coluna(tab, 2)[1] == -n and obter_coluna(tab, 2)[2] == -n:
return marcar_posicao(tab,n,2)
elif identificar_tuplos_nao_pertencentes(obter_coluna(tab, 3), n) is True:
if obter_coluna(tab, 3)[0] == -n and obter_coluna(tab, 3)[1] == -n and obter_coluna(tab, 3)[2] == 0:
return marcar_posicao(tab,n,9)
elif obter_coluna(tab, 3)[0] == -n and obter_coluna(tab, 3)[1] == 0 and obter_coluna(tab, 3)[2] == -n:
return marcar_posicao(tab,n,6)
elif obter_coluna(tab, 3)[0] == 0 and obter_coluna(tab, 3)[1] == -n and obter_coluna(tab, 3)[2] == -n:
return marcar_posicao(tab,n,3)
elif identificar_tuplos_nao_pertencentes(obter_linha(tab, 1), n) is True:
if obter_linha(tab, 1)[0] == -n and obter_linha(tab, 1)[1] == -n and obter_linha(tab, 1)[2] == 0:
return marcar_posicao(tab,n,3)
elif obter_linha(tab, 1)[0] == -n and obter_linha(tab, 1)[1] == 0 and obter_linha(tab, 1)[2] == -n:
return marcar_posicao(tab,n,2)
elif obter_linha(tab, 1)[0] == 0 and obter_linha(tab, 1)[1] == -n and obter_linha(tab, 1)[2] == -n:
return marcar_posicao(tab,n,1)
elif identificar_tuplos_nao_pertencentes(obter_linha(tab, 2), n) is True:
if obter_linha(tab, 2)[0] == -n and obter_linha(tab, 2)[1] == -n and obter_linha(tab, 2)[2] == 0:
return marcar_posicao(tab,n,6)
elif obter_linha(tab, 2)[0] == -n and obter_linha(tab, 2)[1] == 0 and obter_linha(tab, 2)[2] == -n:
return marcar_posicao(tab,n,5)
elif obter_linha(tab, 2)[0] == 0 and obter_linha(tab, 2)[1] == -n and obter_linha(tab, 2)[2] == -n:
return marcar_posicao(tab,n,4)
elif identificar_tuplos_nao_pertencentes(obter_linha(tab, 3), n) is True:
if obter_linha(tab, 3)[0] == -n and obter_linha(tab, 3)[1] == -n and obter_linha(tab, 3)[2] == 0:
return marcar_posicao(tab,n,9)
elif obter_linha(tab, 3)[0] == -n and obter_linha(tab, 3)[1] == 0 and obter_linha(tab, 3)[2] == -n:
return marcar_posicao(tab,n,8)
elif obter_linha(tab, 3)[0] == 0 and obter_linha(tab, 3)[1] == -n and obter_linha(tab, 3)[2] == -n:
return marcar_posicao(tab,n,7)
elif identificar_tuplos_nao_pertencentes(obter_diagonal(tab, 1), n) is True:
if obter_diagonal(tab, 1)[0] == -n and obter_diagonal(tab, 1)[1] == -n and obter_diagonal(tab, 1)[2] == 0:
return marcar_posicao(tab,n,9)
elif obter_diagonal(tab, 1)[0] == -n and obter_diagonal(tab, 1)[1] == 0 and obter_diagonal(tab, 1)[2] == -n:
return marcar_posicao(tab,n,5)
elif obter_diagonal(tab, 1)[0] == 0 and obter_diagonal(tab, 1)[1] == -n and obter_diagonal(tab, 1)[2] == -n:
return marcar_posicao(tab,n,1)
elif identificar_tuplos_nao_pertencentes(obter_diagonal(tab, 2), n) is True:
if obter_diagonal(tab, 2)[0] == -n and obter_diagonal(tab, 2)[1] == -n and obter_diagonal(tab, 2)[2] == 0:
return marcar_posicao(tab,n,3)
elif obter_diagonal(tab, 2)[0] == -n and obter_diagonal(tab, 2)[1] == 0 and obter_diagonal(tab, 2)[2] == -n:
return marcar_posicao(tab,n,5)
elif obter_diagonal(tab, 2)[0] == 0 and obter_diagonal(tab, 2)[1] == -n and obter_diagonal(tab, 2)[2] == -n:
return marcar_posicao(tab,n,7)
#3 Center
elif tab[1][1] == 0:
return marcar_posicao(tab,n,5)
#4 Opposite Corner
elif strat != 'basic':
if tab[0][0] != -n and tab[2][2] == 0:
return marcar_posicao(tab,n,9)
elif tab[0][2] != -n and tab[2][0] == 0:
return marcar_posicao(tab,n,7)
elif tab[2][0] != -n and tab[0][2] == 0:
return marcar_posicao(tab,n,3)
elif tab[2][2] != -n and tab[0][0] == 0:
return marcar_posicao(tab,n,1)
#5 Empty Corner
elif tab[0][0] == 0:
return marcar_posicao(tab,n,1)
elif tab[0][2] == 0:
return marcar_posicao(tab,n,3)
elif tab[2][0] == 0:
return marcar_posicao(tab,n,7)
elif tab[2][2] == 0:
return marcar_posicao(tab,n,9)
#6 What's left
elif tab [0][1] == 0:
return marcar_posicao(tab,n,2)
elif tab [1][0] == 0:
return marcar_posicao(tab,n,4)
elif tab [1][2] == 0:
return marcar_posicao(tab,n,6)
elif tab [2][1] == 0:
return marcar_posicao(tab,n,8)
看看这个,如果我写了 "chose(tab,n,'basic') ,并且在这个例子中 tab 和 n 与此无关,该函数将跳转数字 1 和 2 并转到 3,如果 3 不起作用然后它会跳到第 4 位,然后跳到第 5 位,如果有必要,然后跳到 6 位。我所做的是将这些行“elif strat!='basic':”放在数字之前,但它不起作用而且我没有知道为什么。如果我 str == 'basic' 这有效,但如果 str == 'normal' 则不起作用。这特别烦人,因为在我的实际代码中,strat 可以是 3 种不同的东西。我希望你能理解我的问题提前谢谢你们!