I'm lost with how the knight backtracks using recursion. I've tried multiple ways as you can see it's commented out some of the attempts, but how does it know how far to backtrack back to start moving forward again? My understanding of recursion is the function calls itself each time with new arguments building up the stack frame, and when it gets to the base case it goes back down the stack frame, in this case moving back moves. Can someone point me in the right direction? Thanks.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int GAMEBOARD[8][8] = { 0 };
int TOTAL_MOVES = 0, BAD_MOVES = 0;
bool moveKnight(int row, int col, int movNum);
void print();
int main()
{
int startRow = 3, startCol = 3;
int moveNum = 1;
GAMEBOARD[startRow][startCol] = moveNum;
TOTAL_MOVES++;
moveKnight(startRow, startCol, moveNum);
if (moveKnight(startRow, startCol, moveNum) == true) {
cout << "Knight's Tour Solved! It took " << TOTAL_MOVES <<
" total moves and " << BAD_MOVES << " bad moves." << endl;
}
system("pause");
return 0;
}
bool moveKnight(int row, int col, int moveNum)
{
GAMEBOARD[row][col] = moveNum;
TOTAL_MOVES++;
if (moveNum == 64) {
return true;
}
if (GAMEBOARD[row][col] != 0) {
GAMEBOARD[row][col] = 0;
print();
system("pause");
return moveKnight(row, col, moveNum);
}
// commented out
/*if (GAMEBOARD[row - 2][col + 1] != 0) {
print();
system("pause");
return moveKnight(row - 2, col + 1, moveNum + 1);
}
else if (GAMEBOARD[row - 1][col + 2] != 0) {
print();
system("pause");
return moveKnight(row - 1, col + 2, moveNum + 1);
}
else if (GAMEBOARD[row + 1][col + 2] != 0) {
print();
system("pause");
return moveKnight(row + 1, col + 2, moveNum + 1);
}
else if (GAMEBOARD[row + 2][col + 1] != 0) {
print();
system("pause");
return moveKnight(row + 2, col + 1, moveNum + 1);
}
else if (GAMEBOARD[row + 2][col - 1] != 0) {
print();
system("pause");
return moveKnight(row + 2, col - 1, moveNum + 1);
}
else if (GAMEBOARD[row + 1][col - 2] != 0) {
print();
system("pause");
return moveKnight(row + 1, col - 2, moveNum + 1);
}
else if (GAMEBOARD[row - 1][col - 2] != 0) {
print();
system("pause");
return moveKnight(row - 1, col - 2, moveNum + 1);
}
else if (GAMEBOARD[row - 2][col - 1] != 0) {
print();
system("pause");
return moveKnight(row - 2, col - 1, moveNum + 1);
}
return false;*/
else if (row - 2 >= 0 && row - 2 <= 7 && col + 1 >= 0
&& col + 1 <= 7 && GAMEBOARD[row - 2][col + 1] == 0) {
GAMEBOARD[row - 2][col + 1] = moveNum;
print();
system("pause");
return moveKnight(row - 2, col + 1, moveNum + 1);
}
else if (row - 1 >= 0 && row - 1 <= 7 && col + 2 >= 0
&& col + 2 <= 7 && GAMEBOARD[row - 1][col + 2] == 0) {
GAMEBOARD[row - 1][col + 2] = moveNum;
print();
system("pause");
return moveKnight(row - 1, col + 2, moveNum + 1);
}
else if (row + 1 >= 0 && row + 1 <= 7 && col + 2 >= 0
&& col + 2 <= 7 && GAMEBOARD[row + 1][col + 2] == 0) {
GAMEBOARD[row + 1][col + 2] = moveNum;
print();
system("pause");
return moveKnight(row + 1, col + 2, moveNum + 1);
}
else if (row + 2 >= 0 && row + 2 <= 7 && col + 1 >= 0
&& col + 1 <= 7 && GAMEBOARD[row + 2][col + 1] == 0) {
GAMEBOARD[row + 2][col + 1] = moveNum;
print();
system("pause");
return moveKnight(row + 2, col + 1, moveNum + 1);
}
else if (row + 2 >= 0 && row + 2 <= 7 && col - 1 >= 0
&& col - 1 <= 7 && GAMEBOARD[row + 2][col - 1] == 0) {
GAMEBOARD[row + 2][col - 1] = moveNum;
print();
system("pause");
return moveKnight(row + 2, col - 1, moveNum + 1);
}
else if (row + 1 >= 0 && row + 1 <= 7 && col - 2 >= 0
&& col - 2 <= 7 && GAMEBOARD[row + 1][col - 2] == 0) {
GAMEBOARD[row + 1][col - 2] = moveNum;
print();
system("pause");
return moveKnight(row + 1, col - 2, moveNum + 1);
}
else if (row - 1 >= 0 && row - 1 <= 7 && col - 2 >= 0
&& col - 2 <= 7 && GAMEBOARD[row - 1][col - 2] == 0) {
GAMEBOARD[row - 1][col - 2] = moveNum;
print();
system("pause");
return moveKnight(row - 1, col - 2, moveNum + 1);
}
else if (row - 2 >= 0 && row - 2 <= 7 && col - 1 >= 0
&& col - 1 <= 7 && GAMEBOARD[row - 2][col - 1] == 0) {
GAMEBOARD[row - 2][col - 1] = moveNum;
print();
system("pause");
return moveKnight(row - 2, col - 1, moveNum + 1);
}
// commented out
/*if (row - 2 < 0 || row - 2 > 7 || col + 1 < 0
|| col + 1 > 7 || GAMEBOARD[row - 2][col + 1] != 0) {
GAMEBOARD[row - 2][col + 1] = 0;
return moveKnight(row - 2, col + 1, moveNum + 1);
}
else if (row - 1 < 0 || row - 1 > 7 || col + 2 < 0
|| col + 2 > 7 || GAMEBOARD[row - 1][col + 2] != 0) {
GAMEBOARD[row - 1][col + 2] = 0;
return moveKnight(row - 1, col + 2, moveNum + 1);
}
else if (row + 1 < 0 || row + 1 > 7 || col + 2 < 0
|| col + 2 > 7 || GAMEBOARD[row + 1][col + 2] != 0) {
GAMEBOARD[row + 1][col + 2] = 0;
moveKnight(row + 1, col + 2, moveNum + 1);
return false;
}
else if (row + 2 < 0 || row + 2 > 7 || col + 1 < 0
|| col + 1 > 7 || GAMEBOARD[row + 2][col + 1] != 0) {
GAMEBOARD[row + 2][col + 1] = 0;
moveKnight(row + 2, col + 1, moveNum + 1);
return false;
}
else if (row + 2 < 0 || row + 2 > 7 || col - 1 < 0
|| col - 1 > 7 || GAMEBOARD[row + 2][col - 1] != 0) {
GAMEBOARD[row + 2][col - 1] = 0;
moveKnight(row + 2, col - 1, moveNum + 1);
return false;
}
else if (row + 1 < 0 || row + 1 > 7 || col - 2 < 0
|| col - 2 > 7 || GAMEBOARD[row + 1][col - 2] != 0) {
GAMEBOARD[row + 1][col - 2] = 0;
moveKnight(row + 1, col - 2, moveNum + 1);
return false;
}
else if (row - 1 < 0 || row - 1 > 7 || col - 2 < 0
|| col - 2 > 7 || GAMEBOARD[row - 1][col - 2] != 0) {
GAMEBOARD[row - 1][col - 2] = 0;
moveKnight(row - 1, col - 2, moveNum + 1);
return false;
}
else if (row - 2 < 0 || row - 2 > 7 || col - 1 < 0
|| col - 1 > 7 || GAMEBOARD[row - 2][col - 1] != 0) {
GAMEBOARD[row - 2][col - 1] = 0;
moveKnight(row - 2, col - 1, moveNum + 1);
return false;
}
*/
cout << endl << endl;
return false;
}
void print()
{
for (int row = 0; row <= 7; row++) {
for (int col = 0; col <= 7; col++) {
cout << setw(5) << GAMEBOARD[row][col];
}
cout << endl;
}
}