Thanks again for your help! With it i was able to make a program that simulates the movement of an object in an infinite 2d-board. The movements of this object correspond to the movements of a knight in a chess-game.
This is the code for the program:
#include <stdio.h>
#include <stdlib.h>
int endLocations = 0;
int routes = 0;
int xNew, yNew;
void nextJump(int depth, int x, int y, int xEnd, int yEnd, int totalMovesAllowed) {
int xValue = x;
int yValue = y;
if(depth == totalMovesAllowed) {
if(xValue == xEnd && yValue == yEnd)
routes++;
endLocations++;
return;
}
int i;
for(i=1;i<9; i++) {
/** Represents all eight movements a Knight can make **/
switch(i) {
case 1:
xNew = xValue+2;
yNew = yValue+1;
break;
case 2:
xNew = xValue+2;
yNew = yValue-1;
break;
case 3:
xNew = xValue-2;
yNew = yValue+1;
break;
case 4:
xNew = xValue-2;
yNew = yValue-1;
break;
case 5:
xNew = xValue+1;
yNew = yValue+2;
break;
case 6:
xNew = xValue+1;
yNew = yValue-2;
break;
case 7:
xNew = xValue-1;
yNew = yValue+2;
break;
case 8:
xNew = xValue-1;
yNew = yValue-2;
break;
}
nextJump(depth+1, xNew, yNew, xEnd, yEnd, totalMovesAllowed);
}
}
int main (int argc, char *argv[]) {
int xBegin, yBegin, xEnd, yEnd, totalMovesAllowed;
printf("Check total amount of Knightjump routes\n");
printf("Coordinates begin position: x y: ");
scanf("%d %d", &xBegin, &yBegin);
printf("Coordinates end position: x y : ");
scanf("%d %d", &xEnd, &yEnd);
printf("Number of allowed jumps: ");
scanf("%d", &totalMovesAllowed);
/** Check 1 for increased efficiency
this is a simple check, checking for the minimum distance that needs to be covered to even reach the end-spot
**/
int deltaX, deltaY, movesNeededX, movesNeededY, totalMovesNeeded;
deltaX = xEnd-xBegin;
deltaY = yEnd-yBegin;
if(deltaX<0)
deltaX = deltaX*-1;
if(deltaY<0)
deltaY = deltaY*-1;
movesNeededX=deltaX/2;
movesNeededY=deltaY/2;
if(movesNeededX<1)
movesNeededX=1;
if(movesNeededY<1)
movesNeededY=1;
if(movesNeededX >= movesNeededY)
totalMovesNeeded = movesNeededX+1;
else
totalMovesNeeded = movesNeededY+1;
if(totalMovesAllowed >= totalMovesNeeded)
{
/** Check 2 for increased efficiency
This check checks for the fact that a knight-move, on a black/white-square colored board
will always move from black -> white or vice-versa.**/
int beginSquare, endSquare;
if((xBegin+yBegin)%2 != 0)
beginSquare = 0;
else
beginSquare = 1;
if((xEnd+yEnd)%2 != 0)
endSquare = 0;
else
endSquare = 1;
int difference = totalMovesAllowed%2;
if(beginSquare == endSquare && difference == 0) {
/** Checks passed - Call actual function **/
nextJump(0,xBegin,yBegin, xEnd, yEnd, totalMovesAllowed);
} else if(beginSquare == endSquare && difference != 0) {
/* not possible -> do nothing */
} else if(beginSquare != endSquare && difference == 0) {
/*not possible -> do nothing */
} else if(beginSquare != endSquare && difference != 0) {
/** Checks passed - Call actual function **/
nextJump(0,xBegin,yBegin, xEnd, yEnd, totalMovesAllowed);
}
}
printf("\nTotal amount of routes: %d\n", routes);
return 0;
}
Resulting in the following output with the given input:
However, i'm supposed to be making this program more efficient - so it can calculate upfront if a given end-position can be reached from the begin-position in the given amount of moves, so that it doesn't need to calculate all routes just to come to that conclusion.
For this i added 2 checks, i think the 2nd check is fairly simple and implemented correct, however, i'm not so sure on the first check.
If anyone can give some input regarding this it would be much appreciated. And thanks again, SVR!