Python grid checker -


i'm trying create grid game check if there winner , came code:

for j in range(1):# winning column 2 starting     win  = true     in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(1):# winning column 1 starting top     win  = true     in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(2):# winning column 2 starting     win  = true     in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(2):# winning column 2 starting top     win  = true     in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(3):# winning column 3 starting     win  = true     in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(3):# winning column 3 starting top     win  = true     in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(4):# winning column 4 starting     win  = true     in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(4):# winning column 4 starting top     win  = true     in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(5):# winning column 5 starting     win  = true     in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(5):# winning column 5 starting top     win  = true     in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(6):# winning column 6 starting     win  = true     in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  j in range(6):# winning column 6 starting top     win  = true     in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(1):# winning line 1 starting right     win  = true     j in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(6):# winning line 1 starting left     win  = true     j in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(2):# winning line 2 starting right     win  = true     j in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(2):# winning line 2 starting left     win  = true     j in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(3):# winning line 3 starting right     win  = true     j in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(3):# winning line 3 starting left     win  = true     j in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(4):# winning line 4 starting right     win  = true     j in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(4):# winning line 4 starting left     win  = true     j in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(5):# winning line 5 starting right     win  = true     j in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(5):# winning line 5 starting left     win  = true     j in range(5):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(6):# winning line 6 starting right     win  = true     j in range(1,6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  in range(6):# winning line 6 starting left     win  = true     j in range(6):         if gameboard[i][j] != player:             win = false             break     if win:         return true  win = true i, j in zip(range(5), range(5, -1, -1)):# diagonal right left starting top         if gameboard[i][j] != player:             win = false             break if win:     return true  win = true i, j in zip(range(1, 6), range(4, -1, -1)):# diagonal right left starting bottom         if gameboard[i][j] != player:             win = false             break if win:     return true    win = true i, j in zip(range(5), range(6)):# diagonal left right starting top         if gameboard[i][j] != player:             win = false             break if win:     return true  win = true i, j in zip(range(1,6), range(1 ,6, 1)):# diagonal left right starting bottom         if gameboard[i][j] != player:             win = false             break if win:     return true  win = true i, j in zip(range(1,6), range(6)):         if gameboard[i][j] != player:             win = false             break if win:     return true  win = true i, j in zip(range(1,6), range(5, 0, -1)):# parallel diagnol diagonal 2         if gameboard[i][j] != player:             win = false             break if win:     return true  win = true i, j in zip(range(6), range(1,6)):         if gameboard[i][j] != player:             win = false             break if win:     return true  win = true i, j in zip(range(6), range(4,-1,-1)):         if gameboard[i][j] != player:             win = false             break if win:     return true  return win 

my problem is long code , believe simplified don't how it... me please? thank :).

you can count method check if win possible. can "reverse" rules check continuous sequences - check non-player @ either end of range.

def win(vector, player):     """check row or col len-1 consecutive nodes owned player.     """     if vector.count(player) < len(vector) - 1:         return false     if vector[0] != player or vector[-1] != player:         return true     return false  def check_row(row, player):     return win(gameboard[row], player)  def check_col(col, player):     return win([gameboard[x][col] x in range(len(gameboard)), player) 

once have these, can iterate on rows , columns (or both) checking win:

def any_win(player):     if any([check_row(x, player) x in range(len(gameboard))]):         return true     if any([check_col(x, player) x in range(len(gameboard))]):         return true     return false 

Comments