井字棋 4x4 python

tic tac toe 4x4 python

我正在使用 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

我看到的第一个问题是您的函数没有缩进。在每个 def 语句之后,你应该有缩进。例如,这是错误的:

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 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

对您的整个代码执行此操作,看看是否能解决您的问题。如果没有,请查看是否可以缩小范围:您刚刚 post 编辑的代码量相当大(两个全屏),而且大多数人都不想滚动浏览整个内容。如果你能缩小到 "okay, this is the function that's going wrong",那么可能会有更多人帮助你找到解决方案。

(注意: 当我说 "This would be correct" 时,我的意思只是关于缩进。我还没有真正看过你的程序的逻辑;仍然可能据我所知,你的 ganador 函数中的错误。)

编辑: 在您的逻辑中发现一个问题。此行与您认为的不同:

if str(casilla) in '012345678910111213141516' and # ... rest of line left out

如果 casilla 为 91,则为真,因为可以在您要检查的字符串中找到字符串“91”。如果你想检查 casilla 只有 包含有效输入,你应该对照有效字符串列表检查它,如下所示:

if str(casilla) in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'] and # ... rest of line left out

(请注意我遗漏了“16”:如果您接受“16”作为有效输入,您将在某个时候遇到 IndexError。)

尽管有更好的方法来进行此检查。当您真的只想知道它是否介于 0 和 15 之间时,为什么还要费心将它转换为字符串呢?只需这样做:

if 0 <= casilla <= 15 and # ... rest of line left out

稍后我可能会为您提供更多帮助,但现在就足够了。适当地缩进你的函数,看看你的问题是否消失了。如果没有,请尝试缩小范围,然后 post 使用缩小代码的新问题。

(缩小范围的提示:在各处粘贴 print 语句,打印出您在代码中不同位置获得的值,并查看它们对您来说是对还是错. 一个好的调试器更好,但是如果你没有调试器,print statements 会带你走很长的路。)