-2

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;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • What is stopping you from using a debugger, or basic exploratory techniques to identify where/when something unexpected occurs? What I see is you initialize the board and set `board[startx][starty] = 1`, then call `moveKnight(startx, starty, movecount)` which tests `checkSafe(x, y)`. That function will return false because the board at that position is not 0, and so your `moveKnight` function returns immediately. – paddy Jan 13 '23 at 01:11
  • Also note that if `checkSafe` returns `false`, then it is NOT SAFE to write to `board`. I suggest re-examining that. Presumably you should also be returning `false`, since the move is not valid and will not lead to a solution. – paddy Jan 13 '23 at 01:15

1 Answers1

1

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;
}
paddy
  • 60,864
  • 6
  • 61
  • 103
  • Awesome thank you. Looking at how you fixed the problem I see why my program wasn't working. I had the right idea just formatted it wrong and had a little extra that didn't need to be there. – Tegan Rogers Jan 13 '23 at 01:47
  • Yes you were close, but as I pointed out you had several logic errors. The only time the function should return true is if it reached 64 valid moves. – paddy Jan 13 '23 at 01:49