NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

(This question is a variation of the LeetCode question 348. Design Tic-Tac-Toe. If you haven't completed that question yet, it is recommended to solve it first.)

Design a Connect Four game played on a 6 × 7 grid, where two players, "A" and "B", take turns dropping discs into columns. Each disc will occupy the lowest available cell within the chosen column. The first player to form four consecutive discs of their own horizontally, vertically, or diagonally wins the game. If the board fills without a winner, the game ends in a draw.

Implement the ConnectFour class:

  • ConnectFour() Initializes the game with an empty 6 × 7 board.

  • String move(int col, String player) Drops a disc for player into column col (0-indexed). Returns:

    • "A" or "B" if the move results in that player winning by forming four consecutive discs.
    • "DRAW" if the board is full and neither player has won.
    • "PENDING" if the game is still ongoing.
  • String printBoard() Returns a string representation of the current board:

    • Each cell displays "A", "B", or a single space " " if empty.
    • Each row is shown as "|cell0|cell1|cell2|cell3|cell4|cell5|cell6|", with spaces representing empty cells.
    • Rows are listed from top (row 0) to bottom (row 5), separated by newline characters ("\n").

You may assume all moves are valid, no move will attempt to place a disc in a full column, and no moves will occur after the game has ended.

Constraints:

  • 1 ≤ moves.length ≤ 42
  • 0 ≤ col ≤ 6
  • player is either "A" or "B"

Example 1:

Input: ["ConnectFour", "move", "move", "move", "move", "move", "move", "move", "printBoard"] [[], [0, "A"], [1, "B"], [0, "A"], [1, "B"], [0, "A"], [1, "B"], [0, "A"], []]

Output: [null, "PENDING", "PENDING", "PENDING", "PENDING", "PENDING", "PENDING", "A", "| | | | | | | |\n| | | | | | | |\n|A| | | | | | |\n|A|B| | | | | |\n|A|B| | | | | |\n|A|B| | | | | |"]

Explanation: The final board state is as follows:

   0 1 2 3 4 5 6
0 | | | | | | | |
1 | | | | | | | |
2 |A| | | | | | |
3 |A|B| | | | | |
4 |A|B| | | | | |
5 |A|B| | | | | |
  • ConnectFour game = new ConnectFour();
  • game.move(0, "A"); // Returns "PENDING". "A" is placed in column 0, bottom row.
  • game.move(1, "B"); // Returns "PENDING". "B" is placed in column 1, bottom row.
  • game.move(0, "A"); // Returns "PENDING". "A" is placed above the previous "A" in column 0.
  • game.move(1, "B"); // Returns "PENDING". "B" is placed above the previous "B" in column 1.
  • game.move(0, "A"); // Returns "PENDING". "A" is placed in column 0.
  • game.move(1, "B"); // Returns "PENDING". "B" is placed in column 1.
  • game.move(0, "A"); // Returns "A". "A" achieves four in a vertical line and wins.
  • game.printBoard(); // Returns the string representation of the board as shown above.

Example 2:

Input:

["ConnectFour", "move", "move", "move", "move", "move", "move", "move", "printBoard"] [[], [0, "A"], [0, "B"], [1, "A"], [1, "B"], [2, "A"], [2, "B"], [3, "A"], []]

Output: [null, "PENDING", "PENDING", "PENDING", "PENDING", "PENDING", "PENDING", "A", "| | | | | | | |\n| | | | | | | |\n| | | | | | | |\n| | | | | | | |\n|B|B|B| | | | |\n|A|A|A|A| | | |"]

Explanation: The final board state is as follows:

   0 1 2 3 4 5 6
0 | | | | | | | |
1 | | | | | | | |
2 | | | | | | | |
3 | | | | | | | |
4 |B|B|B| | | | |
5 |A|A|A|A| | | |

Example 3:

Input: ["ConnectFour", "move", "move", "move", "move", "move", "move", "move", "move", "move", "move", "printBoard"] [[], [0, "A"], [1, "B"], [1, "A"], [2, "B"], [2, "A"], [3, "B"], [2, "A"], [3, "B"], [3, "A"], [4, "B"], []]

Output: [null, "PENDING", "PENDING", "PENDING", "PENDING", "PENDING", "PENDING", "PENDING", "PENDING", "PENDING", "B", "| | | | | | | |\n| | | | | | | |\n| | | | | | | |\n| | |A|A| | | |\n| |A|A|B| | | |\n|A|B|B|B|B| | |"]

Explanation: The final board state is as follows:

   0 1 2 3 4 5 6
0 | | | | | | | |
1 | | | | | | | |
2 | | | | | | | |
3 | | |A|A| | | |
4 | |A|A|B| | | |
5 |A|B|B|B|B| | |

Accepted
2.4k/7.4k
Acceptance Rate31.7%
Test Cases
Input
Output
Expected