Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld Talkback >> 960365

Pages: 1
JavaWorld
addict


Reged: 06/20/03
Posts: 482
The knight's tour
      #24310 - 11/23/05 06:00 PM

The knight's tour

Post Extras: Print Post   Remind Me!   Notify Moderator  
BennySense
Unregistered




Re: The knight's tour [Re: JavaWorld]
      #24379 - 11/25/05 03:28 AM

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!

Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: The knight's tour [Re: JavaWorld]
      #24757 - 12/06/05 09:50 AM

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,


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: The knight's tour [Re: Anonymous]
      #24769 - 12/06/05 04:37 PM

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.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: The knight's tour [Re: Anonymous]
      #24812 - 12/07/05 09:08 PM

Thanks for the information.

Jeff


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: The knight's tour [Re: JavaWorld]
      #25077 - 12/17/05 01:05 AM

How do I actually arrive at the solution for this problem. I still did not understand the solution

Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: The knight's tour [Re: Anonymous]
      #25094 - 12/18/05 11:50 AM

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


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 1 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 6551

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5