The code checks many more conditions like the one below. I was thinking to memoize it, but I can't think about how (writers block). How else could I optimize this? I know it seems silly, but my code is spending the majority of the time in the function.
def has_three(p, board):
"""Checks if player p has three in a row"""
# For every position on the board
for i in xrange(6):
for j in xrange(7):
if board[i][j] == p:
if i<=2 and board[i+1][j]==p and board[i+2][j]==p and board[i+3][j]==0:
return True
if i>=3 and board[i-1][j]==p and board[i-2][j]==p and board[i-3][j]==0:
return True
if j<=3 and board[i][j+1]==p and board[i][j+2]==p and board[i][j+3]==0:
return True
if j>=3 and board[i][j-1]==p and board[i][j-2]==p and board[i][j-3]==0:
return True
if i<=2 and j<=3 and board[i+1][j+1]==p and board[i+2][j+2]==p and board[i+3][j+3]==0:
return True
if i<=2 and j>=3 and board[i+1][j-1]==p and board[i+2][j-2]==p and board[i+3][j-3]==0:
return True
if i>=3 and j<=3 and board[i-1][j+1]==p and board[i-2][j+2]==p and board[i-3][j+3]==0:
return True
if i>=3 and j>=3 and board[i-1][j-1]==p and board[i-2][j-2]==p and board[i-3][j-3]==0:
return True
return False
==0
are correct here. \$\endgroup\$