#include
We use backtracking algorithm to fill it. bool fillGrid(){ int row,col; if(!findEmpty(row,col)) return true; int values9 = {1,2,3,4,5,6,7,8,9}; for (int itr = 0; itr < 9; ++itr){ int index = (rand()%(9-itr))+itr; int val = valuesindex; swap(valuesindex,valuesitr); if(!isInBox(row,col,val) && !isInRow(row,val) && !isInCol(col,val)){ gridrowcol = val; if(fillGrid()) return true; gridrowcol = 0; } } return false; } //Depending on the difficulty level, we erase a random number of elements in the grid for the user to solve. void hideSquares(){ int mapRandNum; switch(level){ case 1: mapRandNum = (rand() % 3) + 44; break; case 2: mapRandNum = (rand() % 3) + 50; break; case 3: mapRandNum = (rand() % 3) + 55; break; case 4: mapRandNum = (rand() % 3) + 60; break; default: mapRandNum = (rand() % 3) + 44; break; } for(int sqr = 0; sqr < mapRandNum;){ int x = genRandPos(); int y = genRandPos(); gridxy = 0; ++sqr; } } //This is to get a random position int genRandPos(){ return rand() % 9; } //This finds the empty cells in the grid, it is passed by reference to be used in another function bool findEmpty(int &row , int &col){ for(row = 0; row < 9; row++){ for(col = 0; col < 9; col++){ if(gridrowcol==0) return true; } } return false; } //This checks if the number that is being put in the grid already exists or not in the same square. bool isInBox(int row, int col, int value){ int tempRow, tempCol; if(gridrowcol == 0){ if(row >= 0 && row < 3) tempRow = 0; //If the row of the given number is between 0 and 3 (3 excluded), else if(row >= 3 && row < 6) tempRow = 3; //tempRow is initialized as 0, if it's between 3 and 6, tempRow is 3, else tempRow = 6; //else it's 6. This is to get the first cell in the square the number is in.
if(col >= 0 && col < 3) tempCol = 0; //The same happens to the column. else if(col >= 3 && col < 6) tempCol = 3; else tempCol = 6; for(int x = tempRow; x < tempRow+3; x++){ //2 for loops are implemented to check the 9 cells in the given square. for(int y = tempCol; y < tempCol+3; y++){ //If there is a number that is equal to the number that the user wants to put, if(gridxy == value) //the function returns true to show that the number will be repeated, return true; //else, it returns false to put the new number later on. } } return false; } } //This checks if the number that is being put in the grid already exists or not in the same row. bool isInRow(int row,int value){ for(int y = 0; y < 9; y++) { //The row is fixed and the loop changes only the columns to check the whole row.
if(gridrowy == value) return true; } return false; } //This checks if the number that is being put in the grid already exists or not in the same column. bool isInCol(int col,int value){ for(int x = 0; x < 9; x++) { //The column is fixed and the loop changes only the rows to check the whole column. if(gridxcol == value) return true; } return false; } //This prints the grid void printMap(){ cout< It is passed by reference.void swap(int *x, int *y){ int temp = *y; *y = *x; *x = temp;}int main(int argc, char const *argv){ srand(time(0)); sudokuMap ali = sudokuMap(2); ali.fillGrid(); ali.hideSquares(); ali.printMap(); ali.input(); return 0;}