-3

したがって、次のようなエラーが表示されます。ProgCW.exeの____で未処理の例外:メモリの場所________のstd :: out_of_range。C++でメモリゲームを実装 - メモリ位置エラーで範囲外になる

私はこのコードをオンラインで見つけて実装しましたが、私の実装には何か問題があるようです。

私はこれを行うためにクラスを使用しました。

これはコードです:

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; 
}} 

私はこの問題は、ポイントに現れた、マイクロソフトのVisual Studioを使用して、そして私はブレークポイントを入れています:

gCards.at(nRows).at(nCols) = randNum.at(i); //card at that coordinate will be equal to 

多くのおかげで助けを求めてこのようなループで

+0

ベクトルが空であること。 – molbdnilo

+0

gCardsベクター? – DVilela

答えて

0

<=は少なからず論理エラーです:

for (int s = 0; s <= 20; s++) 
+0

だから私は試してみることができます: (int s = 0; s <20; s ++) – DVilela

関連する問題