I am writing a Sudoky program to practice C++. I have a Sudoku class with a populateGrid method that populates the Sudoku grid with appropriate values. The sudoku grid is a 2d array of Cells (Cell being another class I made). The Cell class has a method called setNumber, to set the number of that particular cell.
Here is the populateGrid method in the Sudoku class:
void Sudoku::populateGrid()
{
unsigned int i, j;
for (i = 0; i < this->rows; i++)
{
for (j = 0; j < this->cols; j++)
{
std::vector<int> validValues = this->findValidValues(i, j);
int proposedValue = validValues.at(rand() % validValues.size());
this->grid[i][j].setNumber(proposedValue);
}
}
}
Note that the setNumber method is being called at the end of the inner for loop.
This is the code of setNumber in the Cell class:
void Cell::setNumber(int n)
{
if (n <= 0 || n > 9)
{
std::cout << "Error: Invalid Argument" << std::endl;
}
this->number = n;
}
The program simply stops execution when tryng to run this->number = n;
in the setNumber method. It doesn't show any errors during compiling, nor runtime. If I sorround that line with a try-catch block, I am not catching any exceptions.
I honestly don't know what the issue might be. I would be very thankful if anyone had an idea. Please let me know if you need more details on the program.
Thanks in advance!
EDIT:
this is how the grid is being initialised:
Sudoku::Sudoku(int rows, int cols)
{
this->setRows(rows);
this->setCols(cols);
this->grid = new Cell *[this->rows];
unsigned int i, j;
for (i = 0; i < this->rows; i++)
{
this->grid[i] = new Cell[this->cols];
for (j = 0; j < this->cols; j++)
{
this->grid[i][j] = Cell();
}
}
this->populateGrid();
this->hideCells();
}
The type of the grid is Cell**
. As mentioned previously, the program stops running when executing this->populateGrid()
.
Aucun commentaire:
Enregistrer un commentaire