again knight's tour

Mar 7, 2012 at 7:31pm
hi guys, i've tried to accomplete knight's tour problem by myself. In fact this is very simple version of original problem. All i need to do is to make the knight have L-moves until it stucked or finished it's tour. But after 3rd move, somethin strange happens
1 0 0 0 0 4 0 0
0 0 0 3 0 0 0 0
0 2 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 4 0 0 0

here numbers represent the sequence of moves, after 3rd one two positions are gained and i dunno how can i deal with it, the only idea that i have, that is the problem in dealing with borders of board or something else.

This 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
#include <iostream>
using namespace std;
int showboard(int chessboard[7][7])
{
    for (int x =0; x<8; x++)
    {
     for (int y =0; y<8; y++)
     {
    cout << " " <<chessboard[y][x] << " ";
     }
     cout << endl;
     }
     return 0;
}
    
int main (){
    int chessboard[7][7];
    int x=0,y=0, counter=0;
    int movx [8] = { 2, 1, -1 , 2, -2, -1, 1, 2};
    int movy [8] = { -1, -2, -2, -1, 1, 2, 2, 1};
    int xy =1;
    for (int i=0; i<8;i++){
    for (int j=0; j<8;j++){
    chessboard[i][j] =0;
    }
    }
    do {
        for (int i=0; i<8;i++)
        {
            cout << "for counter is " <<i;
        chessboard [x][y] =xy;
        cout << "chessboard positions is " << x << " " << y << endl;
        cout << " x+movx[i] = " << x+movx[i] << endl;
        cout << " y+movy[i] = " << y+movy[i] << endl;
        if ( ((x+movx[i])<0) || ((y+movy[i]) < 0)){ cout << "case 1";
        continue;}
        else if (((x+movx[i]) >7)|| ((y+movy[i]) >7)){cout << "case 2";
        continue;}
        else if ((chessboard[x+movx[i]][y+movy[i]]) == 1){cout << "case 3";
        continue;}
        else if ((chessboard[x+movx[i]][y+movy[i]]) == 0)
        {
             x += movx[i];
             y += movy[i];
             chessboard[x][y] = ++xy;
             counter++;
             cout <<" counter is " << counter << endl;
             break;
             }
        else
                cout <<" Unexpected error " << endl;
        }
        showboard ( chessboard);
        system("pause");
        } while (counter <64);
        system("pause");
        return 0;
        }


I'll be very happy if anyone will spend his minute for me, plz help me
Last edited on Mar 10, 2012 at 5:27am
Mar 8, 2012 at 10:41pm
up


Mar 8, 2012 at 10:53pm
Please indent your code.

AFAIK chessboards are 8x8.
If you don't want especial treatment for the borders, then use a bigger board filled with centinels
Mar 9, 2012 at 4:48am
@TJbla2e

With a few adjustments, I was able to get the tour up to 32 moves, then I get the 'Unexpected error' message. Mainly because there is no open space for the next Knight move. You'll have to add a way for the moves to reverse itself, so that when a move is not possible, go back a move or two, and try a different space than the previous one. Any way, here's the program with the few adjustments, I made.
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
74
#include <iostream>

using namespace std;

int showboard(int chessboard[8][8])
{
	for (int x = 0; x < 8; x++)
	{
		cout << "\t";
		for (int y = 0; y < 8; y++)
		{
			cout << " " <<chessboard[y][x] << " ";
		}
		cout << endl;
	}
	return 0;
}

int main (){
	int chessboard[8][8];
	int x=0,y=0, counter=0;
	int movx [8] = { 2, 1, -1 , 2, -2, -1, 1, 2};
	int movy [8] = { -1, -2, -2, -1, 1, 2, 2, 1};
	int xy =1;
	for (int i=0; i<8;i++)
	{
		for (int j=0; j<8;j++)
		{
			chessboard[i][j] =0;
		}
	}
	do
	{
		for (int i=0; i<8;i++)
		{
			cout << "Counter is " <<i << " " << endl;
			chessboard [x][y] =xy;
			cout << "chessboard positions is " << x << " " << y << endl;
			cout << " x+movx[i] = " << x+movx[i] << endl;
			cout << " y+movy[i] = " << y+movy[i] << endl;
			if ( ((x+movx[i])<0) || ((y+movy[i]) < 0))
			{
				cout << "case 1" << endl;
				continue;
			}
			else if (((x+movx[i]) >7)|| ((y+movy[i]) >7))
			{
				cout << "case 2" << endl;
				continue;
			}
			else if ((chessboard[x+movx[i]][y+movy[i]]) == 1)
			{
				cout << "case 3" << endl;
				continue;
			}
			else if ((chessboard[x+movx[i]][y+movy[i]]) == 0)
			{
				x += movx[i];
				y += movy[i];
				xy++;
				chessboard[x][y] = xy;
				counter++;
				cout <<" counter is " << counter << endl;
				break;
			}
			else
				cout <<" Unexpected error " << endl;
		}
		showboard ( chessboard);
		system("pause");
	} while (counter <64);
	system("pause");
	return 0;
}
Mar 9, 2012 at 8:25pm
thank you very much whitenite, thats really helped! If there is anything i could do for you, i will
by the way, after fixing this problem, i will continue by adding recursion

Mar 9, 2012 at 10:18pm
@TJbla2e

Thanks for the offer. I may just take you up on it at a later date. Also, would be very interested on your program progresses. If you wish, PM me the updates if you don't post them here. And, if you need a little more help, I'll try my best.
Topic archived. No new replies allowed.