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;

x并且y将有一个介于 1 和 10 之间的值,但 arr 的大小为 10,其最大有效索引为 9。因此,如果xory为 10(或两者),它们将访问未定义的内存。

除此之外,您的代码还有许多缺陷,例如:

  1. dice = 4当想要比较值时(实际上将值 4 分配给dice@KeithSmith 指出的)
  2. string arr您在其中创建但从不使用它,但在内部int main()声明另一个然后从中返回索引的事实(未定义)string arrstring gameBoard()
于 2013-08-03T07:30:06.530 回答