-1

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?

false
  • 10,264
  • 13
  • 101
  • 209
cparks10
  • 377
  • 2
  • 14
  • 2
    Time to learn how to use a debugger and how to use it to step through your code, line by line, while monitoring variables and their values. – Some programmer dude Feb 16 '17 at 08:02
  • I second that you need to learn some debugging skills. You can either use a visual debugger or add more cout statements to print the values of variables. – Code-Apprentice Feb 16 '17 at 08:04
  • You might want to `n=j+1;` instead of `n=j-2;` at the very beginning of the loop. – felix Feb 16 '17 at 08:17
  • The `else` you are getting corresponds to `if ((i+2) < 8)`, which is not true as `i` is 6. Should the `else` be for the inner `if`? You are at least missing opening `{` after the inner `if`. – Ari Hietanen Feb 16 '17 at 08:24
  • Check your brackets! They are wrong on several places. – Aeonos Feb 16 '17 at 08:37

1 Answers1

1

I think there are few problems.

First you check the allowed moves one after other

while (starting point<32){
  if (not over right board){
  }
  if (not over left board){
  }
  ....
}

This means that multiple if statements can be true until the while loop is checked, hence you go over the limit of 32. You could use continue statement after each move which goes to the beginning of the loop and does the check or you have to do the comparison after every move and then break from the loop.

Then you are missing brackets { in several places.

Then as pointed out by felix in the first if you should have n=j+1 not n=j-2.

You might want to consider slight rewriting of the code to be easier to read. E.g., you could make a function which tries to make the move and return true it is successful.

bool tryToMove(int stepI, int stepJ){
  int n = i+stepI;
  int m = j+stepJ;
  if (n>=0 && n<8 && m>=0 && m<8 && openSpace(n,m)){
    i = n;
    j = m;
    board[i][j] = startingPoint;
    return true;
  }
  return false;
}

and you can use it like

if (tryToMove(-2,1)){
  if (startingPoint == 32) break;
}
if (tryToMove(-2,-1)){
  if (startingPoint == 32) break;
}
...

or instead use continue, but that changes the order of move.

There will be few more comparisons than in your code, but it is much easier to see what is going on.

Ari Hietanen
  • 1,749
  • 13
  • 15