# Knight's Tour Game Problem!

This is a discussion on Knight's Tour Game Problem! within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm teaching myself C++ from a textbook and I have lost my way with a Knight's Tour program ...

1. ## Knight's Tour Game Problem!

Hi all,

I'm teaching myself C++ from a textbook and I have lost my way with a Knight's Tour program implementation.
After having chosen the start position subsequent moves don't land where they should or no incremental mark is left on the board.
Any help from an eagle eyed programmer would be greatly recieved!

Code:
```#include <iostream>
using namespace std;

const int side = 9;
const int maxMoves = 64;
const int horizontal[ 8 ] = { 2, 1, -1, -2, -2, -1, 1, 2 };
const int vertical[ 8 ] = { -1, -2, -2, -1, 1, 2, 2, 1 };

int moveValidation(); // to ensure that moves are between 0 & 7
int initialRow(); // to set the starting position ( Row )
int initialColumn(); // to set the starting position ( Column )
void initializeBoard( int[][ side ], int ); // to set row & column 0 to increment, everything else to zero!
void displayBoard( int[][ side ], int ); // to print the board

int main()
{

int board[ side ][ side ];
int currentRow; // initialized to fail validation tests
int currentColumn; // initialized to fail validation tests
int move;
int currentMove = 1;

initializeBoard( board, side );

displayBoard( board, side );

currentRow = initialRow();

currentColumn = initialColumn();

board[ currentRow ][ currentColumn ] = currentMove;

currentMove++;

displayBoard( board, side );

while ( currentMove < maxMoves )
{

move = moveValidation();

cout << "currentRow += vertical[ move ] = " << ( currentRow += vertical[ move ] ) << endl;
cout << "currentColumn += horizontal[ move ] = " << ( currentColumn += horizontal[ move ] ) << endl;
cout << "currentMove = " << currentMove << endl;

if ( ( currentRow += ( vertical[ move ] ) ) < 1 || ( currentRow += ( vertical[ move ] ) ) > 9 )
{

cout << "That move is forbidden!!!";

}
else if ( ( currentColumn += ( horizontal[ move ] ) ) < 1 || ( currentColumn += ( horizontal[ move ] ) ) > 9 )
{

cout << "That move is forbidden!!!";

}
else if ( board[ ( currentRow += ( vertical[ move ] ) ) ][ ( currentColumn += ( horizontal[ move ] ) ) ]  == 0 )
{

( currentColumn += ( horizontal[ move ] ) );
( currentRow += ( vertical[ move ] ) );

board[ currentRow ][ currentColumn ] = currentMove;

currentMove++;

displayBoard( board, side );

}
else
{

cout << "That move is forbidden!!!";

}

}

} // end main

// to print the board
void displayBoard( int board[][ side ], int side )
{

cout << endl;

for ( int i = 0; i < side; i++ )
{

for ( int j = 0; j < side; j++ )
{

if ( board[ i ][ j ] != 0 )
{

cout << board[ i ][ j ];

}
else
{

cout << " ";

}

}

cout << endl;

}

cout << endl;

} // end function displayBoard

// to set row & column 0 to increment, everything else to zero!
void initializeBoard( int board[][ side ], int side )
{

for ( int i = 0; i < side; i++ )
{

board[ 0 ][ i ] = i;

}

for ( int i = 0; i < side; i++ )
{

board[ i ][ 0 ] = i;

}

for ( int i = 1; i < side; i++ )
{

for ( int j = 1; j < side; j++ )
{

board[ i ][ j ] = false;

}

}

} // end function initializeBoard

// to set the starting position ( Row )
int initialRow()
{

int a = 10;

while ( a < 1 || a > 9 )
{

cout << "Where to begin?\nRow: ";
cin >> a;

}

return a;

} // end function initialRow

// to set the starting position ( Column )
int initialColumn()
{

int a = 10;

while ( a < 1 || a > 9 )
{

cout << "Column: ";
cin >> a;

}

return a;

} // end function initialColumn

// to ensure that moves are between 0 & 7
int moveValidation()
{

int userInput;

cout << "Enter move number ( between 0 & 7 ): ";
cin >> userInput;

while ( userInput < 0 || userInput > 7 )
{

cout << "Enter move number ( between 0 & 7 ): ";
cin >> userInput;

}

return userInput;

} // end function moveValidation```

2. Code:
```        if ( ( currentRow += ( vertical[ move ] ) ) < 1 || ( currentRow += ( vertical[ move ] ) ) > 9 )
else if ( ( currentColumn += ( horizontal[ move ] ) ) < 1 || ( currentColumn += ( horizontal[ move ] ) ) > 9 )
else if ( board[ ( currentRow += ( vertical[ move ] ) ) ][ ( currentColumn += ( horizontal[ move ] ) ) ]  == 0 )```
How many times are you changing currentRow ?

Make a single assignment for the next test position, then validate it.
Trying to compress everything down into a single statement with lots of side effects is not good.