Working on a version of the knight's tour. The program is not acting the way I think it would given my code. I am getting several different strange discrepencies. The 'knight' is going out of bounds though I set up paramters for it not to. There are also more movements being made than there should be given my while loop. Mess with the i value, j value, and the number in the while loop and you will get several strange results. I have the value set to 32 for now just to see what will happen. Here is the code and the result:
#include <iostream>
int board[8][8];
void printBoard();
bool openSpace(int i,int j);
int startingPoint=0, m , n, i , j;
using namespace std;
int main()
{
i=4;
j=2;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
std::fill_n(&board[0][0], sizeof(board) / sizeof(**board), -1);//set all points to -1
//Set the starting point to zero
board[i][j] = startingPoint;//plot the starting point
while(startingPoint<32)//for the first 32 points
{
if (i-2>0)
{
if(j+1<8)
{
m=i-2;
n=j-2;
if (openSpace(m,n))
{
startingPoint++;
i=i-2;
j=j+1;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if(j-1>0)
m=i-2;
n=j-1;
if (openSpace(m,n))
{
startingPoint++;
i=i-2;
j=j-1;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if (i-1>0)
{
if(j+2<8)
{
m=i-1;
n=j+2;
if (openSpace(m,n))
{
startingPoint++;
i=i-1;
j=j+2;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if(j-2>0)
m=i-1;
n=j-2;
if (openSpace(m,n))
{
startingPoint++;
i=i-1;
j=j-2;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if (i+1<8)
{
if(j+2<8)
{
m=i+1;
n=j+2;
if (openSpace(m,n))
{
startingPoint++;
i=i+1;
j=j+2;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if(j-2>0)
m=i+1;
n=j-2;
if (openSpace(m,n))
{
startingPoint++;
i=i+1;
j=j-2;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if (i+2<8){
if(j+1<8)
{
m=i+2;
n=j+1;
if (openSpace(m,n))
{
startingPoint++;
i=i+2;
j=j+1;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if(j-1>0)
m=i+2;
n=j-1;
if (openSpace(m,n))
{
startingPoint++;
i=i+2;
j=j-1;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}else{
cout<<"I don't understand"<<endl;
}
}//end while
printBoard();
}
void printBoard()
{
//We need to loop through each row one by one
for (int row=0; row<8; row++)
{
//now loop through each column in the row
for(int column=0; column<8; column++)
{
cout<<board[row][column]<<" ";
}
cout<<endl;
}
cout<<endl;
}
//Check for open surrounding spaces
bool openSpace(int m,int n){
if (board[m][n] == -1)
return true;
else
return false;
}
Result:
Move: 0 Coordinates: 4,2
Move: 1 Coordinates: 2,3
Move: 2 Coordinates: 0,2
Move: 3 Coordinates: 1,4
Move: 4 Coordinates: 2,2
Move: 5 Coordinates: 4,3
Move: 6 Coordinates: 6,2
Move: 7 Coordinates: 4,3
Move: 8 Coordinates: 3,5
Move: 9 Coordinates: 4,7
Move: 10 Coordinates: 5,5
Move: 11 Coordinates: 7,6
Move: 12 Coordinates: 5,7
Move: 13 Coordinates: 3,6
Move: 14 Coordinates: 2,4
Move: 15 Coordinates: 3,2
Move: 16 Coordinates: 5,3
Move: 17 Coordinates: 7,2
Move: 18 Coordinates: 5,3
Move: 19 Coordinates: 4,5
Move: 20 Coordinates: 3,3
Move: 21 Coordinates: 4,1
Move: 22 Coordinates: 6,0
Move: 23 Coordinates: 4,-1
Move: 24 Coordinates: 3,1
Move: 25 Coordinates: 2,-1
Move: 26 Coordinates: 3,-3
Move: 27 Coordinates: 5,-2
Move: 28 Coordinates: 4,0
Move: 29 Coordinates: 5,2
Move: 30 Coordinates: 6,0
I don't understand
Move: 31 Coordinates: 7,-2
I don't understand
Move: 32 Coordinates: 5,-1
Move: 33 Coordinates: 6,1
I don't understand
-1 -1 2 -1 -1 -1 -1 -1
-1 -1 -1 -1 3 -1 -1 25
-1 -1 4 1 14 26 -1 -1
-1 24 15 20 -1 8 13 23
28 21 0 7 -1 19 27 32
-1 -1 29 18 -1 10 -1 12
30 33 6 -1 -1 -1 31 -1
-1 -1 17 -1 -1 -1 11 -1
Process returned 0 (0x0) execution time : 0.172 s
Press any key to continue.
So yeah, why are there 34 moves, why are the going off the board?