Simulate Cascade Board Game Outcome
Arrays & HashingModel answerLast 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:
-
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.
-
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.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach & Architecture
This problem is a stateful discrete simulation. To solve it cleanly, we need to track:
owner[r][c]: The owner of cell $(r, c)$ ($0$ for unowned, $1$ for Player 1, $2$ for Player 2).height[r][c]: The stack height at cell $(r, c)$.pieces[player]: The total number of pieces currently on the board forplayer($1$ or $2$). Updating this incrementally gives $O(1)$ win-condition checks.current_player: Tracks which player is taking the turn. This value defaults to Player 2 before any move, such that the first Place move toggles it to Player 1.
Turn Progression
- A Place move toggles
current_player = 3 - current_player(flipping between $1$ and $2$). - A Topple move keeps
current_playerunchanged.
Handling Topples & Captures
When a stack at $(X, Y)$ with height $h$ topples in direction $D$:
- Remove all $h$ pieces from $(X, Y)$, clearing its owner to $0$ and height to $0$. Subtract $h$ from
pieces[current_player]. - Move step-by-step along direction $D$:
- If the next position $(r, c)$ falls out of grid bounds ($r < 0, r \ge N, c < 0, c \ge N$), the remaining pieces are lost off-board, so terminate the loop.
- If $(r, c)$ is unowned or owned by
current_player:- Update
owner[r][c] = current_player. - Increment
height[r][c]by 1 andpieces[current_player]by 1.
- Update
- If $(r, c)$ is owned by the opponent (
opp = 3 - current_player):- Subtract
height[r][c]frompieces[opp]. owner[r][c]becomescurrent_player.- Increment
height[r][c]by 1. - Add the new
height[r][c]topieces[current_player].
- Subtract
Game Termination Check
After completing each Topple move:
- Check if
pieces[1] == 0. If true, return"player 2 is the winner". - Check if
pieces[2] == 0. If true, return"player 1 is the winner".
If all moves are processed and neither player has lost all pieces, return "in progress".
Python 3 Implementation
from typing import List
def game_outcome(n: int, moves: List[str]) -> str:
# 0 = unowned, 1 = Player 1, 2 = Player 2
owner = [[0] * n for _ in range(n)]
height = [[0] * n for _ in range(n)]
# Track piece count for player 1 and player 2 (1-indexed for convenience)
pieces = [0, 0, 0]
# Active player tracker; flips to 1 on the first Place move
current_player = 2
# Direction mappings for offsets
directions = {
'u': (-1, 0),
'd': (1, 0),
'l': (0, -1),
'r': (0, 1)
}
for move in moves:
move_type = move[0]
x, y = int(move[1]), int(move[2])
if move_type == 'p':
# Turn alternates on every Place move
current_player = 3 - current_player
if owner[x][y] == 0:
owner[x][y] = current_player
height[x][y] += 1
pieces[current_player] += 1
elif move_type == 't':
# Topple move: executed by the player who made the last Place move
direction = move[3]
dr, dc = directions[direction]
h = height[x][y]
# Lift all pieces from the source stack
owner[x][y] = 0
height[x][y] = 0
pieces[current_player] -= h
opponent = 3 - current_player
curr_r, curr_c = x, y
# Distribute pieces one-by-one along the direction vector
for _ in range(h):
curr_r += dr
curr_c += dc
# If off board, all remaining dropped pieces fall off and are lost
if not (0 <= curr_r < n and 0 <= curr_c < n):
break
if owner[curr_r][curr_c] == opponent:
# Capture opponent's stack
old_stack_height = height[curr_r][curr_c]
pieces[opponent] -= old_stack_height
owner[curr_r][curr_c] = current_player
height[curr_r][curr_c] = old_stack_height + 1
pieces[current_player] += height[curr_r][curr_c]
else:
# Land on empty or friendly cell
owner[curr_r][curr_c] = current_player
height[curr_r][curr_c] += 1
pieces[current_player] += 1
# Check win conditions only after a Topple move
if pieces[1] == 0:
return "player 2 is the winner"
if pieces[2] == 0:
return "player 1 is the winner"
return "in progress"
Complexity Analysis
- Time Complexity: $\mathcal{O}(M \times N)$ where $M$ is the number of moves. Each Place move takes $\mathcal{O}(1)$ time. Each Topple move traverses at most $\min(h, N)$ steps along a straight line before either running out of pieces or going off-board. Since $N \le 9$, each move operates in $O(1)$ operations practically.
- Space Complexity: $\mathcal{O}(N^2)$ auxiliary space to store the $N \times N$ board state (
ownerandheightmatrices).