我刚刚开始学习 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,整个控制台应用程序就会停止响应,并停在那里,只让我退出。