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.
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.
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