矢量用法分割故障

时间:2015-11-07 20:54:45

标签: c++ vector segmentation-fault

我的代码出现分段错误。这是一款可以被认为是x-o-x的游戏。 playable_dots检查点是否可播放的函数。另一个是计算机转动,所以电脑运动。据我消除,我怀疑他们两个。我也检查边界,但我确定边界没有问题。在转换矢量之前,即const矢量<矢量> & board,vector<矢量> & wriggles,它运作良好,因为它们是数组。 char board[MAX][MAX]bool wriggles[MAX][MAX]对于所有代码:http://bit.ly/1Phz76d

可玩的动作:

// 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;
}

计算机移动:

// 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;
}

对于边界:

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;
}

enter image description here

enter image description here

0 个答案:

没有答案