Connect Four Winner
Problem statement
Simulate a standard Connect Four game on a board with 6 rows and 7 columns.
The integer array moves lists the chosen column for each move in chronological order. Player 1 moves first, and the players alternate. A disc falls into the lowest empty cell in its chosen column.
Return the player number of the first player who forms four of their discs in a contiguous horizontal, vertical, or diagonal line. Return 0 if the sequence ends without a winner.
Every supplied move is legal, and the sequence contains no move after a player has won.
Function
connectFourWinner(moves: int[]) → intExamples
Example 1
moves = [3,2,3,2,3,2,3]return = 1Player 1 places four discs in column 3, so the final move completes a vertical line.
Example 2
moves = [0,0,1,1,2,2,3]return = 1Player 1 occupies the bottom cells of columns 0 through 3 and wins horizontally.
Example 3
moves = [0,1,2,3]return = 0Neither player has four connected discs when the move sequence ends.
Constraints
1 <= moves.length <= 42.- Every value in
movesis an integer from0through6. - Each move selects a column that is not full.
- Player
1moves first, then the players alternate. - The sequence stops at the first winning move or before any player wins.