call(back)
Algorithms & Data StructureshardSign in to star problems

Animal Chess Engine

Implement a simplified Animal Chess (Dou Shou Qi) engine with object-oriented design, and drive it through the AnimalChess class's methods. The starter defines the constants: ranks, piece letters, dens, traps and the standard setup.

Board

9 rows by 7 columns, 0-based (row, col). Player A starts at the top, B at the bottom. Rivers are rows 3 to 5 in columns 1, 2, 4 and 5. A's den is (0, 3) and B's is (8, 3). A's traps are (0, 2), (0, 4), (1, 3); B's are (8, 2), (8, 4), (7, 3).

Pieces and movement

Elephant 8, Lion 7, Tiger 6, Leopard 5, Wolf 4, Dog 3, Cat 2, Rat 1. Every piece moves one square orthogonally. Only the Rat may enter river cells. The Lion and the Tiger may also jump in a straight line across exactly one river section, landing on the first land cell beyond it, unless a Rat stands on any jumped river cell. A piece may not move into its own den.

Captures and winning

A move onto an enemy piece captures it when allowed; a move onto an own piece is illegal. If the defender stands in a trap owned by the attacker the capture is always allowed. Otherwise the attacker's effective rank must be at least the defender's, where a piece standing in an enemy trap has effective rank 0. Exception: the Rat can capture the Elephant, and the Elephant cannot capture the Rat. Entering the opponent's den wins at once, and so does capturing the opponent's last piece. After a win no more moves are accepted.

API

  • clear() empties the board; init() loads the standard setup. Both reset the turn to A and the winner to none and return true.
  • place(owner, pieceName, row, col) puts one piece down; false when the square is off the board, occupied, the owner's own den, or a river cell for anything but a Rat.
  • move(owner, r1, c1, r2, c2) attempts a move for the player to move; it returns whether the move was legal and applied, and a legal move passes the turn.
  • piece(row, col) returns the code at a square or null; codes are the owner plus a letter, with P for Leopard: AE AL AT AP AW AD AC AR and the same for B.
  • snapshot() lists every piece as [row, col, code] in row-major order; display() returns the 9 rows of 7 codes with "__" for empty squares; turn() returns "A" or "B"; winner() returns "A", "B" or null.
clear; place A Lion at (3, 0); place B Dog at (3, 3); move A (3, 0) -> (3, 3)
piece(3, 3)  ->  "AL"     // the Lion jumped the river and captured the Dog
winner()     ->  "A"      // B has no pieces left

At most 1,000 actions per game. The interviewer wants classes with clear responsibilities as much as a correct engine: say what owns the rules, the board and the turn.

Hints

Hint 1

Separate the questions: can this piece reach that square (step or jump), can it capture what is there, and does the move end the game. Three small methods are easier to test than one move.

Hint 2

The jump check walks from the start toward the landing square: every cell between must be river, none may hold a Rat, and the landing square must be land. Diagonal or non-river paths fail immediately.

Hint 3

Keep the trap rule in one place: a defender in the attacker's trap is always capturable; otherwise compare ranks with the enemy-trap-means-zero adjustment and the Rat-Elephant exception.

Topics

Asked at

Similar Problems6