samedi 31 octobre 2020

Segmentation fault adding condition to if statement

I'am writing a program that performs the knight's tour. For now it does just the first move.

If I run it I can see the first move performed, however since the move is of one row and one column it's not an allowed move for knights in chess. To fix that I need to add the condition j != i into this if statement:

for( i = 1; i < 3; ++i ) {
    for( j = 1; j < 3; ++j ) { 
        if( r + i < 8 && c + i < 8 ) {
            ...
        }
    }
}

So that it becomes:

if( j != i && r + i < 8 && c + i < 8 )

But if I run the "fixed" program, the compiler gives me a segmentation fault for the line of the modified if. Any idea of what does it mean?

EDIT: I am posting the full program. When the program moves the knight, it sets the destination board[row][column] to 0 (zero)

For now I just tried starting from the top left corner. I know that the starting point is not set to zero and I noticed that the double for with the guilty if should start for both i and j = -2. Also there are repeated actions that I could wrap into a new function. However I am not here to fix these stuff. I just want to get rid of the segmentation fault.

Ps: in case it's relevant, I am writing and running the code into an app on my phone

#include <stdio.h>
#include <stdlib.h>

int board[8][8];

void euBoard( void );
void move( int r, int c );
int checkThat( int r, int c);
void printBoard( void );

int main() {



euBoard();

printBoard();

puts("");

move( 0, 0 );

printBoard();



return 0;
} // end main

void euBoard( void )
{
int i, j;

for( i = 0; i < 8; ++i ) {
    for( j = 0; j < 8; ++j ) { 
        if( i < 4 && j < 4 ) {
            board[i][j] = i + j + 2;
        }
        else if( i < 4 && j >= 4 ) {
            board[i][j] = i + 7 - j +2;
        }
        else if( i >= 4 && j < 4 ) {
            board[i][j] = 7 - i + j + 2;
        }
        else if( i >= 4 && j >= 4 ) {
            board[i][j] = 14 - i - j + 2;
        }
    }
}
} // end euBoard

void move( int r, int c )
{
int i, j;
int best = 9;
int possible[8][3];
int possCount = 0;
int bestPo[8];
int bestCount = 0;
int ct[8];
int ctCount = 0;
int ctBest = 9;
int ctBestCount = 0;
int ctBests[8];
int lucky;

for( i = 1; i < 3; ++i ) {
    for( j = 1; j < 3; ++j ) { 
        if( r + i < 8 && c + i < 8 ) {
            if( board[r+i][c+j] <= best ) {
                best = board[r+i][c+j];
                possible[possCount][0] = r + i;
                possible[possCount][1] = c + j;
                possible[possCount][2] = board[r+i][c+j];
                ++possCount;
            }
        } // end outer if
    }
}

for( i = 0; i <= possCount; ++i ) {
    if( possible[i][2] == best ) {
        bestPo[bestCount] = i;
        ++bestCount;
    }
}
if( bestCount == 1 ) {
    board[possible[bestPo[0]][0]][possible[bestPo[0]][1]] = 0;
}
else {
    for( i = 0; i <= bestCount; ++i ) {
        ct[ctCount] = checkThat( possible[bestPo[i]][0], possible[bestPo[i]][1] );
        if( ct[ctCount] < ctBest ) {
            ctBest = ct[ctCount];
        }
        ++ctCount;
    }
    for( i = 0; i <= ctCount; ++i ) {
        if( ct[i] == ctBest ) {
            ctBests[ctBestCount] = i;
            ++ctBestCount;
        }
    }
    if( ctBestCount == 1 ) {
        board[possible[bestPo[0]][0]][possible[bestPo[0]][1]] = 0;
    }
    else {
        lucky = rand() % ctCount;
        board[possible[lucky][0]][possible[lucky][1]] = 0;
    }
} // end outer else
} // end move

int checkThat( int r, int c)
{
int i, j;
int best = 9;
int possible[8][3];
int possCount = 0;
int bestPo[8];
int bestCount = 0;
int lucky;

for( i = 1; i < 3; ++i ) {
    for( j = 1; j < 3; ++j ) { 
        if( r + i < 8 && c + i < 8 ) {
            if( board[r+i][c+j] <= best ) {
                best = board[r+i][c+j];
                possible[possCount][0] = r + 1;
                possible[possCount][1] = c + j;
                possible[possCount][2] = board[r+i][c+j];
                ++possCount;
            }
        } // end outer if
    }
}

for( i = 0; i <= possCount; ++i ) {
    if( possible[i][2] == best ) {
        bestPo[bestCount] = i;
        ++bestCount;
    }
}
if( bestCount == 1 ) {
    return possible[bestPo[0]][2];
}
else {
    lucky = rand() % bestCount;
    return possible[bestPo[lucky]][2];
}
} // end checkThat

void printBoard( void )
{
int i, j;

for( i = 0; i < 8; ++i ) {
    for( j = 0; j < 8; ++j ) { 
        printf("%d ", board[i][j] );
    }
    puts("");
}
}

Aucun commentaire:

Enregistrer un commentaire