JavaWorld
addict
Reged: 06/20/03
Posts: 482
|
|
The knight's tour
|
BennySense
Unregistered
|
|
I like the tangent concept here of using chess and the movement of the knight. Its both creative and inspiring and I have decided to look into a few other board games to consider certain possibilities. One idea I've wanted to toy with is simulating monopoly situations (a game I played as a child). Best to you, keep submitting!
|
Anonymous
Unregistered
|
|
Here is another version of the boardPosition array. With this one, the knight ends up at a place where you could get back to position 1. So from this solution, all starting positions have been solved. Loop through the array and find your starting position, then run through the array looping back to boardPositions[0] when needed.
1,11,17,2,19,36,53,38, 55,40,23,8,14,31,48,63, 46,61,44,59,42,57,51,34, 49,43,60,45,62,56,39,29, 12,27,33,50,35,25,10,4, 21,6,16,22,7,24,30,20, 5,15,32,47,64,54,37,52, 58,41,26,9,3,13,28,18,
|
Anonymous
Unregistered
|
|
So to get the applet to work from any location. First adjust the drop down to show 64 positions instead of 2, then in the actionListener's run method, I got rid of boardPositions1 and 2, then added this line.. int[] moves = getMoves(c.getSelectedIndex()+1);
and changed the loop to this.. for (int i = 0; i < moves.length && thd != null; i++) { cb.moveKnight (moves[ i ]);
Now the final part was the getMoves method... private int[] getMoves(int startingPosition) { int [] boardPositions1 = { 1,11,17,2,19,36,53,38, 55,40,23,8,14,31,48,63, 46,61,44,59,42,57,51,34, 49,43,60,45,62,56,39,29, 12,27,33,50,35,25,10,4, 21,6,16,22,7,24,30,20, 5,15,32,47,64,54,37,52, 58,41,26,9,3,13,28,18 };
int retc[] = new int[64]; int offset=-1; // find starting position for (int i=0;i<boardPositions1.length;i++) { if (boardPositions1[ i ] == startingPosition) { offset = i; } } // now fill up the return for (int i=0;i<retc.length;i++) { retc[ i ] = boardPositions1[(i+offset)%64]; } return retc; }
And now you have a general solution.
|
Anonymous
Unregistered
|
|
Thanks for the information.
Jeff
|
Anonymous
Unregistered
|
|
How do I actually arrive at the solution for this problem. I still did not understand the solution
|
Anonymous
Unregistered
|
|
I worked out the solution through trial and error, and hardcoded board positions.
For more information on how to achieve a solution, you might want to check out the many Web sites dedicated to the Knight's Tour. An interesting Web page that might be helpful to you is <a href="http://www.borderschess.org/KTclosed.htm">Closed (Re-entrant) Knight Tours</a>.
Jeff
|
|