Minmax tictactoe :c Cannot understand why this does not work

Bill WhoKnows at newsguy.com
Tue Apr 24 20:30:17 EDT 2018


You need a good lesson in "program documentation". Your code 
looks terrible--really!


fifii.geral at gmail.com wrote:
> class AiMove:
> 	def __init__(self):
> 		self.x = -1
> 		self.y=-1
> 		self.score = 0
> 
> 
> def Imprimir(Matriz,n):
> 	for i in range(n):
> 		linea ="|"
> 		for j in range(n):
> 			if (Matriz[i][j] == 0):
> 				linea+=" "
> 			elif (Matriz[i][j]==- 1):
> 				linea+="X"
> 			elif (Matriz[i][j]== 1):
> 				linea+="O"
> 			linea+="|"
> 		print linea		
> 
> 		print "---------"
> 
> def Gameover(Matriz,n):
> 	#Condicion tablero lleno
> 	blancos=0;
> 	for i in range(n):
> 		contJfilas = 0
> 		contCfilas = 0
> 		contJcol = 0
> 		contCcol = 0
> 		for j in range(n):
> 			if(Matriz[i][j]==0):
> 				blancos = blancos + 1
> 			if (Matriz[i][j]==1):
> 				contCfilas= contCfilas+1
> 			elif(Matriz[i][j]==-1):
> 				contJfilas = contJfilas +1
> 			if (Matriz[j][i]==1):
> 				contCcol= contCcol +1
> 			elif (Matriz[j][i]==-1):
> 				contJcol= contJcol +1
> 		#Condicion 3 en raya en fila o 3 en raya columna
> 		if (contJcol==3 or contJfilas ==3):
> 			return -1
> 		elif (contCfilas==3 or contCcol ==3):
> 			return 1
> 	if blancos == 16:
> 		return 2
> 	else:
> 		return 0
> 
> def getMejorM(Matriz, player,n):
> 	rv = Gameover(Matriz,n)
> 	if(rv== 1):
> 		a = AiMove()
> 		a.score = 10
> 		return a
> 	elif rv==-1:
> 		a = AiMove()
> 		a.score = -10
> 		return a
> 
> 	elif rv ==2: #Si es empate
> 		a = AiMove()
> 		return a
> 
> 	movimientos = []
> 	for i in range (n):
> 		for j in range (n):
> 			if(Matriz[i][j] == 0):
> 				movimiento = AiMove()
> 				movimiento.x = i
> 				movimiento.y = j
> 				Matriz[i][j] = player
> 				if (player == 1):
> 					movimiento.score = getMejorM(Matriz, -1,n).score
> 				else:
> 					movimiento.score = getMejorM(Matriz, 1,n).score
> 				movimientos.append(movimiento)
> 				Matriz[i][j] = 0
> 	
> 	#Escoger
> 	auxb = 0
> 	if player==1:
> 		mejorscore = -100000
> 		for i in range (len(movimientos)):
> 			if movimientos[i].score > mejorscore:
> 				auxb= i
> 				mejorscore = movimientos[i].score
> 	
> 	elif player==-1:
> 		peorscore = 100000
> 		for i in range (len(movimientos)):
> 			if movimientos[i].score < peorscore:
> 				auxb= i
> 				peorscore = movimientos[i].score
> 
> 	#Retornar el mejor movimiento
> 	return movimientos[auxb]
> 
> 
> 
> n=4
> Matriz = [[0 for i in range(n)] for j in range(n)]
> for i in range(n):
> 	for j in range(n):
> 		Matriz[i][j] = 0
> Imprimir(Matriz,n)
> while True:
> 	fila = int(raw_input("Ingresa casilla x: "))-1
> 	columna = int(raw_input("Ingresa casilla y: "))-1
> 	if (Matriz[fila][columna] == 0):
> 		Matriz[fila][columna] = -1
> 	#Mover(Matriz,n)
> 	Imprimir(Matriz,n)
> 	bestM = AiMove()
> 	bestM = getMejorM(Matriz, 1, n)
> 	Matriz[bestM.x][bestM.y] = 1
> 	Imprimir(Matriz,n)
> 	if Gameover(Matriz,n) != 0:
> 		break
> 
> print "FIN"
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 




More information about the Python-list mailing list