help me debug C code, please ...

Sharky Forums


Results 1 to 2 of 2

Thread: help me debug C code, please ...

  1. #1
    Mako Shark slavik's Avatar
    Join Date
    May 2001
    Location
    Brooklyn
    Posts
    3,308

    help me debug C code, please ...

    this is a program I personally wrote in C to solve the knight's tour problem on an 8 by 8 board with user entering the square ...

    here's the prblem ... if you use a1 as the starting square (0, 0 because of 2d array), it will get to h1, where there is only one move, which turns out to be f2 ... but it doesn't move the knight there for some reason (it is supposed to select CASE 6, but it doesn't) ... this happens on move 17 (when the knight is supposed to go to f2) ...

    the idea was to have it goto to next possible square (each one) and give it a rating (how many moves are available from that square), then choose the lost rating and move the knight there ... rinse repeat ...

    #include <stdio.h>

    void getstart(int board[][8], int *row, int *col, int movelist[][2], int *movelist_index);
    void makemove(int *row, int *col, int board[][8], int movelist[][2], int *movelist_index);
    int nummoves(int row, int col, int board[][8]);

    void main()
    {
    int board[8][8]={0};
    int movelist[64][2]={0};
    int movelist_index=0;
    int row;
    int col;
    int loop_count;

    getstart(board,&row,&col,movelist,&movelist_index);
    printf("\nStarting square= %c%d\n",movelist[0][0],movelist[0][1]);

    for(loop_count=1;loop_count<63;loop_count++) {
    makemove(&row,&col,board,movelist,&movelist_index);
    // printf("%c%d\n",movelist[loop_count][0],movelist[loop_count][1]);
    printf("%d\t%c%d\n",movelist_index,(movelist[loop_count][0])+97,(movelist[loop_count][1])+1);
    }
    }

    void getstart(int board[][8], int *row, int *col, int movelist[][2], int *movelist_index)
    {

    printf("Please select the starting move by following the instructions!\n\n");
    printf(" 7 a8 b8 c8 d8 e8 f8 g8 h8\n");
    printf(" 6 a7 b7 c7 d7 e7 f7 g7 h1\n");
    printf("R 5 a6 b6 c6 d6 e6 f6 g6 h1\n");
    printf("O 4 a5 b5 c5 d5 e5 f5 g5 h1\n");
    printf("W 3 a4 b4 c4 d4 e4 f4 g4 h1\n");
    printf("S 2 a3 b3 c3 d3 e3 f3 g3 h1\n");
    printf(" 1 a2 b2 c2 d2 e2 f2 g2 h1\n");
    printf(" 0 a1 b1 c1 d1 e1 f1 g1 h1\n\n");
    printf(" 0 1 2 3 4 5 6 7\n\n");
    printf("\t\tCOLUMNS\n");

    printf("INSTRUCTIONS:\n");
    printf("- look at the square you want to start at\n");
    printf("- note the COLUMN and the ROW where it is located\n");
    printf("- enter the COLUMN, then the ROW as promted\n\n");

    do {
    printf("Please enter the COLUMN: ");
    scanf("%d",col);

    if (*col<0||*col>7) {
    printf("COLUMN is out of range, it must be between 0 and 7 inclusively\n");
    }

    } while (*col<0||*col>7);

    do {
    printf("Please enter the ROW: ");
    scanf("%d",row);

    if (*row<0||*row>7) {
    printf("ROW is out of range, it must be between 0 and 7 inclusively\n");
    }

    } while (*row<0||*row>7);

    board[*row][*col]=1;

    movelist[*movelist_index][0]=((*col)+97);
    movelist[*movelist_index][1]=((*row)+1);
    (*movelist_index)++;

    }

    void makemove(int *row, int *col, int board[][8], int movelist[][2], int *movelist_index)
    {
    int move[8]={0};
    int counter;
    int temp;
    int num_moves=10;

    // printf("%d\n",*movelist_index);
    if (((*row)-2>=0)&&((*col)-1>=0)&&(board[(*row)-2][(*col)-1]!=1)) {
    // printf("move[0] is getting called\n");
    move[0]=nummoves((*row)-2,(*col)-1,board);
    }

    if (((*row)-2>=0)&&((*col)+1<=7)&&(board[(*row)-2][(*col)+1]!=1)) {
    move[1]=nummoves((*row)-2,(*col)+1,board);
    // printf("move[1] is getting called\n");
    }

    if (((*row)+2<=7)&&((*col)-1>=0)&&(board[(*row)+2][(*col)-1]!=1)) {
    move[2]=nummoves((*row)+2,(*col)-1,board);
    // printf("move[2] is getting called\n");
    }

    if (((*row)+2<=7)&&((*col)+1<=7)&&(board[(*row)+2][(*col)+1]!=1)) {
    move[3]=nummoves((*row)+2,(*col)+1,board);
    // printf("move[3] is getting called\n");
    }

    if (((*row)-1>=0)&&((*col)-2>=0)&&(board[(*row)-1][(*col)-2]!=1)) {
    move[4]=nummoves((*row)-1,(*col)-2,board);
    // printf("move[4] is getting called\n");
    }

    if (((*row)-1>=0)&&((*col)+2<=7)&&(board[(*row)-1][(*col)+2]!=1)) {
    move[5]=nummoves((*row)-1,(*col)+2,board);
    // printf("move[5] is getting called\n");
    }

    if (((*row)+1<=7)&&((*col)-2>=7)&&(board[(*row)+1][(*col)-2]!=1)) {
    move[6]=nummoves((*row)+1,(*col)-2,board);
    // printf("move[6] is getting called\n");
    }

    if (((*row)+1<=7)&&((*col)+2<=7)&&(board[(*row)+1][(*col)+2]!=1)) {
    move[7]=nummoves((*row)+1,(*col)+2,board);
    // printf("move[7] is getting called\n");
    }

    for(counter=0;counter<=7;counter++) {
    if (move[counter]<num_moves&&move[counter]>0) {
    temp=counter;
    num_moves=move[counter];
    }
    }

    printf("temp=%d\n",temp);

    switch (temp) {
    case 0:
    (*row)-=2;
    (*col)-=1;
    break;
    case 1:
    (*row)-=2;
    (*col)+=1;
    break;
    case 2:
    (*row)+=2;
    (*col)-=1;
    break;
    case 3:
    (*row)+=2;
    (*col)+=1;
    break;
    case 4:
    (*row)-=1;
    (*col)-=2;
    break;
    case 5:
    (*row)-=1;
    (*col)+=2;
    break;
    case 6:
    (*row)+=1;
    (*col)-=2;
    break;
    case 7:
    (*row)+=1;
    (*col)+=2;
    break;
    }

    board[*row][*col]=1;
    movelist[*movelist_index][0]=(*col);
    movelist[*movelist_index][1]=(*row);
    (*movelist_index)++;

    }

    int nummoves(int row, int col, int board[][8])
    {
    int count=0;

    if (((row)-2>=0)&&((col)-1>=0)&&(board[row-2][col-1]!=1))
    count++;

    if (((row)-2>=0)&&((col)+1<=7)&&(board[row-2][col+1]!=1))
    count++;

    if (((row)+2<=7)&&((col)-1>=0)&&(board[row+2][col-1]!=1))
    count++;

    if (((row)+2<=7)&&((col)+1<=7)&&(board[row+2][col+1]!=1))
    count++;

    if (((row)-1>=0)&&((col)-2>=0)&&(board[row-1][col-2]!=1))
    count++;

    if (((row)-1>=0)&&((col)+2<=7)&&(board[row-1][col+2]!=1))
    count++;

    if (((row)+1<=7)&&((col)-2>=0)&&(board[row+1][col-2]!=1))
    count++;

    if (((row)+1<=7)&&((col)+2<=7)&&(board[row+1][col+2]!=1))
    count++;

    // printf("count= %d\n",count);
    return (count);
    }
    Activation? What activation?
    Quote Originally Posted by Geekkit (from ubuntu forums regarding 'goto' statement)
    Yep it sure does. So does crack cocaine. Existence is not a valid endorsement for being acceptable.
    Quote Originally Posted by Linus Torvalds
    Only wimps use tape backup: _real_ men just upload their important stuff on ftp, and let the rest of the world mirror it

  2. #2
    Hammerhead Shark nattylife's Avatar
    Join Date
    Jul 2003
    Location
    So Fla
    Posts
    1,303
    here are some things i noticed:
    - just a helpful hint, use indenting often and line up yer brackets, yer code is kinda messy to look at, helps others like me to understand yer code better if its written neatly
    - if it reaches h1, then it can go to either f2 OR g3 right?
    - always try to add a default case in yer switch statement...
    - a major prob is that you put the code into a loop that will ALWAYS execute 63 times so you need put this loop in a decision stmt or put a decision stmt in the loop to stop it after it reaches a point where the knight cant move anymore...

    - when i have more time at home to look it over ill send you the fixed version...
    eVGA 680i A1
    Intel Core 2 Quad Q6600 [email protected]
    2x2 Patriot Extreme DDR2800
    Tuniq Tower 120
    120G WD
    120G Seagate
    160G Seagate
    160G Seagate
    eVGA 8800GTX (borrowing a friends)


    http://miniprofile.xfire.com/bg/bg/type/0/badash666.png

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •