```
#include <iostream.h>
const maxSize=20;
int boardType[maxSize][maxSize];
class knightTour {
//Handles logic problem of Knight's tour
//Maximum board size 20x20
public:
void initialize(int boardType[maxSize][maxSize], int size);
bool validMove(const boardType[maxSize][maxSize], int size, int row, int column);
void prBoard(const boardType[maxSize][maxSize], int size);
bool knightRecur(int boardType[maxSize][maxSize], int size, int row, int column, int moves);
};//class knightTour
void main(){
knightTour check;
int row, col, size;
cout<< "Enter in a board size." << endl;
cin >> size;
cout<<"Enter the board location you wish to test:"<<endl;
cout<<"Board row number: "<<endl;
cin>>row;
row-=1;
cout<<endl;
cout<<"Board column number: ";
cin>>col;
col-=1;
cout<<endl;
check.initialize(boardType, size);
check.knightRecur(boardType, size, row, col, 1);
}
void knightTour::initialize(int boardType[maxSize][maxSize], int size)
{
for (int row=0; row < size; row++)
{
for (int column=0; column < size; column++)
boardType[row][column] = 0;
}
}
bool knightTour::validMove(const boardType[maxSize][maxSize], int size, int row, int column)
{//checks to see if a move is valid
if((row >= size) || (row < 0) || (column >= size) || (column < 0))
return false;//checks to see if move falls off the board
if(boardType[row][column] == 0)
return true;//if position is marked with 0, the move is okay.
else
return false;//spot is already taken
}//bool validMove
void knightTour::prBoard(const boardType[maxSize][maxSize], int size)
{
// outputs a board with knight moves
// Pre: size <= MAXSIZE
int temp, col, row;
// output top of grid
cout <<"\n+";
for (temp = 0; temp < size; ++temp)
cout <<"----+";
cout <<endl;
for (row = 0; row < size; ++row)
{
for (col = 0; col < size; ++col) // do one row
if (boardType[row][col] != 0) // print the move number
cout <<"| " << boardType[row][col] << " ";
else
cout <<"| ";
cout <<"|\n+"; // grid between rows
for (temp= 0; temp < size; ++temp)
cout <<"----+";
cout <<endl;
} // for
cout <<endl;
}//void prBoard
bool knightTour::knightRecur(int boardType[maxSize][maxSize], int size, int row, int column, int moves)
{
knightTour game;
game.prBoard(boardType, size);
if(!game.validMove(boardType, size, row, column))//checks to see if move is valid
return false;
boardType[row][column] = moves;
if (moves == (size * size))
{ // then it has moved to ALL the spaces
game.prBoard(boardType, size);
return true;
}//if
else // recursion
{ // tries each possible move for the knight
if (knightRecur(boardType, size, row+2, column+1, moves+1))
return true;
else if (knightRecur(boardType, size, row+2, column-1, moves+1))
return true;
else if (knightRecur(boardType, size, row-2, column+1, moves+1))
return true;
else if (knightRecur(boardType, size, row-2, column-1, moves+1))
return true;
else if (knightRecur(boardType, size, row+1, column+2, moves+1))
return true;
else if (knightRecur(boardType, size, row+1, column-2, moves+1))
return true;
else if (knightRecur(boardType, size, row-1, column+2, moves+1))
return true;
else if (knightRecur(boardType, size, row-1, column-2, moves+1))
return true;
else // if no moves work then go back recursively
return false;
}//else
}//bool knightRecur
```