I'm using the following function to determine if a winner has been crowned in connect four. Piece is whether they are green or red, last is the last played move (by piece), and name is the discord name of the person playing the game, as it is a file based connect four game. Board is a 2d array being made of all empty and filled squares. Due to the game being based in python, is this a effecient way to check?
Examples: Piece: :green_circle:
Board: [[':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:'], [':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:'], [':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:'], [':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:'], [':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:'], [':white_large_square:', ':green_circle:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:', ':white_large_square:']]
Last: 5,1
def checks(piece, last, name):
board = []
open_file = open(name, "r")
thing = open_file.readline()
for x in range(6):
value = open_file.readline()
board.append(value.strip("\n").split(","))
open_file.close()
cords = last.split(',')
i = int(cords[0]) # row/x
j = int(cords[1]) # column/y
# checks for 000_
if j > 2:
if board[i][j - 1] == piece and board[i][j - 2] == piece and board[i][
j - 3] == piece:
return piece + " won"
# checks for _000
if j < 4:
if board[i][j + 1] == piece and board[i][j + 2] == piece and board[i][
j + 3] == piece:
return piece + " won"
# checks for downs
if i < 3:
if board[i + 1][j] == piece and board[i + 2][j] == piece and board[
i + 3][j] == piece:
return piece + " won"
#check if you place in a 00_0
if not j in [0, 1, 6]:
if board[i][j + 1] == piece and board[i][j - 1] == piece and board[i][
j - 2] == piece:
return piece + " won"
#check for 0_00
if not j in [0, 5, 6]:
if board[i][j + 1] == piece and board[i][j + 2] == piece and board[i][
j - 1] == piece:
return piece + " won"
# check for top piece of a down-right diagonal
if i < 3 and j < 4:
if board[i + 1][j + 1] == piece and board[i + 2][j + 2] == piece and board[
i + 3][j + 3] == piece:
return piece + " won"
# check for bottom piece of a down-right diagonal
if i > 2 and j > 2:
if board[i - 1][j - 1] == piece and board[i - 2][j - 2] == piece and board[
i - 3][j - 3] == piece:
return piece + " won"
# check for top piece of down-left diagonal
if i < 3 and j > 2:
if board[i + 1][j - 1] == piece and board[i + 2][j - 2] == piece and board[
i + 3][j - 3] == piece:
return piece + " won"
# check for bottom piece of down-left diagonal
if i > 2 and j < 4:
if board[i - 1][j + 1] == piece and board[i - 2][j + 2] == piece and board[
i - 3][j + 3] == piece:
return piece + " won"
# check for 2nd top piece of down-right diagonal
if i in [1,2,3] and j in [1,2,3,4]:
if board[i - 1][j - 1] == piece and board[i +1 ][j + 1] == piece and board[i +2][j +2] == piece:
return piece + " won"
# check for 3rd piece of down-right diagonal
if i in [2,3,4] and j in [2,3,4,5]:
if board[i - 1][j - 1] == piece and board[i -2 ][j -2] == piece and board[i +1][j +1] == piece:
return piece + " won"
# check for 2nd piece of down-left diagonal
if i in [1,2,3] and j in [2,3,4,5]:
if board[i - 1][j + 1] == piece and board[i +1 ][j -1] == piece and board[i +2][j -2] == piece:
return piece + " won"
# check for 3rd piece in down-left diagonal
if i in [2,3,4] and j in [1,2,3,4]:
if board[i - 1][j + 1] == piece and board[i +1 ][j -1] == piece and board[i -2][j +2] == piece:
return piece + " won"

Keeping in mind your conditions are apt, your code could be enhanced in the following manners:
all()elifin places ofifmin()to check inequality for smallest rather than checking bothiandjHere's just an enhanced version of your code:
If you could provide an exact input when there is a win, maybe a better approach could be made. Hope this helps :)