Design an Animal Chess OOP simulation
Company: Duolingo
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates object-oriented design and software engineering skills, specifically class modeling, encapsulation, state management, and rule-based logic for a game simulation.
Constraints
- Board size is fixed at 9 x 7.
- At most 16 pieces are on the board at any time.
- 0 <= len(actions) <= 1000.
- Each player has at most one of each piece type.
Examples
Input: [('clear',), ('place', 'A', 'Lion', 0, 0), ('place', 'A', 'Tiger', 0, 6), ('place', 'A', 'Dog', 1, 1), ('place', 'A', 'Cat', 1, 5), ('place', 'A', 'Rat', 2, 0), ('place', 'A', 'Leopard', 2, 2), ('place', 'A', 'Wolf', 2, 4), ('place', 'A', 'Elephant', 2, 6), ('place', 'B', 'Elephant', 6, 0), ('place', 'B', 'Wolf', 6, 2), ('place', 'B', 'Leopard', 6, 4), ('place', 'B', 'Rat', 6, 6), ('place', 'B', 'Cat', 7, 1), ('place', 'B', 'Dog', 7, 5), ('place', 'B', 'Tiger', 8, 0), ('place', 'B', 'Lion', 8, 6), ('snapshot',)]
Expected Output: [True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, [(0, 0, 'AL'), (0, 6, 'AT'), (1, 1, 'AD'), (1, 5, 'AC'), (2, 0, 'AR'), (2, 2, 'AP'), (2, 4, 'AW'), (2, 6, 'AE'), (6, 0, 'BE'), (6, 2, 'BW'), (6, 4, 'BP'), (6, 6, 'BR'), (7, 1, 'BC'), (7, 5, 'BD'), (8, 0, 'BT'), (8, 6, 'BL')]]
Explanation: The board is cleared, all 16 standard pieces are manually placed, and `snapshot` returns the row-major board state.
Input: [('clear',), ('place', 'A', 'Lion', 3, 0), ('place', 'B', 'Dog', 3, 3), ('move', 'A', 3, 0, 3, 3), ('piece', 3, 3), ('winner',)]
Expected Output: [True, True, True, True, 'AL', 'A']
Explanation: The Lion jumps horizontally across the river from `(3,0)` to `(3,3)`, captures the Dog, and wins because `B` has no remaining pieces.
Hints
- Use a base Piece class for shared behavior, then override only the special cases: Rat, Elephant, Lion, and Tiger.
- Validate a move in phases: turn/bounds, terrain, movement pattern, capture rule, then win-condition update.