KatInterview

Simulate Cascade Board Game Outcome

Arrays & HashingModel answer
MediumModel answerArrays & Hashingarrayssimulationmatrix

Last updated

Simulate a custom board game involving stack placing and directed toppling moves to determine the game outcome.

You are tasked with writing a simulator for a two-player grid-based board game.

The board consists of an $N \times N$ grid, where rows and columns are 0-indexed ($3 \le N \le 9$). Two players, Player 1 and Player 2, execute a series of turns described by a list of move strings.

There are two types of moves:

  1. Place Move ("pXY"):

    • Places a single chip at grid location $(X, Y)$, where $X$ is the row and $Y$ is the column.
    • A player can only place a chip on an unowned cell or a cell they currently own.
    • If the target cell is empty, the active player claims ownership of that cell and sets its stack height to 1.
    • If the cell is already owned by the active player, the stack height increases by 1.
    • Turn Rule: The active player changes on every Place move. Player 1 makes the very first Place move, followed by Player 2, and so on.
  2. Topple Move ("tXYD"):

    • Topples a stack of chips at $(X, Y)$ in direction $D \in {\text{'u'}, \text{'r'}, \text{'d'}, \text{'l'}}$ (representing up, right, down, and left).
    • Up moves towards row $X-1$, right towards column $Y+1$, down towards row $X+1$, and left towards column $Y-1$.
    • A player may only execute a Topple move from a cell they own that has a stack height $h \ge 2$.
    • All $h$ chips are removed from $(X, Y)$ (making $(X, Y)$ an empty, unowned cell). The chips are then distributed one-by-one into consecutive cells along direction $D$, starting at the cell immediately adjacent to $(X, Y)$.
    • Any chip dropped outside the $N \times N$ board boundary is permanently lost, and all remaining chips in that topple action are also lost off-board.
    • Capture Mechanics: When a dropped chip lands on an empty cell, the active player claims it with a stack height of 1. When it lands on a cell owned by the active player, its stack height increases by 1. When it lands on a cell owned by the opponent, the active player captures that entire cell stack: the stack height increases by 1, and ownership of the cell (and all chips previously in that stack) converts to the active player.
    • Turn Rule: Topple moves do not alternate turns. Any Topple move belongs to the player who performed the most recent Place move.

Game End Condition:

  • A player loses immediately if they have 0 total chips remaining anywhere on the board.
  • The game can only end as a result of a Topple move (a Place move can never reduce total piece count). If a Topple move causes a player's piece count on the board to drop to 0, the game terminates immediately, and the other player wins. Any subsequent moves in the list are ignored.

Given $N$ and a list of valid, well-formed move strings moves, return one of the following strings:

  • "player 1 is the winner"
  • "player 2 is the winner"
  • "in progress" (if all moves are processed without any player reaching 0 chips)

Example 1

N = 3
moves = ["p10", "p12", "p10", "t10r"]
# Output: "player 1 is the winner"

Example 2

N = 3
moves = ["p00", "p22", "p02", "p22", "t22u", "t02l"]
# Output: "player 2 is the winner"

Hints

Hint 1

Maintain separate state representations for cell ownership (0, 1, or 2) and height, along with active piece counts for both players.

Hint 2

Note that turn switching depends strictly on Place moves. Consecutive Topple moves belong to the player who executed the last Place move.

Hint 3

When a piece lands on an enemy stack during a Topple, ownership of the ENTIRE stack flips to the active player, and the stack height increments by 1.