Discover how a simple hierarchy can turn chaotic game code into a clean, powerful design!
Why Board and piece hierarchy in LLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine trying to build a chess game by writing separate code for each piece and board position without any shared structure or rules.
You would have to manually handle every piece's movement, capture, and interaction individually.
This manual approach quickly becomes confusing and full of repeated code.
It's easy to make mistakes, like forgetting a rule or mixing up piece behaviors.
Adding new pieces or changing rules means rewriting lots of code, which is slow and error-prone.
Using a board and piece hierarchy organizes the game elements into clear layers.
Pieces inherit common behaviors, and the board manages positions and moves uniformly.
This structure reduces repetition, makes rules easier to enforce, and simplifies adding new pieces or features.
if piece == 'pawn': move_pawn() if piece == 'rook': move_rook() if piece == 'bishop': move_bishop()
class Piece: def move(self): pass class Pawn(Piece): def move(self): # pawn move logic pass class Rook(Piece): def move(self): # rook move logic pass
This hierarchy enables building complex board games that are easy to maintain, extend, and understand.
Chess apps use board and piece hierarchies to handle all pieces with shared rules and unique moves, making the game logic clean and scalable.
Manual coding of each piece and move is repetitive and error-prone.
A hierarchy groups shared behaviors and simplifies management.
It makes adding new pieces or rules easier and safer.
Practice
Piece class in a board game design?Solution
Step 1: Understand the role of a base class
A base class provides shared properties and methods for all derived classes, avoiding repetition.Step 2: Apply to board game pieces
All pieces share common traits like position and type, so the basePiececlass holds these.Final Answer:
To define common properties like position and type for all pieces -> Option BQuick Check:
Base class = common properties [OK]
- Confusing board layout storage with piece properties
- Thinking base class handles user input
- Assuming base class manages network tasks
King that extends a base Piece class in a typical object-oriented design?Solution
Step 1: Identify correct subclass syntax
In modern OOP, a subclass usesextendskeyword and callssuper()in constructor.Step 2: Check each option
class King extends Piece { constructor(position) { super(position); } } uses correct syntax:class King extends Piece { constructor(position) { super(position); } }.Final Answer:
class King extends Piece { constructor(position) { super(position); } } -> Option AQuick Check:
Subclass syntax = extends + super() [OK]
- Using incorrect keywords like inherits
- Placing extends after function declaration
- Trying to add properties with '+' operator
console.log(board.pieces[0].type);?
class Piece {
constructor(type, position) {
this.type = type;
this.position = position;
}
}
class Board {
constructor() {
this.pieces = [];
}
addPiece(piece) {
this.pieces.push(piece);
}
}
const board = new Board();
board.addPiece(new Piece('Knight', 'B1'));
Solution
Step 1: Understand object creation and storage
A newPiecewith type 'Knight' and position 'B1' is created and added toboard.pieces.Step 2: Access the first piece's type
board.pieces[0]refers to the first piece, soboard.pieces[0].typeis 'Knight'.Final Answer:
"Knight" -> Option DQuick Check:
First piece type = 'Knight' [OK]
- Confusing position with type
- Assuming pieces array is empty
- Expecting an error due to missing pieces
class Piece {
constructor(type, position) {
this.type = type;
this.position = position;
}
}
class Queen extends Piece {
constructor(position) {
this.type = 'Queen';
this.position = position;
}
}Solution
Step 1: Review subclass constructor rules
In subclasses, the constructor must callsuper()before usingthis.Step 2: Check Queen constructor
Queen constructor assignsthis.typeandthis.positionwithout callingsuper(), causing an error.Final Answer:
Missing call to super() in Queen constructor -> Option CQuick Check:
Subclass constructor must call super() first [OK]
- Forgetting super() call in subclass constructor
- Trying to assign this before super()
- Assuming constructor is optional in subclass
Solution
Step 1: Understand scalability and extensibility
Good design allows adding new piece types without modifying existing code, following open-closed principle.Step 2: Evaluate design options
SubclassingPiecelets each piece implement its own move logic, enabling easy extension.Final Answer:
Use a base Piece class and create subclasses for each piece type implementing their own move logic -> Option AQuick Check:
Subclassing = scalable and extensible design [OK]
- Using large switch-case blocks that are hard to maintain
- Handling moves globally with if-else reduces flexibility
- Hardcoding moves in board class limits scalability
