Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 02-06-2008, 06:52 AM
Member
 
Join Date: Feb 2008
Posts: 3
atl2 is on a distinguished road
Knights Tour.
Hello all, Im having a bit of trouble with a project.

I need to create a program that finds a solution to the knights tour problem (a chess Knight must travel to each space on a chess board, but can only enter each space once.)

The code I wrote seems to work when i make the base case 63 (but one square is unreached), when the base case is 64 it seems to loop forever. Anyhelp would surely be appreciated.

Thanks in advance

Code:
public class Main { public static void main(String args[]) { int count = 1; Tour knightsTour = new Tour(); System.out.println("Please Wait.."); knightsTour.check( 0, 0, count); knightsTour.printBoard(); } }
Code:
public class Tour { int board[][]; boolean done; final int xaval[] = {-1, 1, -1, 1, -2, 2, -2, 2}; final int yaval[] = {-2, 2, 2, -2, -1, 1, 1, -1}; public Tour() { board=new int[8][8]; done = false; board[0][0]=1; } public int check(int x, int y, int count) { for(int i = 0; i < 8; i++) { if(done == true) { return count; } else { if(x+xaval[i]<8 && x+xaval[i]>=0) { if(y+yaval[i]<8 && y+yaval[i]>=0) { //on the board if((board[x+xaval[i]][y+yaval[i]])==0) { board[x+xaval[i]][y+yaval[i]]=-1; board[x+xaval[i]][y+yaval[i]]=check(x+xaval[i],y+yaval[i], count + 1); } } } } } if(count >= 64 || done == true) { done = true; return count; } else { return 0; } } public void printBoard() { //Draw Board for(int i = 0; i < 8; i++) { System.out.println("\n________________________"); for(int j = 0; j < 8; j++) { if(board[j][i] < 10) { System.out.print("|0" + board[j][i]); } else { System.out.print("|" + board[j][i]); } } } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-06-2008, 08:31 PM
Member
 
Join Date: Feb 2008
Posts: 3
atl2 is on a distinguished road
Any Help PLEASE....

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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 problem goorioles747 New To Java 1 11-26-2007 04:54 AM


All times are GMT +3. The time now is 03:37 PM.


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