Skip to content
Advertisement

Recursive Java programming, Knight’s Tour driving me nuts

I’ve been working on a school project and can not figure out the problem. The problem that the knight jumps back where it was in the the last step when a dead end occurs.

I’ve added the output for a 4×4 test and you can clarly see that the knight jumps back to turn number 11 when it sees that there’s a dead-way from number 12. It then continues from turn number 11 and “solves the tour”.

Also I’m unsure how to continue if a pattern don’t solve the problem. Because then I need to somehow log that pattern so that I don’t go in the same pattern all over again. Sorry for my bad Engligh and thanks in advance.

JavaScript

Here’s the output for a 4×4:

JavaScript

Advertisement

Answer

I figured out the problem! I had to put else blocks in every 8th if(takeTour(x+-a, y+-b)) statements inside the takeTour(int x, int y), and then return false. Now I just wonder how I will be able to track the pattern so that I can go back one step and try a new one

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement