c - segmentation fault because of character changement -
i'm writing board game detailed information. segmentation fault changing problem_ch
' '
(space) '.'
. when play moves , ' '
, don't when play moves , '.'
i get. , don't understand happening.
solved: because of negative index, segmentation fault. handling, solved.
following moves:
3d 2d 5f 2e 1e 4b 4c 4a 3b @ 4b segmentation fault
code:
void make_move(char board[][size], size_t row, size_t col, char player) { int rowdelta = 0; // row increment int coldelta = 0; // column increment size_t x = 0; // row index searching size_t y = 0; // column index searching // identify opponent char opponent = (player == player_ch) ? computer_ch : player_ch; board[row][col] = player; // place player counter // check squares around square opponents counter for(rowdelta = -1 ; rowdelta <= 1 ; ++rowdelta) for(coldelta = -1; coldelta <= 1; ++coldelta) { // don’t check off board, or current square if((row == 0 && rowdelta == -1) || row + rowdelta >= size || (col == 0 && coldelta == -1) || col + coldelta >= size || (rowdelta == 0 && coldelta == 0)) continue; // check square if(board[row + rowdelta][col + coldelta] == opponent) { // found opponent search in same direction player counter x = row + rowdelta; // move opponent y = col + coldelta; // square for(;;) { x += rowdelta; // move y += coldelta; // next square if(board[x][y] == problem_ch) // if square blank... break; // ...give // if find player counter, go backward here // changing opponents counters player if(board[x][y] == player) { while(board[x -= rowdelta][y -= coldelta] == opponent) // opponent? board[x][y] = player; // yes, change break; // done } } } } }
Comments
Post a Comment