Connect N
AirbnbPhone screenMedium
Build ConnectN(rows, cols, n), the engine of a Connect Four style game on an empty rows by cols board. Players drop pieces into columns, and each piece falls to the lowest empty cell of its column. Players may move in any order, and the game does not stop after a win: later moves are still played and judged the same way. Moves are only given for columns that are not full.
Each test builds one ConnectN and checks every return value.
Part 1: Drop a piece and detect the win
Implement move(col, player), which drops a piece for player (an integer) into column col.
Return True if that piece completes a line of at least n pieces of the same player, horizontally, vertically, or along either diagonal, passing through the newly dropped piece. Return False otherwise.
A line is judged only on the move that completes it: a line made by an earlier move does not count again on later moves.
- Example 1
- init
[4,4,3]- operations
[["move",[0,1]],["move",[1,2]],["move",[0,1]],["move",[1,2]],["move",[0,1]]]- Output
[false,false,false,false,true]
Why: Player 1 drops into column 0 on moves 1, 3 and 5 while player 2 answers in column 1. Only the fifth move completes three in a column, so it is the only
True.
Constraints
1 <= rows <= 1001 <= cols <= 1001 <= n <= max(rows, cols)0 <= col < cols1 <= player <= 10^91 <= len(operations) <= rows * cols- Every
movetargets a column that is not full