KatInterview

Build a Fox-and-Geese Turn-Based Grid Game Engine

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented designgame enginesimulation

Last updated

Model the board state, movement rules, and win conditions for an asymmetric two-player grid game where geese try to reach the far side while foxes hunt them.

Background

You are building the core engine for a digital board game called Foxes and Geese, played on an 8 x 8 grid. Two players alternate turns:

  • Goose Player (GOOSE): Starts with 7 geese placed at row 7, columns 1 through 7. Geese move first.
  • Fox Player (FOX): Starts with 2 foxes placed at row 0, columns 2 and 5.

Movement Rules

Geese move exactly one cell in any orthogonal direction (up, down, left, right) into an unoccupied cell. They cannot jump over or capture any piece.

Foxes have two options on their turn:

  • Step: Move one cell orthogonally into an empty cell.
  • Leap: If a goose occupies an adjacent orthogonal cell and the cell directly beyond it (in the same direction) is empty and within bounds, the fox may jump over the goose and land there. The leaped-over goose is permanently removed from the board. Only one leap is permitted per turn.

Win Conditions

  • GOOSE wins if any goose reaches row 0, or if the foxes have no legal moves at the start of their turn.
  • FOX wins if fewer than 2 geese remain on the board, or if the geese have no legal moves at the start of their turn.

What to Implement

Design the object model and implement the class below. Explain your layering strategy, how move generation and validation share a single code path, how win conditions are evaluated, and what the complexity of each operation is.

class PieceType(Enum):
    GOOSE = "GOOSE"
    FOX = "FOX"

class Position:
    row: int
    col: int

class GameStatus(Enum):
    IN_PROGRESS = "IN_PROGRESS"
    GOOSE_WON = "GOOSE_WON"
    FOX_WON = "FOX_WON"

class FoxAndGeeseGame:
    def __init__(self):
        """
        Set up the 8x8 board with starting positions.
        GOOSE moves first.
        """
        pass

    def get_valid_moves(self, player: PieceType) -> list[tuple[Position, Position]]:
        """
        Return all legal (from_pos, to_pos) pairs for the given player
        in the current board state.
        """
        pass

    def make_move(self, from_pos: Position, to_pos: Position) -> GameStatus:
        """
        Validate and apply the move for the currently active player.
        Raise ValueError if the move is illegal or it is not that player's turn.
        Update game status and switch the active player.
        Return the resulting GameStatus.
        """
        pass

Constraints

  • Board coordinates satisfy 0 <= row < 8 and 0 <= col < 8.
  • No piece may move outside the board.
  • make_move must run in O(1) time relative to board size.
  • get_valid_moves must run in O(P) time, where P is the number of active pieces for that player.

Hints

Hint 1

Separate pure piece storage from game logic. A Board class that only handles placement, removal, and lookup makes it much easier to test the rules layer independently.

Hint 2

Generate and validate moves using the same function. One method, _moves_from(pos), produces every legal move for a single piece. make_move validates a requested move simply by checking whether the destination appears in that piece's candidate list. This guarantees the generator and validator can never disagree.

Hint 3

Evaluate win conditions in a fixed order immediately after applying the move: (1) did the moved goose land on row 0? (2) are fewer than 2 geese left? (3) does the opponent have any legal move? Checking stalemate last means you only reach that check when neither piece-count nor goal-row condition was triggered.