Knight's Tour

Hey guys, I am trying to complete a c++ Knight's tour program. I am getting full output but the tour stops after the first move so my output is this

00000000
00000000
00000000
00000000
00000000
00000000
00000000

00000000
00000200
00000000
00001000
00000000
00000000
00000000
00000000

I think my problem is my if else statement cluster but I can't seem to determine what went wrong. I have a sneaking suspicion it has something to do with syntax. Here is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
using namespace std;

	const int rows = 8;
	const int columns = 8;
	void printArray( int[][columns]);


int main ()
{
	int i, j;

	int counter = 1;
	int moveNumber = 1;
	int currentRow = 3;
	int currentColumn = 4;
	int pastRow;
	int pastColumn;

	int chessBoard[rows][columns];

	for (i = 0; i < 8; i++)
	{
		for (j = 0; j < 8; j++)
		{
			chessBoard[i][j] = 0;
		}
	}

	printArray (chessBoard);
	cout << endl;

	chessBoard[currentRow][currentColumn] = counter;
	counter++;

	int horizontal [columns] = {2, 1, -1, -2, -2, -1, 1, 2};
	int vertical [columns] = {-1, -2, -2, -1, 1, 2, 2, 1};

	if (counter <= 64)
	{
		pastRow = currentRow;
		pastColumn = currentColumn;
		currentRow += vertical[moveNumber];
		currentColumn += horizontal[moveNumber];

		if ( chessBoard[currentRow][currentColumn] != 0)
		{	currentRow = pastRow;
			currentColumn = pastColumn;
			moveNumber++;
		}
		else
		{	chessBoard[currentRow][currentColumn] = counter;
			counter++;
			moveNumber = 1;
		}
	}

		printArray (chessBoard);

return 0;

};

void printArray( int a[rows][columns])
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
			cout << a[i][j] << ' ';
			
		cout << endl;
	}
}
Last edited on
Can anyone help me? I am really confused.
Hi
What are you expecting this program to do? What do you expect the output to be?
Regards, keineahnung
Ah, yes I forgot that part. I'll try to give a condensed version of what it should do. We are supposed to write a program that tests the knights tour to see if a knight chess piece could touch every space on an 8x8 board once and only once. Here were the steps given.

Develop a program that will move the knight around the chess board. The board is represented by an 8x8 2d array. Each of the squares is initialized to zero. There are eight moves the knight can make (these moves are set by the horizontal and vertical arrays and the variable moveNumber).

Let the variables currentRow and currentColumn indicate the row and column of the knights current position. Keep a counter that varies from 1 to 64, record the latest count in each square the knight moves to. Test each move to see if the knight has already visited that square and test each move to make sure the knight does not move off the chessboard. Now write a program to make the knight move around the chess board.

I hope that makes sense. It's kind of a confusing condensed version of the instructions I was given
Have you searched the web for the algorithms that are out there? They might be in c++ but they are out there when I did a search for the Queens Eight, I believe, problem. I found both of them at the same time. I even found code in other languages. I would have to spend a lot of time studying the problem to give any insight to it.
I am getting full output but the tour stops after the first move
Absolutely, because you're asking it to make only one move. At the very least you need some sort of loop. Something like this pseudo code:

1
2
3
4
5
6
int unvisited_squares = 64;
while ( unvisited_squares )
{
    move_knight();
    --unvisited_squares;
} // while 


I can't say much about the details of your 'move_knight()' code. Are you going for an 'elegant' solution or brute force? As @Azagaros has already advised, if you're stuck on the solution have a look around the WWW.
Hope that helps a bit ;)
I'm going for more brute force at the beginning, then working out something more elegant. But I need the brute force one to work first. I know what you mean with the unvisited squares, that's what the if-if-else loop is for and the counter. So, I'm not really sure what you want me to change...
@ OP: Have you started with objects yet? This would be so much easier if Teachers made that the first thing they taught with C++.
Yeah, we have done objects, but I'm still not used to them I suppose. Do you think this would be easier to implement with a class and objects?
Oh, goodness I feel so stupid now. I just realized I don't have a repetition loop to continue the tour, so I need to use a while loop to continue it! Wow, I don't know how i missed that :P
@Seeshi
Oh, goodness I feel so stupid now.

Been there, done that - many, many times over ;)
Let us know how you get on.
Topic archived. No new replies allowed.