Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-25-2007, 04:45 AM
Member
 
Join Date: Nov 2007
Posts: 1
goorioles747 is on a distinguished road
Knights Tour problem
I am trying to make a knights tour program. I was going to use stacks, but decided to just use an object array. The problem I have is when it backtracks, it doesnt always continue in the direction it left off from. here is my code:
(Vars is a class with variables x, y, and d) d is direction
my demo is simply:

Code:
KnightsTour5 knight=new KnightsTour5(1,1,size); knight.GO();


the rest:

import java.util.Stack;
public class KnightsTour5 {
Vars locs[]=new Vars[0];
private int size, max=1, d=0, board[][];

Code:
public KnightsTour(int x,int y, int newSize) { size=newSize; locs=new Vars[size*size+1]; for(int n=1;n<=size*size;n++) { locs[n]=new Vars(); } board=new int[size+1][size+1]; for(int n=1;n<=size;n++) { for(int n2=1;n2<=size;n2++) { board[n][n2]=0; } } locs[max].x=x; locs[max].y=y; locs[max].d=0; board[x][y]=max; } public void GO() { int n=0; while(n<250) { n++; d++; if(d>8) { board[locs[max].x][locs[max].y]=0; locs[max].d=0; max--; d=locs[max].d+1; } move(); } } public void move() { int x=locs[max].x, y=locs[max].y; switch(d) { case 1:x--;y-=2;System.out.print("a");break; case 2:x++;y-=2;System.out.print("b");break; case 3:x+=2;y--;System.out.print("c");break; case 4:x+=2;y++;System.out.print("d");break; case 5:x++;y+=2;System.out.print("e");break; case 6:x--;y+=2;System.out.print("f");break; case 7:x-=2;y++;System.out.print("g");break; case 8:x-=2;y--;System.out.print("h");break; } System.out.println(" X: "+x+" Y: "+y+" |"+(max+1)); if((x<1)||(x>size)||(y<1)||(y>size)){} else if(board[x][y]!=0){} else { max++; locs[max].x=x; locs[max].y=y; locs[max].d=d; board[x][y]=max; d=0; printBoard(); } } public void printBoard() { for(int n=1;n<=size;n++) { for(int n2=1;n2<=size;n2++) { if(board[n2][n]<10) System.out.print(board[n2][n]+" "); else System.out.print(board[n2][n]+" "); } System.out.println(); } System.out.println(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-26-2007, 05:54 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Your search algorithm goes from eleven o'clock clockwise through ten o'clock independent of location in the grid. Here's an idea that you might be able to implement: An Extremely Simple Solution.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply

« Search | Object ID »

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Knights Tour. atl2 New To Java 1 02-06-2008 09:31 PM


All times are GMT +3. The time now is 04:06 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org