空字符随机添加到字符串C ++中

时间:2017-05-24 02:37:23

标签: c++

我用C ++编写了一个简单的战舰游戏。在游戏的几次迭代之后,其中一个字符串在" Player"对象被改变了。此更改是将多个空字符添加到字符串的末尾。否则,对象的其余部分不受影响。例如,如果播放器类型为" cpu",则播放器类型切换为" cpu \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0"。我相信导致问题的代码行是:

currPlayer->getStrategy().getNextAttack(nextPlayer->getBoard(1));

以下是getNextAttack()的代码:

int Strategy::getNextAttack(Board enemyBoard) {
    //clear prob board
    for(int i = 0; i < 100; i++) {
        probBoard[i] = 0;
    }

    //reset largest ship
    largestShip = 0;

    //assign largest ship
    for(int i = 0; i < 100; i++) {
        Ship currShip = enemyBoard.getShipByCoord(i);
        if(!currShip.isSunk()) { //if ship is still afloat
            if(currShip.getSize() > largestShip) { largestShip = currShip.getSize(); } //reassign largest ship on board
        }
    }

    //assign base prob
    std::vector<int> allPossible;
    //for all horiz coords
    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < (10 - largestShip +1); j++) {
            for(int k = 0; k < (largestShip); k++) {
                if(!enemyBoard.beenHit((i*10) + j + k) || (enemyBoard.beenHit((i*10) + j + k) && !enemyBoard.getShipByCoord((i*10) + j + k).isSunk())) { //if not hit or if hit but contains a ship that is not sunk
                    allPossible.push_back((i*10) + j + k);
                }
                else {
                    for(int m = 0; m < k; m++) {
                        allPossible.pop_back(); //should delete last element
                    }
                    break;
                }
            }
            //for all vert coords
            for(int z = 0; z < (largestShip); z++) {
                if(!enemyBoard.beenHit(((j+z)*10) + i)) {
                    allPossible.push_back(((j+z)*10) + i);
                }
                else {
                    for(int m = 0; m < z; m++) {
                        allPossible.pop_back(); //should delete last element
                    }
                    break;
                }
            }
        }
    }

    for(int p = 0; p < allPossible.size(); p++) {
        probBoard[allPossible[p]] += 1;
    }


    //add improvements based on hits
    for(int i = 0; i < 10; i++) {
        for(int k = 0; k < 10; k++) {
            int currCoord = (i*10) + k;
            int leftCoord = (i*10) + k-1;
            int rightCoord = (i*10) + k+1;
            int upCoord = ((i-1)*10) + k;
            int downCoord = ((i+1)*10) + k;
            if(enemyBoard.beenHit(currCoord) && (enemyBoard.getShipByCoord(currCoord).getName() != "") && !enemyBoard.getShipByCoord(currCoord).isSunk()) { //currCoord is a coordinate that has been hit, contains a ship and is not sunk
                if((enemyBoard.beenHit(leftCoord) || enemyBoard.beenHit(rightCoord)) && (enemyBoard.getShipByCoord(leftCoord) == enemyBoard.getShipByCoord(currCoord) || enemyBoard.getShipByCoord(rightCoord) == enemyBoard.getShipByCoord(currCoord))) { //if space to left or right is hit and the same ship
                    //increment only the left and right
                    if(!enemyBoard.getShipByCoord(currCoord).isSunk()) { //ship cannot be sunk as well
                        probBoard[leftCoord] += 25;
                        probBoard[rightCoord] += 25;
                    }
                }

                else if((enemyBoard.beenHit(upCoord) || enemyBoard.beenHit(downCoord)) && (enemyBoard.getShipByCoord(upCoord) == enemyBoard.getShipByCoord(currCoord) || enemyBoard.getShipByCoord(downCoord) == enemyBoard.getShipByCoord(currCoord))) { //if space on top or bottom is hit and the same ship and not sunk
                    //increment only the top and bottom
                    if(!enemyBoard.getShipByCoord(currCoord).isSunk()) { //ship cannot be sunk as well
                        probBoard[upCoord] += 25;
                        probBoard[downCoord] += 25;
                    }
                }

                //if no direct spaces in any direction to hit coord, increment top, bot, left, and right equally
                else {
                    probBoard[upCoord] += 20;
                    probBoard[downCoord] += 20;
                    probBoard[leftCoord] += 20;
                    probBoard[rightCoord] += 20;
                }
            }
        }
    }

    //marks odds at 0 if already fired upon
    for(int n = 0; n < 100; n++) {
        if(enemyBoard.beenHit(n)) {
            probBoard[n] = 0;
        }
    }

    //find next best attack coord based on prob board
    int highestValue = 0;
    std::vector<int> highestSpaces;
    for(int j = 0; j < 100; j++) {
        if(probBoard[j] > highestValue) { highestValue = probBoard[j]; }
    }
    for(int r = 0; r < 100; r++) {
        if(probBoard[r] == highestValue) {
            highestSpaces.push_back(r);
        }
    }
    srand(static_cast<unsigned int>(time(NULL)));
    int randNum = rand() % highestSpaces.size();
    return highestSpaces[randNum];
}

感谢您的阅读和任何帮助!

1 个答案:

答案 0 :(得分:0)

当行或列为0或9时,它看起来会超出边缘的数组边界:

 probBoard[upCoord] += 20;
 probBoard[downCoord] += 20;
 probBoard[leftCoord] += 20;
 probBoard[rightCoord] += 20;