c++debuggingrecursionknights-tour

c++ Knights tour using recursion


I know my code is extremely close I have all of my functions working except the moveKnight() function if you do not know what knights Tour is, it's a program we are writing to help learn recursion in class. The knight is suppose to touch every space on the 8*8 chessboard only once and then prints out the move number that it took to get there. It currently only prints out the first position board[0][0]=1 but does not give "No solution". I can not figure out where I should start looking for the problem any help is greatly appreciated.

#include <iostream>
using namespace std;
//Global Variables

//Defining the 8 possible Moves in the order from class
int yMove[8] = { 1,2, 2, 1,-1,-2,-2,-1 };
int xMove[8] = { 2,1,-1,-2,-2,-1, 1, 2 };

int board[8][8];
int startx, starty = 0;
int movecount = 1;

//checks if move is safe
bool checkSafe(int x, int y)
{
return (x >= 0 && x < 8 && y >= 0 && y < 8 && board[x][y] == 0);
}

//Prints Current board
void printBoard(int board[8][8])
{
for (int x = 0; x < 8; x++)
{
    for (int y = 0; y < 8; y++)
        cout << " " << board[x][y] << " ";
    cout << endl;
}
}

bool moveKnight(int x, int y, int movecount)
{
if (!checkSafe(x, y))
{
    board[x][y] = movecount;
    return true;
}
//end condition
if (movecount == 64)
    return true;
if (moveKnight(x + xMove[1], y + yMove[1], movecount + 1))
    return true;
else if (moveKnight(x + xMove[0], y + yMove[0], movecount + 1))
    return true;
else if (moveKnight(x + xMove[2], y + yMove[2], movecount + 1))
    return true;
else if (moveKnight(x + xMove[3], y + yMove[3], movecount + 1))
    return true;
else if (moveKnight(x + xMove[4], y + yMove[4], movecount + 1))
    return true;
else if (moveKnight(x + xMove[5], y + yMove[5], movecount + 1))
    return true;
else if (moveKnight(x + xMove[6], y + yMove[6], movecount + 1))
    return true;
else if (moveKnight(x + xMove[7], y + yMove[7], movecount + 1))
    return true;
else
{
    board[x][y] = 0;
    return false;
}
}

int KnightTour()
{
//creating board
for (int x = 0; x < 8; x++)
{
    for (int y = 0; y < 8; y++)
        board[x][y] = 0;
}
board[startx][starty] = 1;
movecount + 1;
//No possible moves
if (!moveKnight(startx, starty, movecount))
    cout << "Not possible";
else
{
    //yes possible now print
    printBoard(board);
}
//exits
return 0;
}

int main()
{
//calls knights tour
KnightTour();
cout << endl;
system("pause");
return 0;
}

Solution

  • Your moveKnight function returns immediately because it determines the very first position is not a valid move. The reason is you initialized the board with a non-zero value at the start position.

    Remove these two lines from main:

    board[startx][starty] = 1;
    movecount + 1;
    

    The first one breaks your recursion, and the second one does nothing at all.

    Additionally, the logic after calling checkSafe() is screwy, because at the moment when you determine a move is either out-of-bounds or already-played, you are writing a value to the board. That's going to result in undefined behavior.

    Correcting these things, and also simplifying the recursive calls:

    bool moveKnight(int x, int y, int movecount)
    {
        if (checkSafe(x, y))
        {
            board[x][y] = movecount;
            if (movecount == 64)
                return true;
    
            for (int i = 0; i < 8; i++)
            {
                if (moveKnight(x + xMove[i], y + yMove[i], movecount + 1))
                    return true;
            }
    
            board[x][y] = 0;
        }
        return false;
    }