所以,我不断收到一条错误消息:ProgCW.exe中_______处的未处理异常:Microsoft C ++异常:内存位置的std :: out_of_range ________。
我在网上发现了这个代码,并且实现了它,但是我的实现似乎有问题。
我用过一堂课来做这件事。
这是代码:
dispGrid::dispGrid(const int gRows, const int gCols){
//declaration and allocation for creating a grid and cardstatus
std::vector<std::vector<int>> cGrid;
int** cardStatus = new int*[gRows];
for (int i = 0; i < gRows; i++) {
cardStatus[i] = new int[gCols];
}
//initialisation of variables
int r1, c1, r2, c2;
int moves;
bool gameOver = false;
cardShuffle(cGrid, gRows, gCols);
while (gameOver != true) { //allows to repeat over the game
moves = 0;
///////////////////////////////
// prints grid //
/////////////////////////////
//prints the coordinates
std::cout << "\t ";
for (int nCols = 0; nCols < gCols; nCols++) {
std::cout << nCols + 1 << " ";
}
std::cout << std::endl;
//initialisation
//displays the starred grid according to difficulty level the user chose and sets cardstatus as false, for face down
for (int nRows = 0; nRows < gRows; nRows++) {
std::cout << "\t" << nRows + 1 << " | ";
for (int nCols = 0; nCols < gCols; nCols++) {
std::cout << "* ";
cardStatus[nRows][nCols] = false;
}
std::cout << std::endl;
}
/******************************/
/* game mechanics */
/*****************************/
do { //allows execution of thr input as long the card status is not false at that card
//user input for the first card's coordinates
std::cout << "Enter the first cards row: " << std::endl;
std::cin >> r1;
std::cout << "Enter the first cards column: " << std::endl;
std::cin >> c1;
if ((r1 && c1) == 0) { //checks to see whether user has input the values 0
std::cout << "#NOTE : There are no coordinates of that value." << std::endl;
}
} while (cardStatus[r1 - 1][c1 - 1] != false);
do { //allows execution of thr input as long the card status is not false at that card
//user input for the second card's coordinates
std::cout << "Enter the second cards row: " << std::endl;
std::cin >> r2;
std::cout << "Enter the second cards column: " << std::endl;
std::cin >> c2;
if ((r2 && c2) == 0) {//checks to see whether user has input the values 0
std::cout << "#NOTE : There are no coordinates of that value." << std::endl;
}
else if (cardStatus[r2 - 1][c2 - 1] = true) { //if the cardstatus of the second card is true then output the care is already flipped
std::cout << "#NOTE : This card is already flipped." << std::endl;
}
else if ((r2 == r1) && (c2 == c1)) { //if the cards are the same then the card is already flipped
std::cout << "#NOTE : You have already chosen this card." << std::endl;
continue;
}
} while (cardStatus[r2 - 1][c2 - 1] != false);
r1--;
c1--;
r2--;
c2--;
//reaveal cards
//prints the coordinates
std::cout << "\t ";
for (int nCols = 0; nCols < gCols; nCols++) {
std::cout << nCols + 1 << " ";
}
std::cout << std::endl;
//initialisation
//displays grid with blank spaces according to the coordinates then picked, setting up to replace blank spaces for values
for (int nRows = 0; nRows < gRows; nRows++) {
std::cout << "\t" << nRows + 1 << " | ";
for (int nCols = 0; nCols < gCols; nCols++) {
if ((nRows == r1) & (nCols == c1)) {
std::cout << cGrid[nRows][nCols] << " ";
}
else if ((nRows == r2) && (nCols == c2)) {
std::cout << cGrid[nRows][nCols] << " ";
}
else if (cardStatus[nRows][nCols] = true) {
std::cout << cGrid[nRows][nCols] << " ";
}
else {
std::cout << "* ";
}
}
std::cout << std::endl;
}
//if match?
if (cGrid[r1][c1] == cGrid[r2][c2]) {
std::cout << "#NOTE : Cards Match!" << std::endl;
cardStatus[r1][c1] = true;
cardStatus[r2][c2] = true;
}
std::cin.get();
std::cin.get();
/*********************************************************/
/* prints to blank screen */
/*********************************************************/
for (int z = 0; z <= 30; z++) {
std::cout << std::endl;
}
//reprint the board
//prints the coordinates
std::cout << "\t ";
for (int nCols = 0; nCols < gCols; nCols++) {
std::cout << nCols + 1 << " ";
}
std::cout << std::endl;
//initialisation
//displays the starred grid according to difficulty level the user chose and sets cardstatus as false, for face down
for (int nRows = 0; nRows < gRows; nRows++) {
std::cout << "\t" << nRows + 1 << " | ";
for (int nCols = 0; nCols < gCols; nCols++) {
if (cardStatus[nRows][nCols] = true) {
std::cout << cGrid[nRows][nCols] << " ";
}
else {
std::cout << "* ";
}
}
std::cout << std::endl;
}
gameOver = true;
//checks all card status, should all be set to true
for (int nRows = 0; nRows < gRows; nRows++) {
for (int nCols = 0; nCols < gCols; nCols++) {
if (cardStatus[nRows][nCols] == false) {
gameOver = false;
break;
}
}
if (gameOver == false) {
break;
}
}
moves++; //counts moves
}
std::cout << "#NOTE : You have matched all the tiles" << std::endl;
std::cout << "#NOTE: You had " << moves << "moves." << std::endl << std::endl; }
问题似乎是需要洗牌的时候。
洗牌的功能:
void dispGrid::cardShuffle(std::vector<std::vector<int>> &gCards, int rows, int cols)//parameters of a 2d vector and the number of rows and columns {
int numOfElem = rows*cols;//total number of random number should be made
std::vector<int> randNum;
//generate same random numbers
for (int x = 0; x < (numOfElem / 2); x++) {
const int fNum = rand() % 100 + 1; //generate num between 1 and 100
int sNum;
do {
sNum = rand() % 100 + 1;
} while (fNum == sNum);
randNum.push_back(fNum);
randNum.push_back(sNum);
}
for (int s = 0; s <= 20; s++) {
for (int x = 0; x < numOfElem; x++) {
srand((unsigned)time(NULL));//initialise random seed
int i = rand() % (numOfElem - 1) + 1; //chooses a number between the length of the array
int temp = randNum.at(x); //sets temp as the value in the at that indes in the start array
randNum.at(x) = randNum.at(i); //sets the value at that index in the start array as the value of the index at the start array
randNum.at(i) = temp;
}
}
int i = 0;
for (int nRows = 0; nRows < rows; nRows++) {// for every row and column
for (int nCols = 0; nCols < cols; nCols++) {
gCards.at(nRows).at(nCols) = randNum.at(i);//card at that coordinate will be equal to
std::cout << gCards[nRows][nCols];
i = i + 1;
}
std::cout << std::endl;
}}
我正在使用Microsoft Visual Studio,当我把断点放入时,问题就出现了:
gCards.at(nRows).at(nCols) = randNum.at(i); //card at that coordinate will be equal to
非常感谢你的帮助。
答案 0 :(得分:0)
<=
通常不是逻辑错误:
for (int s = 0; s <= 20; s++)