Skip to main content
1 of 3
dreamcrash
  • 1.1k
  • 1
  • 12
  • 24

To me the easy human way it is to start from the square that you want to go to (backtracking approach), lets call it 'A', because you can exclude from the path calculation the direct linked paths from 'A' that you can't go to because they are attack. For example, you want to go to the square c4, so the directed linked paths will be from a5, a3, b2, d2, e3, e5, d6 and b6, if only the square a5 and b6 are not attack you reduce your calculation to only to paths now. Furthermore, if you use this "technique" you can check if the square is reachable at all (if have at least 1 directed linked path not attacked).

After doing that, you go to the position where you knight stands and you check which are the squares that you can go to without being attack. Using this two phase approach you reduce you tree of calculation immensely.

You should also know beforehand how many moves you need to go from one square to another with your knight, to avoid that you calculate larger paths than the ones really need it. I din't check online to find any heuristic for this but I can give you some tips about this:

-> The Knight on a black square can only go to a white square and vise-versa; -> Every square on the diagonal of the actual square of the Knight can be reach in two only moves. Square (x,y) to the squares (x-1,y+1), (x+1,y+1), (x+1,y-1) and (x-1,y-1) takes 2 moves; -> The squares up, above, right and left of the actual square takes 3 moves; -> Every square on the diagonal of the actual square with a one square between them (e.g actual square = c4, target square a6), takes 4 moves. -> Every square on the diagonal of the actual square with a 2 squares between them (e.g actual square = c4, target square f7), takes only 2 moves.

Finally, if you are the a given color square you will take a odd number of moves to reach the opposite color square. If the target square have the same color as the square that you are in now it will take a even number of moves.

dreamcrash
  • 1.1k
  • 1
  • 12
  • 24