当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - vector usage segmentation fault

问题描述:

I get segmentation fault on my code. It is a game that can be thought like x-o-x. A function that playable_dots checks dots whether it is playable. The other one is for turn of computer so computer movement. As far as I eliminate I doubt both of them. I also check boundries but I'm sure there is no issue about boundries. Before conversion of vector i.e. const vector < vector > &board, vector < vector > &wriggles, it works well because they were arrays. char board[MAX][MAX]

and bool wriggles[MAX][MAX] For all code: http://bit.ly/1Phz76d

Playable moves:

// Precondition: p_size >= 4 and even, 2D char array, a specified char

// 2D bool array

// Postcondition: determine proper playable dots and return how many

// playable dots there are, otherwise 0, stand for true

int playable_moves(const vector < vector<char> > &board, vector < vector<bool> > &wriggles,

char const player, int const p_size)

{

bool go_on;

int wriggles_count = 0;

for(int i = 0 ; i < p_size ; ++i)

for(int j = 0 ; j < p_size ; ++j)

wriggles[i][j] = false;

// search legal playable dots

for(int i = 0 ; i < p_size ; ++i)

for(int j = 0 ; j < p_size ; ++j)

{

if(board[i][j] != what_symbol) // if not dot

continue; // don't do rest of the code

// increment the loop

for(int ii = 1 ; ii > -2 ; --ii)

for(int jj = 1 ; jj > -2 ; --jj)

{

if(check_boundry_for_rival(i, j, ii, jj, p_size))

continue;

if(specify_rival(player) == board[i + ii][j + jj])

{

go_on = true;

int x_coor = i + ii;

int y_coor = j + jj;

while(go_on)

{

x_coor += ii;

y_coor += jj;

if(check_boundry_for_player(x_coor, y_coor, p_size)

|| board[x_coor][y_coor] == what_symbol)

go_on = false;

else if(player == board[x_coor][y_coor])

{

wriggles_count += 1;

//if(p_symbol == player)

// cout << i + 1 << static_cast<char>(j + 'a') << ", ";

wriggles[i][j] = true;

go_on = false;

}

}

}

}

}

return wriggles_count;

}

computer moves:

// Precondition: p_size >= 4 and even, 2D char array, a specified char

// 2D bool array

// Postcondition: determine proper playable dots and return how many

// playable dots there are, otherwise 0, stand for true

int computer_moves(const vector < vector<char> > &board, vector < vector<bool> > &wriggles,

char const player, int const p_size)

{

bool go_on;

int wriggles_count = 0;

for(int i = 0 ; i < p_size ; ++i)

for(int j = 0 ; j < p_size ; ++j)

wriggles[i][j] = false;

// search legal playable dots

for(int i = 0 ; i < p_size ; ++i)

for(int j = 0 ; j < p_size ; ++j)

{

if(board[i][j] != what_symbol) // if not dot

continue; // don't do rest of the code

// increment the loop

for(int ii = 1 ; ii > -2 ; --ii)

for(int jj = 1 ; jj > -2 ; --jj)

{

if(check_boundry_for_rival(i, j, ii, jj, p_size))

continue;

if(specify_rival(player) == board[i + ii][j + jj])

{

go_on = true;

int x_coor = i + ii;

int y_coor = j + jj;

while(go_on)

{

x_coor += ii;

y_coor += jj;

if(check_boundry_for_player(x_coor, y_coor, p_size)

|| board[x_coor][y_coor] == what_symbol)

go_on = false;

else if(player == board[x_coor][y_coor])

{

wriggles_count += 1;

//if(p_symbol == player)

// cout << i + 1 << static_cast<char>(j + 'a') << ", ";

wriggles[i][j] = true;

go_on = false;

}

}

}

}

}

return wriggles_count;

}

For boundries:

bool check_boundry_for_rival(int const i,int const j,

int const around_r, int const around_c, int const p_size)

{

return (i == 0 && around_r == -1) || i + around_r >= p_size || // outta up range

(j == 0 && around_c == -1) || j + around_c >= p_size || // outta down range

(around_r == 0 && around_c == 0);// if the coordinate itself

}

// Precondition: p_size >= 4 and even, the others must be int int

// Postcondition: while the scanning with same coordinate outta boundry return false

bool check_boundry_for_player(int const i,int const j, int const p_size)

{

return p_size + 1 < i || i < 0 || p_size + 1 < j || j < 0;

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: