Bird
0
0
LLDsystem_design~7 mins

Win condition checking in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
Without a clear and efficient way to check if a player has won, games can become slow or incorrect, causing poor user experience and frustration. Naive win checking can lead to redundant computations and delayed game responses.
Solution
Implement a systematic approach that checks the game state after each move to determine if a win condition is met. This involves tracking relevant game elements and applying concise logic to verify if a player has achieved the winning criteria immediately after their action.
Architecture
Player makes
a move
Update game
state
Check win
condition
Win
found

This diagram shows the flow from a player's move to updating the game state, then checking the win condition, and finally deciding if the game ends or continues.

Trade-offs
✓ Pros
Ensures immediate feedback to players about game outcomes.
Reduces unnecessary checks by focusing only on relevant parts of the game state.
Improves game responsiveness and user experience.
✗ Cons
Requires careful design to cover all possible win scenarios.
Can become complex for games with multiple or dynamic win conditions.
May add overhead if not optimized, especially in large or complex games.
Use when building turn-based or real-time games where immediate and accurate win detection is critical, especially for games with defined and limited win conditions.
Avoid in games where win conditions are probabilistic or evaluated only at the end, or when the game state is too large to check efficiently after each move.
Real World Examples
Nintendo
In games like Chess or Tic-Tac-Toe, Nintendo implements efficient win condition checks after each move to instantly declare a winner or continue play.
Blizzard Entertainment
In Hearthstone, win conditions are checked after each card play or attack to determine if a player’s health reaches zero, ending the game immediately.
Epic Games
In Fortnite, the system checks if a player is the last one standing after each elimination to declare the winner.
Code Example
The before code checks all rows, columns, and diagonals after every move, which is repetitive and inefficient. The after code keeps counters for rows, columns, and diagonals updated with each move, allowing instant win detection by checking only these counters.
LLD
### Before: Naive win checking after every move by scanning entire board
class Game:
    def __init__(self):
        self.board = [[None]*3 for _ in range(3)]

    def make_move(self, x, y, player):
        self.board[x][y] = player
        if self.check_win():
            print(f"Player {player} wins!")

    def check_win(self):
        # Check all rows, columns, diagonals
        for i in range(3):
            if self.board[i][0] == self.board[i][1] == self.board[i][2] != None:
                return True
            if self.board[0][i] == self.board[1][i] == self.board[2][i] != None:
                return True
        if self.board[0][0] == self.board[1][1] == self.board[2][2] != None:
            return True
        if self.board[0][2] == self.board[1][1] == self.board[2][0] != None:
            return True
        return False

### After: Optimized win checking by tracking last move
class Game:
    def __init__(self):
        self.board = [[None]*3 for _ in range(3)]
        self.rows = [0]*3
        self.cols = [0]*3
        self.diag = 0
        self.anti_diag = 0

    def make_move(self, x, y, player):
        val = 1 if player == 'X' else -1
        self.board[x][y] = player
        self.rows[x] += val
        self.cols[y] += val
        if x == y:
            self.diag += val
        if x + y == 2:
            self.anti_diag += val
        if abs(self.rows[x]) == 3 or abs(self.cols[y]) == 3 or abs(self.diag) == 3 or abs(self.anti_diag) == 3:
            print(f"Player {player} wins!")
OutputSuccess
Alternatives
End-of-game evaluation
Checks win conditions only after the game ends rather than after each move.
Use when: Use when game outcomes depend on cumulative scoring or complex end states rather than immediate moves.
Event-driven win checking
Triggers win checks only on specific events rather than every move.
Use when: Use when only certain actions can lead to a win, reducing unnecessary checks.
Summary
Win condition checking prevents slow or incorrect game outcomes by verifying wins immediately after moves.
Efficient implementations track relevant game state changes to avoid scanning the entire board repeatedly.
This pattern improves game responsiveness and user experience by providing instant feedback on game status.