我正在写一个棋盘游戏detailed information。我只是将problem_ch
从' '
(空格)更改为'.'
,从而导致细分错误。当我通过移动和' '
进行游戏时,我不会得到,但是当我按照移动进行游戏时,我得到了'.'
。我真的真的不明白发生了什么。
解决:由于负指数,我得到了分段错误。通过 处理,它解决了。
跟随行动:
3d
2d
5f
2e
1e
4b
4c
4a
3b
at 4b segmentation fault
代码:
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 for searching
size_t y = 0; // Column index for searching
// Identify opponent
char opponent = (player == player_ch) ? computer_ch : player_ch;
board[row][col] = player; // Place the player counter
// Check all squares around this square for opponents counter
for(rowdelta = -1 ; rowdelta <= 1 ; ++rowdelta)
for(coldelta = -1; coldelta <= 1; ++coldelta)
{
// Don’t check off the board, or the current square
if((row == 0 && rowdelta == -1) || row + rowdelta >= SIZE ||
(col == 0 && coldelta == -1) || col + coldelta >= SIZE ||
(rowdelta == 0 && coldelta == 0))
continue;
// Now check the square
if(board[row + rowdelta][col + coldelta] == opponent)
{ // Found opponent so search in same direction for player counter
x = row + rowdelta; // Move to opponent
y = col + coldelta; // square
for(;;)
{
x += rowdelta; // Move to the
y += coldelta; // next square
if(board[x][y] == problem_ch) // If square is blank...
break; // ...give up
// If we find the player counter, go backward from here
// changing all the opponents counters to player
if(board[x][y] == player)
{
while(board[x -= rowdelta][y -= coldelta] == opponent) // Opponent?
board[x][y] = player; // Yes, change it
break; // We are done
}
}
}
}
}