The Trapped Knight

Introduction

In this short workshop, we will explore the Trapped Knight problem and its visualization.

The trapped knight problem is a mathematical puzzle in which a chess knight moves on an infinite board whose squares are numbered in an outward spiral. Starting from the center, the knight must always move to the lowest-numbered square it has not yet visited. Despite this simple rule, the knight follows a surprisingly intricate path and eventually becomes trapped, unable to make any further legal moves. To see the problem in action, watch the following video by Numberphile: The Trapped Knight.

We will solve this problem by simulating the knight’s moves and visualizing the path it takes on a grid (just like in the video).

Simulation

At first glance, it may seem natural to model the chessboard as a large 2D array: for example, one array could store the spiral numbering of the squares, while another could track whether each square has been visited.

The difficulty is that we do not know in advance how large the board needs to be. In principle, the knight might continue moving outward indefinitely before becoming trapped, or perhaps never become trapped at all. Using a fixed-size array would therefore require choosing an arbitrary board size beforehand.

Instead, we take a more flexible approach and represent the knight’s trajectory directly as a list of visited positions. To support this, we first define two functions that convert between spiral indices and Cartesian coordinates (see the discussion here).

We can then simulate the knight’s movement step by step until it either becomes trapped or reaches a prescribed step limit.

Visualization

We plot the knight’s path on the grid using a color gradient to indicate the progression of steps over time. The resulting visualization reveals the intricate pattern of the knight’s trajectory and highlights the point at which it becomes trapped.

Exercise

Solve and visualize a variant of the trapped knight problem described in the Numberphile video, where the knight starts at the top left corner and the board is numbered in anti-diagonal stripes instead of a spiral.

References