i'm working in a tic tac toe 4x4 with minmax , the algorythm runs, but does nothing, i think that the problem is in "def ganador" or "def juega_humano" reading the inputs, i would greatly appreciate any help, thanks
PD: sorry for my tarzan english
#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
The first problem I can see is that your functions aren't indented. After each def statement, you should have indentation. For example, this is wrong:
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
This would be correct:
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
Do that for your whole code, and see if that solves your problem. If not, see if you can narrow it down a little: the amount of code you just posted is quite large (two full screens), and most people aren't going to want to scroll through the entire thing. If you can narrow it down to "okay, this is the function that's going wrong", then more people will be likely to help you find a solution.
(NOTE: When I say "This would be correct", I only mean with regard to indentation. I haven't actually looked at your program's logic; there could still be a bug in your ganador
function, for all I know.)
EDIT: Found one problem in your logic. This line doesn't do what you think it does:
if str(casilla) in '012345678910111213141516' and # ... rest of line left out
If casilla
is 91, this will be true, because the string "91" can be found in that string you're checking against. If you want to check that casilla
only contains valid input, you should check it against a list of valid strings, like this:
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
(Notice that I left out '16': if you accept '16' as valid input, you're going to have an IndexError at some point.)
Although there's a much better way to do this check. Why bother with converting it to a string, when you really just want to know if it's between 0 and 15? Just do this:
if 0 <= casilla <= 15 and # ... rest of line left out
I may give you more help later on, but that's enough for now. Indent your functions properly, and see if your problem goes away. If it doesn't, try to narrow it down, then post a new question with the narrowed-down code.
(Hint for narrowing it down: stick print
statements all over the place, printing out the values that you're getting at different places in your code, and see if they look right or wrong to you. A good debugger is even better, but if you don't have a debugger, print
statements can take you a long way.)