Hi,
My program's suppose to move the knight around the chess board touching the each square only once. It's starting off at (0, 0) and there are eight possible moves for the knight depending on its position. The program tests each potential move and if that move is not off the board or it has been visited yet then that move is made. However my program's output only say that the knight make 8 moves. i tried out by hand and it's sure more than that. Pls help , what's wrong ?
thnx
Code:
/* Knight's Tour */
#include <stdio.h>
int main()
{
int board[ 8 ][ 8 ] = { 0 }; /* Chess board */
int horizontal[ 8 ] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int vertical[ 8 ] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int totalMoves = 0; /* Total of all moves made */
int currentRow = 0, currentColumn = 0; /* Current position of knight */
int moveNumber; /* Move type */
int i, j; /* Loop counters */
int newPos; /* Test next possible move for validity */
int tryNextMoveType = 0; /* Flag to see if try next move number */
int end = 0;
while ( end != 1 ) {
moveNumber = 0;
for ( i = 0; i <= 7; i++ ) {
tryNextMoveType = 0;
newPos = currentRow + vertical[ moveNumber ];
if ( newPos < 0 || newPos > 7 )
tryNextMoveType = 1;
newPos = currentColumn + horizontal[ moveNumber ];
if ( newPos < 0 || newPos > 7 )
tryNextMoveType = 1;
if ( board[ currentRow + vertical[ moveNumber ] ][ currentColumn + horizontal[ moveNumber ] ] == 1 )
tryNextMoveType = 1;
if ( tryNextMoveType == 0 ) {
currentRow += vertical[ moveNumber ];
currentColumn += horizontal[ moveNumber ];
board[ currentRow ][ currentColumn ] = 1;
++totalMoves;
break;
}
else
moveNumber++;
if ( i == 7 && tryNextMoveType == 1 ) /* No more possible moves */
end = 1;
}
}
printf( "Total moves made by the knight: %d", totalMoves );
return 0;
}