Two Sigma OA 面试真题解析:Implement Connect6 连珠游戏设计

14次阅读
没有评论

Implement Connect6.

Connect6 is a simple board game similar to tic-tac-toe. There are two players, black and white, who alternate turns placing their colored pieces onto a grid. The first player to connect 6 or more of their pieces together horizontally, vertically, or diagonally wins.

Black places one piece first, then each player alternates placing two pieces onto the board. The board size should be K x K where K <= 10^9.

An example game may look something like this:

You should implement a class called Connect6 with the following methods:

  • constructor: Initializes the game with a k by k board.
  • getTurn: Retrieve which player should move next.
  • placeBlack: Place a black piece onto the board at the specified coordinates.
  • placeWhite: Place a white piece onto the board at the specified coordinates.

Return values:

  • getTurn: string, "black" if it is black’s turn, otherwise "white".
  • placeBlack / placeWhite: boolean, true if the move resulted in a win, else false.

这道题要求你实现一个 Connect6 棋类,核心是维护一个 K×K 的棋盘、当前轮到谁下,以及每次落子后是否形成了至少 6 子连线。由于棋盘规模上限很大,不能真的开二维数组,通常要用哈希表或集合保存已落子的坐标,只在需要时检查新落子所在的四个方向(横、竖、两条对角线)连续同色棋子数量,从而判断是否获胜。同时还要根据规则处理回合切换:黑方先手,之后双方轮流每次下两子。

正文完
 0