Need help to determine winner for a game

Anyone need your help. I am doing the coding for the Connect 4 game, wherein there where 6 rows and 7 columns in a board. I was able to create the board and have the players to input their turns on the board using list as a container to the board.

I am now figuring it out how to identify the winner with 4 straight same piece in a row or column or diagonal.

Any idea on how to determine this function? Many thanks

What’s the datastructure you’ve used to store the board state? What particular problems are you having: enumerating all the rows, or finding if a row has 4-in-a-row?

The simple way to me is just to walk all the rows, all the columns, and all the diagonals and see if there are four of a single player chip in a row. Especially in an interactive game like this, there’s no need for something super fast.

Loop over all the tokens in a row, increment a counter if the current element is the same as the last one. Declare a winner if your count hits 4 and it’s a player token (not an empty space).

You could alternatively use itertools.groupby as well on the row and just check each of the returned groups for player and length.

yeah i need to find 4 in a row or 4 in a column or 4 in diagonal of same player piece and then declare a winner.