KatInterview

Build a Jungle Board Game Engine with Class Hierarchies

Data Structure DesignModel answer
HardModel answerData Structure Designobject-oriented-designgame-enginepolymorphism

Last updated

Design the classes for a two-player Jungle (Dou Shou Qi) engine: eight animals, a 9x7 board with water, traps and dens, capture and jump rules, a small API, and tests.

You are building the rules engine for a local, two-player version of the traditional board game Jungle (also called Animal Chess or Dou Shou Qi). There is no AI opponent and no GUI.

Your design should cover:

  • Eight animals: Elephant, Lion, Tiger, Leopard, Wolf, Dog, Cat and Rat. Each has its own movement and capture behaviour.
  • A 9-row by 7-column board with two lakes, trap squares, and one den for each player.
  • Move and capture logic, including swimming, river jumping and trap effects.
  • A compact public API that can set up a game, place pieces, list legal moves for a piece, play a move, and print the board as text.
  • Test cases for at least three scenarios: the starting position, a Lion capturing across a river, and several turns played in a row.

Before writing any classes, state the rules you are assuming. Then explain which class owns which responsibility and why that split makes the design easy to extend.

Hints

Hint 1

Separate 'may this animal take that one?' from 'what is the situation on the board?'. If the animal answers the first and the board supplies facts such as terrain and trap ownership, the board never needs to know about specific species.

Hint 2

Most animals follow the default rules and differ in one respect only (swims, jumps, or a special capture rule). Model that as small overridable attributes and methods instead of a long if/else chain on the animal type.

Hint 3

A trap is a property of the square the defender stands on, relative to the attacker's side. Do not mutate the defender's rank; just pass 'defender is in a trap of the attacker's side' into the capture check.