hello to everybody. i'm writing a knight tour program. in this code, you enter the dimension of the board and the frst place of the knight and then ask the user to what to do!
n = next
p = print
(the other are not completed yet)
here's the code :

#include <stdio.h>
#include <strings.h>
//print function for printing chess board with custom dimension and showing the knights moves

int print(int dim,int moved[15][15]) {
    int i,j;
    for(i = 1; i <= dim; i++) {
        for(j = 1; j <= dim; j++) {
            if(moved[i-1][j-1] != -1) {
                printf("|  %d  |", moved[i-1][j-1]);
            }
            else printf("|     |");
        }
        printf("\n");
        for(j = 1; j <= dim; j++) printf("|_____|");
        printf("\n");
    }
}

/*--------------------------*/
//next function for print the possible movements for the knight.

int next(int *firstX, int *firstY, int dimension,int allmoves[15][15],int *counter) {
   int moves[8][2],i = 0,count = 0,next_move;
    if(*firstX + 2 <= dimension && *firstY + 1 <= dimension ) {
        moves[i][0] = *firstX + 2;
        moves[i][1] = *firstY + 1;
        i++;
        count++;
        if(moves[i-1][0] != moves[i-1][1]) {
            moves[i][0] = moves[i - 1][1];
            moves[i][1] = moves[i - 1][0];
            i++;
            count++;
        }
    }
    if(*firstX + 2 <= dimension && *firstY - 1 <= dimension && *firstY - 1 > 0) {
        moves[i][0] = *firstX + 2;
        moves[i][1] = *firstY - 1;
        i++;
        count++;
        if(moves[i-1][0] != moves[i-1][1]) {
            moves[i][0] = moves[i - 1][1];
            moves[i][1] = moves[i - 1][0];
            i++;
            count++;
        }
    }
    if(*firstX - 2 <= dimension && *firstX - 2 > 0 && *firstY + 1 <= dimension) {
        moves[i][0] = *firstX - 2;
        moves[i][1] = *firstY + 1;
        i++;
        count++;
        if(moves[i-1][0] != moves[i-1][1]) {
            moves[i][0] = moves[i - 1][1];
            moves[i][1] = moves[i - 1][0];
            i++;
            count++;
        }
    }
    if(*firstX - 2 <= dimension && *firstX - 2 > 0 && *firstY - 1 <= dimension && *firstY - 1 > 0) {
        moves[i][0] = *firstX - 2;
        moves[i][1] = *firstY - 1;
        i++;
        count++;
        if(moves[i-1][0] != moves[i-1][1]) {
            moves[i][0] = moves[i - 1][1];
            moves[i][1] = moves[i - 1][0];
            i++;
            count++;
        }
    }
    for(i = 0; i < count; i++) printf("%d. (%d , %d)\n", i+1, moves[i][0],moves[i][1]);
    int sw = 1;
    printf("Choose one movement: ");
    scanf("%d", &next_move);
    while(sw) {
        if (allmoves[moves[next_move][0]][moves[next_move][1]] == -1)  {
            allmoves[moves[next_move][0]][moves[next_move][1]] = *counter;
            *counter++;
            *firstX = moves[next_move][0];
            *firstY = moves[next_move][1];
            sw = 0;
        }
        else sw = 1;
    }
}
void main() {
    const int maxRow = 100, maxCol = 100;
    int k,j,n, row, col, board[maxRow][maxCol],i = 0,moves[8][2],counter = 0,moved[15][15];
    for(i = 0; i < 15; i++) {
        for(j = 0; j < 15; j++) {
            moved[i][j] = -1;
        }
    }
    char answer;
    printf("Please Enter The Dimensions: ");
    scanf("%d", &n);
    printf("enter the first position of Knight: ");
    scanf("%d %d", &row, &col);
    moved[row-1][col-1] = counter;
    counter++;
    while(answer != 'e') {
        printf("\n What Do You Want ?! (n, b, p, e)\n");
        scanf("%s", &answer);
        if(answer == 'n') {
            next(&row,&col,n,moved,&counter);
        }
        else if(answer == 'p')
            print(n,moved[15][15]);
        else
            printf("It's WRONG ! \n");
    }

}

as you see, i used a 2d array for marking the movements, 0 for the first move, 1 for the second and so on .
the next function does work well but the problem is when i call the print function, the program crash!
i'll appreciate any help. thank you.

One thing I see, is that you're passing the moved array to the print function incorrectly:

print(n,moved);

Without any sort of error message, it's hard to say if that is the problem though.

Edited 10 Months Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.