Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get a program to work, and I've made decent progress. The program, as you may know, is suppose to start at one point of choice, and then move to every other point on the "board" without returning to any points. Each time it lands on a spot, it is replaces the value stored there with a number equal to the number of moves that have been made, starting at 1. Currently, I recieve the following output if I enter it at [0,0]:
 1   46   49   56   61   59   58   57
48   25   28   45   50   55   62   60
27    2   47   24   38   44   51   54
 4   23   26   29   52   34   37   43
 9   14    3   22   36   39   53   33
16    5    8   13   30   21   35   40
 7   10   15   18   42   12   31   20
 0   17    6   11   32   19   41   63


As you can see looking at various points, this fails to accomplish the goal. I've gone through my code several times now and I can't figure out what to do the fix this problem. So I come to you all, looking for help. Here is my code, I was hoping to recieve assistance with it, whether this takes the form of guidance or rewritten code. Any and all help is appreciated.


C++
#include <iostream>
#include <iomanip>

using namespace std;

void moveKnight(int row, int col);
void print();
bool checkBounds(int row, int col);

int totalMoves = 1;
int board[8][8] = { 0 };
int moveNum = 1;

int main()
{
	int startRow;
	int startCol;

	cout << "Enter starting Row and Column: ";
	cin >> startRow >> startCol;

	if (checkBounds(startRow, startCol) == false)
	{
		while (checkBounds(startRow, startCol) ==  false)
		{
			cout << "Values must be between 0 and 7. Please try again: ";
			cin >> startRow >> startCol;
		}
	}

	board[startRow][startCol] = 1;

	moveNum++;

	moveKnight(startRow, startCol);
	
	cout << "Success! It took a total of " << totalMoves << " moves and there were only " << totalMoves - 64 << " mistakes!" << endl << endl;

	system("pause");
	return 0;
}

void print()
{
	for (int col = 0; col <= 7; col++) 
    {
		for (int row = 0; row <= 7; row++) 
        {
			cout << setw(5) << board[row][col];
		}
		cout << endl;
	}
}

bool checkBounds(int row, int col)
{
	if ((row <= 7 && row >= 0) && (col <= 7 && col >= 0))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*bool moveKnight(int row, int col, int moveNum)
{
	if (board[row][col] == 0)
	{
		board[row][col] = moveNum;
	}

	if (checkBounds(row, col) == true)
	{
		if (moveNum == 25)
		{
			return true;
		}

	

		totalMoves++;

		if (moveKnight(row - 2, col + 1, moveNum + 1))
		{

			return true;
		}
		else if (moveKnight(row - 2, col - 1, moveNum + 1))
		{
1;
			return true;
		}
		else if (moveKnight(row + 2, col + 1, moveNum + 1))
		{

			return true;
		}
		else if (moveKnight(row + 2, col - 1, moveNum + 1))
		{

			return true;
		}
		else if (moveKnight(row - 1, row + 2, moveNum + 1))
		{

			return true;
		}
		else if (moveKnight(row - 1, row - 2, moveNum + 1))
		{

			return true;
		}
		else if (moveKnight(row + 1, row + 2, moveNum + 1))
		{

			return true;
		}
		else if (moveKnight(row + 1, row - 2, moveNum + 1))
		{

			return true;
		}
		else
		{
			if (moveNum != 25)
			{
				board[row][col] = 0;
				return false;
			}
		}

	}
	else
	{
		return false;
	}
}*/

void moveKnight(int row, int col)
{
	if (moveNum == 64)
	{
		print();
		return;
	}

	if ((board[row - 2][col - 1] == 0) && checkBounds(row - 2, col - 1) == true)
	{
		board[row - 2][col - 1] = moveNum;

		moveNum++;
		totalMoves++;

		(moveKnight(row - 2, col - 1));
	}

	if ((board[row + 1][col + 2] == 0) && checkBounds(row + 1, col + 2) == true)
	{
		board[row + 1][col + 2] = moveNum;

		moveNum++;
		totalMoves++;

		(moveKnight(row + 1, col + 2));
	}

	if ((board[row + 2][col + 1] == 0) && checkBounds(row + 2, col + 1) == true)
	{
		board[row + 2][col + 1] = moveNum;

		moveNum++;
		totalMoves++;

		(moveKnight(row + 2, col + 1));
	}

	if ((board[row - 2][col + 1] == 0) && checkBounds(row - 2, col + 1) == true)
	{
		board[row - 2][col + 1] = moveNum;

		moveNum++;
		totalMoves++;

		(moveKnight(row - 2, col + 1));
	}

	if ((board[row + 2][col - 1] == 0) && checkBounds(row + 2, col - 1) == true)
	{
		board[row + 2][col - 1] = moveNum;

		moveNum++;
		totalMoves++;

		(moveKnight(row + 2, col - 1));
	}

	if ((board[row + 1][col - 2] == 0) && checkBounds(row + 1, col - 2) == true)
	{
		board[row + 1][col - 2] = moveNum;

		moveNum++;
		totalMoves++;

		(moveKnight(row + 1, col - 2));
	}

	if ((board[row - 1][col + 2] == 0) && checkBounds(row - 1, col + 2) == true)
	{
		board[row - 1][col + 2] = moveNum;

		moveNum++;
		totalMoves++;

		(moveKnight(row - 1, col + 2));
	}

	if ((board[row - 1][col - 2] == 0) && checkBounds(row - 1, col - 2) == true)
	{
		board[row - 1][col - 2] = moveNum;

		moveNum++;
		totalMoves++;

		(moveKnight(row - 1, col - 2));
	}
}


What I have tried:

To see what I have tried, look at the statement above.
Posted
Updated 27-Feb-21 22:40pm
v2

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
You have written nice code, but now you have to start the debugging process of your code. Read this Debugger tutorial to learn some information how to do it.

tips:
- use classes and structs
- use more braces and the else command when possible
- write some test code, so you start with a clear scenario
- make some output and remove it later
- make output, when some error case may occur

good luck, you are on the right track ;-)
 
Share this answer
 
v2
Quote:
Looking for assistance on recurssive knight's tour

Many errors in your code.
C++
void moveKnight(int row, int col)
{
	if (moveNum == 64) // you are checking the end condition here
	{
		print();
		return;
	}

	if ((board[row - 2][col - 1] == 0) && checkBounds(row - 2, col - 1) == true)
	{
		board[row - 2][col - 1] = moveNum; // you number the cell only here

		moveNum++; // and you increment only here
		totalMoves++;

		(moveKnight(row - 2, col - 1));
	}

Thus 64 is never written to a cell.

C++
if ((board[row - 2][col - 1] == 0) && checkBounds(row - 2, col - 1) == true)

First you check if cell is available, and then you check if cell is within board.
You need to first check if within board, and then cell is available.
Here you are lucky, the mistake is not armful , but reading outside of board may lead to reading in a place you do not own and get a crash for this reason.

Then the worst is you recursion:
The problem is that you get out of recursion for 2 different reasons:
- First: the goal is reached and you want to got out of recursion without changing the board.
- Second: you reached a dead end and want to backtrack and change your move.
Changing a move means cancel last move and try another.
So you have to free last cell and decrement moveNum.

I fear you need to rethink your code to cope with problems I described above.

Advice: instead of repeating same code 8 times for the 8 moves of the knight, it would be wise to use a table and loop on it instead.
Quote:
As you can see looking at various points, this fails to accomplish the goal. I've gone through my code several times now and I can't figure out what to do the fix this problem.

Make sure your code is doing what you expect, use the debugger
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900