1

C++ について学び始めたばかりで、現在、実行するたびにクラッシュするこの for ループに悩まされています (これはより大きなコードの一部です)。

void placeItem()
{
    int settler = 20, castle = 5, tower = 10, mine = 10, E = 100;
    int player1Settler = 0, player1Castle = 0, player1Tower = 0, player1Mine = 0;
    int player2Settler = 0, player2Castle = 0, player2Tower = 0, player2Mine = 0;
    int dice, x, y;
    currentBoardStatus();
    cout << "Player " << playerTurn(1 || 2) << " starts first." << endl;
    for (int game = 1; game <= 46; game++)
    {
        system("CLS");
        cout << "Turn #" << game << endl;
        dice = rand() % 6 + 1;
        x = rand() % 10 + 1;
        y = rand() % 10 + 1;
        cout << "Dice number: " << dice << endl;
        if (dice < 4 && settler > 0)
        {
            cout << "Settler placed on [" << x << "] [" << y << "]" << endl;
            gameBoard(x, y) = "S";
            settler = settler--;
            if (playerTurn(1))
            {
                player1Settler = player1Settler + 1;
            }
            else
            {
                player2Settler = player2Settler + 1;
            }
            currentBoardStatus();
        }
        else if (dice = 4 && castle > 0)
        {
            cout << "Castle placed on [" << x << "] [" << y << "]" << endl;
            gameBoard(x, y) = "C";
            castle = castle--;
            if (playerTurn(1))
            {
                player1Castle = player1Castle + 1;
            }
            else
            {
                player2Castle = player2Castle + 1;
            }
            currentBoardStatus();
        }
        else if (dice = 5 && tower > 0)
        {
            cout << "Tower placed on [" << x << "] [" << y << "]" << endl;
            gameBoard(x, y) = "T";
            tower = tower--;
            if (playerTurn(1))
            {
                player1Tower = player1Tower + 1;
            }
            else
            {
                player2Tower = player2Tower + 1;
            }
            currentBoardStatus();
        }
        else if (dice = 6 && mine > 0)
        {
            cout << "Mine placed on [" << x << "] [" << y << "]" << endl;
            gameBoard(x, y) = "M";
            mine = mine--;
            if (playerTurn(1))
            {
                player1Mine = player1Mine + 1;
            }
            else
            {
                player2Mine = player2Mine + 1;
            }
            currentBoardStatus();
        }
        else
        {
            cout << "Player skips his/her turn." << endl;
        }
        nextTurn();
        //currentBoardStatus();
        system("pause");
    }
}

ループが 16 になるとすぐに、コンソール アプリケーション全体が応答を停止し、そこで停止し、終了するだけです。

4

1 に答える 1

2

あなたのコードは次の理由でクラッシュします

string gameBoard(int i, int j)
{
    string arr[10][10];
    return arr[i][j];
}

x = rand() % 10 + 1;
y = rand() % 10 + 1;

xandyは 1 から 10 の間の値を持ちますが、arr のサイズは 10 で、その最大有効インデックスは 9 です。したがって、xoryが 10 (または両方) の場合、未定義のメモリにアクセスします。

これとは別に、コードには次のような多くの欠陥があります。

  1. dice = 4値を比較したい場合(dice@KeithSmithが指摘したように、実際には値4を割り当てます)
  2. で作成string arrして使用することはありませんが、内部でint main()別の宣言を行い、そこからインデックスを返します (これは未定義です)。string arrstring gameBoard()
于 2013-08-03T07:30:06.530 に答える