我写了一个名为“nqueens”的迷你游戏,它利用数组代表棋盘。我为游戏写了5个函数。但是,每次游戏到达时:
if (checkGameEnd(board, queenNumber) == 1) {
if (checkResult(board) == 1) {
cout << "Yeah! Problem Solved." << endl;
} else {
cout << "Oops! Not enough queens." << endl;
}
break;
}
在main函数中,它显示了ABORT TRAP(6)。我在这个论坛上搜索了类似的帖子,大部分帖子都说ABORT TRAP与访问我们不拥有的内存有关。但我不知道我的代码的哪一部分正在访问无主内存。有人能找出问题吗?非常感谢。
要进入checkGameEnd == 1,请在游戏中键入I 8,K 7,J 10,L 9,B 17,A 15。
完整代码:
#include <iostream>
#include <iomanip>
using namespace std;
const int N = 18;
void printBoard(int [N][N], int);
int getAndProcessInput(int [N][N], int&, int&);
void flipBoard(int [N][N], int, int, int&);
int checkGameEnd(int [N][N], int);
int checkResult(int [N][N]);
int main(int argc, char *argv[]) {
int board[N][N] = {0};
int rowNumber, columnNumber, queenNumber = 0;
while (1) {
printBoard(board, queenNumber);
if (checkGameEnd(board, queenNumber) == 1) {
if (checkResult(board) == 1) {
cout << "Yeah! Problem Solved." << endl;
} else {
cout << "Oops! Not enough queens." << endl;
}
break;
}
while (getAndProcessInput(board, rowNumber, columnNumber) != 0){
cout << "Invalid. Try again!" << endl;
}
flipBoard(board, rowNumber, columnNumber, queenNumber);
}
}
void printBoard(int board[N][N], int queenNumber) {
cout << " ";
for (int i = 65; i < 65+N; i++) {
char columnLetter = i;
cout << columnLetter << " ";
}
cout << endl;
for (int j = 0; j < N; j++) {
cout << setw(2) << j << " ";
for (int k = 0; k < N; k++) {
if (board[j][k] == 0) {
cout << ". ";
} else if (board[j][k] == 1) {
cout << "= ";
} else if (board[j][k] == 2) {
cout << "Q ";
}
}
cout << endl;
}
cout << "No. of Queens = " << queenNumber << endl;
}
int getAndProcessInput (int board[N][N], int& rowNumber, int& columnNumber) {
char column;
cout << "Make a move (col row): ";
cin >> column >> rowNumber;
if (column < 'A' || column > 'Z') {
return 1;
}
columnNumber = (int)column - 65;
if (columnNumber < 0 || columnNumber >= N) {
return 1;
} else if (rowNumber < 0 || rowNumber >= N) {
return 1;
}
if (board[rowNumber][columnNumber] != 0) {
return 1;
}
return 0;
}
void flipBoard(int board[N][N], int rowNumber, int columnNumber, int& queenNumber) {
int a, b;
board[rowNumber][columnNumber] = 2;
//flip in north-east direction
a = rowNumber - 1;
b = columnNumber + 1;
while (a >= 0 && b < N) {
board[a][b] = 1;
a--;
b++;
}
//flip in east direction
a = rowNumber;
b = columnNumber + 1;
while (b < N) {
board[a][b] = 1;
b++;
}
//flip in south-east direction
a = rowNumber + 1;
b = columnNumber + 1;
while (a <= N && b < N) {
board[a][b] = 1;
a++;
b++;
}
//flip in south direction
a = rowNumber + 1;
b = columnNumber;
while (a < N) {
board[a][b] = 1;
a++;
}
//flip in south-west direction
a = rowNumber + 1;
b = columnNumber - 1;
while (a < N && b >= 0) {
board[a][b] = 1;
a++;
b--;
}
//flip in south-west direction
a = rowNumber;
b = columnNumber - 1;
while (b >= 0) {
board[a][b] = 1;
b--;
}
//flip in north-west direction
a = rowNumber -1 ;
b = columnNumber - 1;
while (a >= 0 && b >= 0) {
board[a][b] = 1;
a--;
b--;
}
//flip in north-west direction
a = rowNumber -1 ;
b = columnNumber;
while (a >= 0) {
board[a][b] = 1;
a--;
}
queenNumber++;
}
int checkGameEnd(int board[N][N], int queenNumber) {
if (queenNumber == N) {
return 1;
}
for (int i = 0; i < N; i++) {
int countEqual = 0;
for (int j = 0; j < N; j++) {
if (board[i][j] == 1) {
countEqual++;
}
}
if (countEqual == N) {
return 1;
}
}
return 0;
}
int checkResult(int board[N][N]) {
int checkQueenNumber[N] = {0};
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] == 2) {
checkQueenNumber[j] = 1;
break;
}
}
}
for (int k = 0; k < N; k++) {
if (checkQueenNumber[k] != 1) {
return 0;
}
}
return 1;
}
答案 0 :(得分:1)
你有一个错字:
while (a <= N && b < N) {
// ** <--- here
我使用gcc的address sanitizer找到了它。通常的嫌疑人gdb和valgrind几乎没用。
Asan和其他gcc消毒剂是精彩且被低估的工具,你应该学会使用它们(当然还有gdb和valgrind)。