• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Knight Tour, Does the recursive call get placed inside the if block?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, of all let me say that the function needs to be implements recursively, in java, and MUST FIND a knights tour ever single time. I've spend alot of time trying to implement this and have gone through about 4 functions for the traverse. Alright the specific question is, where can I put the traverse function call recursively so that the function will tour the entire chess. Board as it is now the knight only moves once and the program returns false. I've tried to methods to doing this, one jumping the knight into the square and then checking if the knight is still on the board, and two checking if the knight can jump to the square first and then jumping if the spot is valid.

I know that this flies against both the philosophy and regulations of javaranch. For details, please look here: DoYourOwnHomework

Please ask a specific question about your code that can be answered and you will likely quickly receive many helpful answers.



I'm sorry about not showing that I've shown effort. But, I've got to point in the program where I just have hit a BRICK WALL, I keep taking tid bids of advice from many people and try to apply them. These two functions are a result of applying my own knowledge about programming concepts and advice from others. And the program ends up producing bad output on me. I have a hard time understanding recursion.



I noticed that whenever the knight moves out of the bounds of the array the program justs ends because it can no longer enter into any of the if statements.
if(hor < 0...) in the first move hor ends up being -1 or -2 so program just returns false at the end. I don't know how to fix this though. Any ideas? I think this has to do with backtracking. I don't know how to get this program to backtrack correctly.





 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jon Bud wrote:please have mercy and post a working version tested if possible that fulfill the above requirements.



I know that this flies against both the philosophy and regulations of javaranch. For details, please look here: DoYourOwnHomework

Please ask a specific question about your code that can be answered and you will likely quickly receive many helpful answers.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As it is, at most one of the 8 possible recursive calls can happen for any given position. You need to rearrange the code so that all 8 calls could potentially happen. This involves removing all the "else" keywords form the traverse method.

You should also check the return value of the recursive traverse calls. Whenever it returns "true", further execution should be stopped by calling "return true".
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only other thing I'd suggest would be to order your potential paths based on how many paths branch out from next square. You want to try the path with the fewest branches first as this will more readily and quickly lead you to a solution. Otherwise you'll be either running your program all day, or more likely, running out of memory.
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic