Hello, I've written a knight's tour program and handed in a working version of it already using a bunch of if statements for each possible move. I really wanted to use a couple of arrays for the moves and consolidate my code more, but I ran out of time. I do really want to get it working using the arrays for the moves however. I really thought that I had them set up correctly in the movePosition(), but I'm missing something (probably has to do with my for loop), but I can't seem to get my head around the missing/wrong logic I used with it. Any suggestions or what I should look at? I can post my working program (if statements) if needed, but this is what I've gotten so far with the arrays:
bool knightsTour::checkPosition (int r, int c, int size) //validates position
{
boardSize = size;
if((r < 0 || c < 0) || (r >= boardSize || c >= boardSize)) //within bounds
return false;
else
if((board[r][c]) == 0)
return true; //not occupied
else
return false;//occupied
}
void knightsTour::movePosition(int r, int c, int size, int count)
{
board[r][c] = count;
int moveRow[8] = {2,2,-2,-2,1,1,-1,-1};
int moveCol[8] = {1,-1,1,-1,2,-2,2,-2};
if (count == size * size)
{
print(size);
return;
}
for(int x = 0; x < 8; x++)
{
if(checkPosition(r + moveRow[x], c + moveCol[x], size))
{
count++;
movePosition(r + moveRow[x], c + moveCol[x], size, count);
if(count < (size * size))
{
int row = (r + moveRow[x]);
int col = (c + moveCol[x]);
board[row][col] = 0;
count--;
}
}
if(count < (size * size))
{
board[r][c] = 0;
count--;
}
}
}//end movePosition()