[英]Segmentation fault for minesweeper program c++
我認為問題出在我的構造函數中。 它開始運行並將炸彈“放入”二維數組(網格),但在中間停止。 (有時它會貫穿整個過程...
SweeperGrid::SweeperGrid(const int initialRows, const int initialCols, const int density){
if ((initialRows<5 || initialCols<5) || (density<25 || density>75)) {
throw out_of_range("Grid not large enough (number of rows or columns cannot be fewer than 5) or density is too low or high (must be between 25% and 75%)");
}
numRows = initialRows;
numColumns = initialCols;
numBombs = 0;
grid = new SweeperCell*[numRows];
for(int i=0; i <numRows; i++){
grid[i] = new SweeperCell[numColumns];
}
srand(time(0));
for(int i=0; i<numRows; i++){
for (int j=0; j<numColumns; j++){
if(rand()%100+1<=density){
PlaceBomb(i, j);
}
}
}
}
這是PlaceBomb函數:
void SweeperGrid::PlaceBomb(int row, int col){
cout<<row<<", "<<col<<endl;
if ((row<0||row>=numRows)||(col<0||col>=numColumns)){
throw out_of_range("Out of bounds (PlaceBomb)");
}
At(row, col).PlaceBomb();
numBombs++;
//add if statements so it doesn't go out of bounds
if (row==0) {
At(row+1, col).IncrementNumAdjacent();
At(row, col-1).IncrementNumAdjacent();
At(row, col+1).IncrementNumAdjacent();
At(row+1, col+1).IncrementNumAdjacent();
At(row+1, col-1).IncrementNumAdjacent();
}
if (row==0&&col==0) {
At(row+1, col).IncrementNumAdjacent();
At(row, col+1).IncrementNumAdjacent();
At(row+1, col+1).IncrementNumAdjacent();
}
if (col==0) {
At(row-1, col).IncrementNumAdjacent();
At(row+1, col).IncrementNumAdjacent();
At(row, col+1).IncrementNumAdjacent();
At(row+1, col+1).IncrementNumAdjacent();
At(row-1, col+1).IncrementNumAdjacent();
}
if (row==numRows-1) {
At(row-1, col).IncrementNumAdjacent();
At(row, col-1).IncrementNumAdjacent();
At(row, col+1).IncrementNumAdjacent();
At(row-1, col-1).IncrementNumAdjacent();
At(row-1, col+1).IncrementNumAdjacent();
}
if (row==numRows-1&&col==numColumns-1) {
At(row-1, col).IncrementNumAdjacent();
At(row, col-1).IncrementNumAdjacent();
At(row-1, col-1).IncrementNumAdjacent();
}
if (col==numColumns-1) {
At(row-1, col).IncrementNumAdjacent();
At(row+1, col).IncrementNumAdjacent();
At(row, col-1).IncrementNumAdjacent();
At(row-1, col-1).IncrementNumAdjacent();
At(row+1, col-1).IncrementNumAdjacent();
}
if (row==0&&col==numColumns-1) {
At(row+1, col).IncrementNumAdjacent();
At(row, col-1).IncrementNumAdjacent();
At(row+1, col-1).IncrementNumAdjacent();
}
if (row==numRows-1&&col==0) {
At(row-1, col).IncrementNumAdjacent();
At(row, col+1).IncrementNumAdjacent();
At(row-1, col+1).IncrementNumAdjacent();
}
** IncrementNumAdjacent在另一個文件中,我確定它可以工作。
您的IncrementNumAdjacent
調用過多,其中一些可能超出范圍。
例如:
if (row==0) {
At(row+1, col).IncrementNumAdjacent();
At(row, col-1).IncrementNumAdjacent();
如果此時col
為0會怎樣?
在您的SweeperGrid
類中擁有一個safeIncrementNumAdjacent(int row, int col)
方法可能會更容易,而該方法只是忽略了超出范圍的項目。 就像是:
void SweeperGrid::safeIncrementNumAdjacent(int row, int col) {
if (row >= 0 && row < numRows && col >= 0 & col < numCols)
At(row, col).IncrementNumAdjacent();
}
然后只需調用當前點周圍的8個位置即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.