Another Card Game: Design and Implement a Turn-Based Card Game Engine with OOD
Question Details
Problem
Design an object-oriented engine for a simplified turn-based card game. Two players take turns drawing and playing cards. Cards have types: Attack (reduces opponent HP), Heal (restores own HP), Shield (blocks next attack). Game ends when a player's HP reaches 0.
Design the class structure, then implement play_turn(player, card).
python
class Card:
def __init__(self, name: str, card_type: str, value: int): ...
class Player:
def __init__(self, name: str, hp: int, deck: list): ...
def draw(self) -> Card: ...
def is_alive(self) -> bool: ...
class Game:
def __init__(self, player_a: Player, player_b: Player): ...
def play_turn(self, attacker: Player, card: Card) -> str: ...
**returns** a description of what happened
def is_over(self) -> bool: ...
def winner(self): ...
Example turn:
card = Card("Fireball", "Attack", 20)
game.play_turn(alice, card)
# "Alice plays Fireball. Bob takes 20 damage. Bob HP: 80."
Follow-ups
- How would you implement the
Shieldmechanic so it persists across turns and stacks correctly? - How would you add a
Deckclass with shuffle, draw, and discard-pile management? - How would you extend the engine to support card effects that trigger on specific game events (e.g., "on draw" or "on death")?
- How would you serialize the full game state to JSON for save/resume functionality?
Full Details
Problem
Design an object-oriented engine for a simplified turn-based card game. Two players take turns drawing and playing cards. Cards have types: Attack (reduces opponent HP), Heal (restores own HP), Shield (blocks next attack). Game ends when a player's HP reaches 0.
Design the class structure, then implement play_turn(player, card).
python
class Card:
def __init__(self, name: str, card_type: str, value: int): ...
class Player:
def __init__(self, name: str, hp: int, deck: list): ...
def draw(self) -> Card: ...
def is_alive(self) -> bool: ...
class Game:
def __init__(self, player_a: Player, player_b: Player): ...
def play_turn(self, attacker: Player, card: Card) -> str: ...
**returns** a description of what happened
def is_over(self) -> bool: ...
def winner(self): ...
Example turn:
card = Card("Fireball", "Attack", 20)
game.play_turn(alice, card)
# "Alice plays Fireball. Bob takes 20 damage. Bob HP: 80."
Follow-ups
- How would you implement the
Shieldmechanic so it persists across turns and stacks correctly? - How would you add a
Deckclass with shuffle, draw, and discard-pile management? - How would you extend the engine to support card effects that trigger on specific game events (e.g., "on draw" or "on death")?
- How would you serialize the full game state to JSON for save/resume functionality?
About This Question
This is a reported interview question from a scale ai interview during the phone round.
It covers the following topics: Oop, Ood, Phone, Coding, Onsite .