^^^ ROFL!

I thought something like this was enough to give him a start:

It still needs to be finished, but I know the logic has a bug in it, already.

If the OP finishes the knights possible moves and gets the logic fixed, he'll be good to go, I believe.

/* knight's tour by Adak May 1, 2009

status: just started

In the main() function:

1. Declare a 8x8 two dimensional array (int chess[8][8] to represent a chess

board and set all its elements to -1 (i.e., no cells (squares) are visited, yet).

2. Ask user to enter i and j as the row and column numbers of the initial cell. (Since

you are using C, make sure that user enters a number between 0 and 7 (inclusive)

for row and column numbers).

3. Set chess[i][j] to 1 means that the horse visited that cell first.

4. Implement and call a function to determine in which order the horse visits the legal

squares once according to our random selection strategy described below and

returns the number of squares visited

numofsquares = random_horse(chess, i, j);

print(“Number of squares visited = %d\n”, numofsquares);

5. Implement and call a function print_board(chess); to print the values in chess

board showing in which order the cells are visited

In the int random_horse(int chess[][8], int i, int j) function:

while(1) {

a. Identify the legal but not-yet-visited squares

b. If there is no such squares return chess[i][j];

c. Pick one of these cells randomly (say you picked chess[x][y])

d. Update order number (chess[x][y] = chess[i][j] + 1

e. Move the horse to the selected cell (i=x; j=y

} /* end of while */

*/

Code:

#include <stdio.h>
#include <stdlib.h>
void print_board(int chess[][8]);
int random_horse(int chess[][8], int i, int j);
int main(void) {
int r,c, i, j, numofsquares;
int chess[8][8];
for(r = 0; r < 8; r++)
for(c = 0; c < 8; c++)
chess[r][c] = =1;
i = -1;
while(i < 0 || i > 7) {
printf("\nEnter your starting row [0-7] ");
scanf("%d", &i);
}
j = -1;
while(j < 0 || j > 7) {
printf("\nEnter your starting column [0-7] ");
scanf("%d", &j);
}
chess[i][j] = 1;
numofsquares = random_horse(chess, i, j);
print_board(chess);
printf("Number of squares visited = %d\n", numofsquares);
printf("\n\n\t\t\t press enter when ready ");
i = getchar();
return 0;
}
int random_horse(int chess[][8], int i, int j) {
int r = i;
int c = j;
int k, kmoves, moves, validmoves, n, sqr;
int valid[8] = { 0 };
moves = kmoves = 0;
/*
Log:
-1 = sqr has not been visited by the knight
>0= sqr has been visited by the knight
-2= sqr is a legal next move by the knight
array rows are in ascending order on the screen
0
1
2
*/
while(1) {
//check the 8 (max) possible knight moves
if((r > 1) && (c < 7) && (chess[r-2][c+1] == -1)) //1:30 o'clock move ok?
chess[r-2][c+1] = -2;
if((r > 0) && (c < 6) && (chess[r-1][c+2] == -1)) //2:30 move
chess[r-1][c+2] = -2;
if((r < 7) && (c < 6) && (chess[r+1][c+2] == -1)) //3:30 move
chess[r+1][c+2] = -2;
if((r < 6) && (c < 7) && (chess[r+2][c+1] == -1)) //5:30 move
chess[r+2][c+1] = -2;
//etc., for all 8 possible moves.
for(r = 0, k = 0; r < 8; r++) {
for(c = 0; c < 8; c++) {
if(chess[r][c] == -2) {
validmoves++;
if(!r) sqr = c;
else sqr = r * 10 + c;
valid[k] = sqr;
++k;
}
}
}
if(!moves) {
return chess[i][j];
}
n = rand() % validmoves;
if(valid[n] < 8) {
r = 0;
c = valid[n];
}
else {
r = valid[n] / 10;
c = valid[n] % 10;
}
chess[r][c] = kmoves;
++kmoves;
print_board(chess);
moves = getchar();
//resets
moves = 0;
for(i = 0; i < 8; i++)
valid[i] = 0;
validmoves = 0;
}
}
void print_board(int chess[][8]) {
int i, r, c;
printf("\n\n");
for(r = 0; r < 8; r++) {
for(i = 0; i < 9; i++) {
if(i < 8)
printf("|---");
else
printf("|");
}
printf("\n");
for(c = 0; c < 8; c++) {
printf("| %d ", chess[r][c]);
if(c % 7 == 0 && c > 0)
printf("|\n");
}
}
for(i = 0; i < 9; i++) {
if(i < 8)
printf("|---");
else
printf("|");
}
printf("\n");
}

Edit:

A bit of smoothing out was needed, even for a rough idea.

No need to post it here, but the above is on the right track.

The random choice does not allow it to ever complete a full knights tour of all 64 squares, unless you add

more logic, as noted in my posts earlier in the thread.