The knight's tour

Get a knight to visit each square on a chessboard exactly once

The game of chess provides many interesting diversions that are independent of the game itself. Many of these diversions are based on the strange L-shaped move of the knight. A classical example is the knight's tour.

The knight's tour, which has captured the attention of both mathematicians and puzzle aficionados since the beginning of the eighteenth century, places a knight on any one of a chessboard's 64 squares, and then proceeds to move that knight to each one of the remaining 63 squares, landing on each square exactly once.

This installment of Java Fun and Games presents a KT applet that demonstrates a restricted version of the knight's tour. Instead of starting from any square, the knight starts in one of the corner squares. This applet's GUI appears in Figure 1.

Figure 1. KT's GUI consists of a chessboard, a choice component for choosing the starting square, and a button for taking the tour

Before launching the tour, choose the knight's initial corner by selecting one of the items from the choice component. In response, the knight appears in the appropriate corner. (The knight defaults to the upper-left corner.) Then click the Take the Tour button to start the knight on its tour. Both the button and the choice component are disabled during the tour.

What does the tour look like? Figure 2 reveals a sequence of lines (a trail), where each line is drawn from the center of the previous square to the center of the current square as the knight moves around the chessboard.

Figure 2. This tour began in the upper-left corner

Now that you have seen both the applet's GUI and what a tour looks like, study its source code in Listing 1.

Listing 1. KT.java

1 2 Page 1