【发布时间】:2015-10-02 12:05:05
【问题描述】:
我正在使用 minmax 进行 tic tac toe 4x4 工作,算法运行,但什么也没做,我认为问题在于“def ganador”或“def juega_humano”读取输入,我将不胜感激任何帮助,谢谢
PD:对不起我的泰山英语
#the lines for win
def ganador(tablero):
lineas = [[0,1,2,3], [4,5,6,7], [8,9,10,11], [12,13,14,15], [0,4,8,12], [1,5,9,13], [2,6,10,14], [3,7,11,15], [0,5,10,15], [3,6,9,12]]
ganador = 0
for linea in lineas:
if tablero[linea[0]] == tablero[linea[1]] and tablero[linea[0]] == tablero[linea[2]] and tablero[linea[0]] == tablero[linea[3]] and tablero[linea[0]] != 0:
ganador = tablero[linea[0]]
return ganador
def ver_tablero(tablero):
for i in range(0,4):
for j in range(0,4):
if tablero[i*4+j] == MAX:
print 'X',
elif tablero[i*4+j] == MIN:
print 'O',
else:
print '.',
print ''
def juega_humano(tablero):
ok=False
while not ok:
casilla = input ("Casilla?")
# 0 to exit, 1-16 for cells defined with "range" in another place
if str(casilla) in '012345678910111213141516' and (len(str(casilla))<= 2) and tablero[casilla-1] == 0:
if casilla == 0:
sys.exit(0)
tablero[casilla-1]=MIN
ok=True
return tablero
def juega_ordenador(tablero):
global jugada_maquina
punt = minimax(tablero[:], MAX)
tablero[jugada_maquina] = MAX
return tablero
if __name__ == "__main__":
print 'Introduce casilla o 0 para terminar'
tablero = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
while (True):
ver_tablero(tablero)
tablero = juega_humano(tablero)
if game_over(tablero):
break
tablero = juega_ordenador(tablero)
if game_over(tablero):
break
【问题讨论】:
-
欢迎来到 StackOverflow!首先,如果您可以edit 您的问题正确缩进您的代码,那就太好了。特别是在python中,很难判断错误是否与缩进有关。
-
我看到的第一个问题是你的函数没有缩进。在每个
def语句之后,您应该有缩进。我无法将缩进作为评论发布,因此我将其作为答案发布。
标签: python tic-tac-toe