my code tries to generate a tree for reversi/othello but it runs into an infinite while loop and i am unable to debug it.The purpose of this code is to make minmax algorithm for the aiplayer.please help.
import numpy
board=numpy.array([[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,1,2,0,0,0],
[0,0,0,2,1,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]])
matrix_list=[numpy.copy(board)]
points=[]
decoy_list=[]
valid_moves=[[3,5],[5,3],[4,2],[2,4]]
valid_move=[[[3,5],[5,3],[4,2],[2,4]]]
depth=0
#the above assignments is to be provided by riversi game
while len(valid_move)!=0:
for t in range(len(matrix_list)):
value=[0]*len(valid_move[t])
for z in range(len(valid_move[t])):
c=matrix_list[t]
valid_moves=valid_move[t]
#player one
if depth%2==0:
board=numpy.copy(c)
if (valid_moves[z][0])-1!=-1 and (valid_moves[z][1])-1!=-1:
if board[valid_moves[z][0]-1][valid_moves[z][1]-1]==2:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=1
while (valid_moves[z][0]-k)!=-1 and (valid_moves[z][1]-k)!=-1:
if board[valid_moves[z][0]-k][valid_moves[z][1]]==2 and board[valid_moves[z][0]][valid_moves[z][1]-k]==2:
board[valid_moves[z][0]-k][valid_moves[z][1]]=1
board[valid_moves[z][0]][valid_moves[z][1]-k]=1
value[z]+=1
decoy_list.append(board)
k+=1
if (valid_moves[z][0])-1!=-1 and (valid_moves[z][1])+1!=8:
if board[valid_moves[z][0]-1][valid_moves[z][1]+1]==2:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=1
while (valid_moves[z][0]-k)!=-1 and (valid_moves[z][1]+k)!=8:
if board[valid_moves[z][0]-k][valid_moves[z][1]]==2 and board[valid_moves[z][0]][valid_moves[z][1]+k]==2:
board[valid_moves[z][0]-k][valid_moves[z][1]]=1
board[valid_moves[z][0]][valid_moves[z][1]+k]=1
value[z]+=1
decoy_list.append(board)
k+=1
if (valid_moves[z][0])+1!=8 and (valid_moves[z][1])+1!=8:
if board[valid_moves[z][0]+1][valid_moves[z][1]+1]==2:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=1
while (valid_moves[z][0]+k)!=8 and (valid_moves[z][1]+k)!=8:
if board[valid_moves[z][0]+k][valid_moves[z][1]]==2 and board[valid_moves[z][0]][valid_moves[z][1]+k]==2:
board[valid_moves[z][0]+k][valid_moves[z][1]]=1
board[valid_moves[z][0]][valid_moves[z][1]+k]=1
value[z]+=1
decoy_list.append(board)
k+=1
if (valid_moves[z][0])+1!=8 and (valid_moves[z][1])-1!=-1:
if board[valid_moves[z][0]+1][valid_moves[z][1]-1]==2:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=1
while (valid_moves[z][0]+k)!=8 and (valid_moves[z][1]-k)!=-1:
if board[valid_moves[z][0]+1][valid_moves[z][1]]==2 and board[valid_moves[z][0]][valid_moves[z][1]-k]==2:
board[valid_moves[z][0]+1][valid_moves[z][1]]=1
board[valid_moves[z][0]][valid_moves[z][1]-k]=1
value[z]+=1
decoy_list.append(board)
k+=1
if (valid_moves[z][0])-1!=-1:
if board[valid_moves[z][0]-1][valid_moves[z][1]]==2:
board[valid_moves[z][0]][valid_moves[z][1]]=1
k=1
while (valid_moves[z][0]-k)!=-1 and board[valid_moves[z][0]-k][valid_moves[z][1]]==2:
board[valid_moves[z][0]-k][valid_moves[z][1]]=1
value[z]+=1
k+=1
decoy_list.append(board)
if (valid_moves[z][0])+1!=8:
if board[valid_moves[z][0]+1][valid_moves[z][1]]==2:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=1
while (valid_moves[z][0]+k)!=8 and board[valid_moves[z][0]+k][valid_moves[z][1]]==2:
board[valid_moves[z][0]+k][valid_moves[z][1]]=1
value[z]+=1
k+=1
decoy_list.append(board)
if (valid_moves[z][1])-1!=-1:
if board[valid_moves[z][0]][valid_moves[z][1]-1]==2:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=1
while (valid_moves[z][1]-k)!=-1 and board[valid_moves[z][0]][valid_moves[z][1]-k]==2:
board[valid_moves[z][0]][valid_moves[z][1]-k]=1
value[z]+=1
k+=1
decoy_list.append(board)
if (valid_moves[z][1])+1!=8:
if board[valid_moves[z][0]][valid_moves[z][1]+1]==2:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=1
while (valid_moves[z][1]+k)!=8 and board[valid_moves[z][0]][valid_moves[z][1]+k]==2:
board[valid_moves[z][0]][valid_moves[z][1]+k]=1
value[z]+=1
k+=1
decoy_list.append(board)
if z==len(valid_move)-1:
points.append(value)
#player2
if depth%2==1:
board=numpy.copy(c)
if (valid_moves[z][0])-1!=-1 and (valid_moves[z][1])-1!=-1:
if board[valid_moves[z][0]-1][valid_moves[z][1]-1]==1:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=2
while (valid_moves[z][0]-k)!=-1 and (valid_moves[z][1]-k)!=-1:
if board[valid_moves[z][0]-k][valid_moves[z][1]]==1 and board[valid_moves[z][0]][valid_moves[z][1]-k]==1:
board[valid_moves[z][0]-k][valid_moves[z][1]]=2
board[valid_moves[z][0]][valid_moves[z][1]-k]=2
value[z]+=1
decoy_list.append(board)
k+=1
if (valid_moves[z][0])-1!=-1 and (valid_moves[z][1])+1!=8:
if board[valid_moves[z][0]-1][valid_moves[z][1]+1]==1:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=2
while (valid_moves[z][0]-k)!=-1 and (valid_moves[z][1]+k)!=8:
if board[valid_moves[z][0]-k][valid_moves[z][1]]==1 and board[valid_moves[z][0]][valid_moves[z][1]+k]==1:
board[valid_moves[z][0]-k][valid_moves[z][1]]=2
board[valid_moves[z][0]][valid_moves[z][1]+k]=2
value[z]+=1
decoy_list.append(board)
k+=1
if (valid_moves[z][0])+1!=8 and (valid_moves[z][1])+1!=8:
if board[valid_moves[z][0]+1][valid_moves[z][1]+1]==1:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=2
while (valid_moves[z][0]+k)!=8 and (valid_moves[z][1]+k)!=8:
if board[valid_moves[z][0]+k][valid_moves[z][1]]==1 and board[valid_moves[z][0]][valid_moves[z][1]+k]==1:
board[valid_moves[z][0]+k][valid_moves[z][1]]=2
board[valid_moves[z][0]][valid_moves[z][1]+k]=2
value[z]+=1
decoy_list.append(board)
k+=1
if (valid_moves[z][0])+1!=8 and (valid_moves[z][1])-1!=-1:
if board[valid_moves[z][0]+1][valid_moves[z][1]-1]==1:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=2
while (valid_moves[z][0]+k)!=8 and (valid_moves[z][1]-k)!=-1:
if board[valid_moves[z][0]+1][valid_moves[z][1]]==1 and board[valid_moves[z][0]][valid_moves[z][1]-k]==1:
board[valid_moves[z][0]+1][valid_moves[z][1]]=2
board[valid_moves[z][0]][valid_moves[z][1]-k]=2
value[z]+=1
decoy_list.append(board)
k+=1
if (valid_moves[z][0])-1!=-1:
if board[valid_moves[z][0]-1][valid_moves[z][1]]==1:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=2
while (valid_moves[z][0]-k)!=-1 and board[valid_moves[z][0]-k][valid_moves[z][1]]==1:
board[valid_moves[z][0]-k][valid_moves[z][1]]=2
value[z]+=1
k+=1
decoy_list.append(board)
if (valid_moves[z][0])+1!=8:
if board[valid_moves[z][0]+1][valid_moves[z][1]]==1:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=2
while (valid_moves[z][0]+k)!=8 and board[valid_moves[z][0]+k][valid_moves[z][1]]==1:
board[valid_moves[z][0]+k][valid_moves[z][1]]=2
value[z]+=1
k+=1
decoy_list.append(board)
if (valid_moves[z][1])-1!=-1:
if board[valid_moves[z][0]][valid_moves[z][1]-1]==1:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=2
while (valid_moves[z][1]-k)!=-1 and board[valid_moves[z][0]][valid_moves[z][1]-k]==1:
board[valid_moves[z][0]][valid_moves[z][1]-k]=2
value[z]+=1
k+=1
decoy_list.append(board)
if (valid_moves[z][1])+1!=8:
if board[valid_moves[z][0]][valid_moves[z][1]+1]==1:
k=1
board[valid_moves[z][0]][valid_moves[z][1]]=2
while (valid_moves[z][1]+k)!=8 and board[valid_moves[z][0]][valid_moves[z][1]+k]==1:
board[valid_moves[z][0]][valid_moves[z][1]+k]=2
value[z]+=1
k+=1
decoy_list.append(board)
if z==len(matrix_list)-1:
points.append(value)
matrix_list=decoy_list[:]
decoy_list=[]
valid_move=[]
for x in range(len(matrix_list)):
valid_moves=[]
for i in range(8):
for j in range(8):
#valid_moves-valid_move for player 1
if depth%2==1:
if board[i][j]==2:
if i-1!=-1 and j-1!=-1:
if i+1!=8 and j+1!=8:
if board[i-1][j]==1 and board[i+1][j]==0:
valid_moves.append([i+1,j])
if board[i][j+1]==1 and board[i][j-1]==0:
valid_moves.append([i,j-1])
if board[i+1][j]==1 and board[i-1][j]==0:
valid_moves.append([i-1,j])
if board[i][j-1]==1 and board[i][j+1]==0:
valid_moves.append([i,j+1])
if board[i-1][j+1]==1 and board[i+1][j-1]==0:
valid_moves.append([i+1,j-1])
if board[i+1][j-1]==1 and board[i-1][j+1]==0:
valid_moves.append([i-1,j+1])
if board[i-1][j-1]==1 and board[i+1][j+1]==0:
valid_moves.append([i+1,j+1])
if board[i+1][j+1]==1 and board[i-1][j-1]==0:
valid_moves.append([i-1,j-1])
#valid_moves-valid_move for player 2
if depth%2==0:
if board[i][j]==1:
if i-1!=-1 and j-1!=-1:
if i+1!=8 and j+1!=8:
if board[i-1][j]==2 and board[i+1][j]==0:
valid_moves.append([i+1,j])
if board[i][j+1]==2 and board[i][j-1]==0:
valid_moves.append([i,j-1])
if board[i+1][j]==2 and board[i-1][j]==0:
valid_moves.append([i-1,j])
if board[i][j-1]==2 and board[i][j+1]==0:
valid_moves.append([i,j+1])
if board[i-1][j+1]==2 and board[i+1][j-1]==0:
valid_moves.append([i+1,j-1])
if board[i+1][j-1]==2 and board[i-1][j+1]==0:
valid_moves.append([i-1,j+1])
if board[i-1][j-1]==2 and board[i+1][j+1]==0:
valid_moves.append([i+1,j+1])
if board[i+1][j+1]==2 and board[i-1][j-1]==0:
valid_moves.append([i-1,j-1])
valid_move.append(valid_moves)
print len(valid_move)
print len(matrix_list)
depth+=1