Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- randomly generated SUDOKU (unique numbers in 3x3 squares, in every row, and every column)
- in C++, created by redve as test of his skills, BD
- */
- // includes
- #include <iostream> // standard library, here it's just to print values
- #include <cstdlib> // randomize numbers
- #include <time.h> // RNG source
- // set namespace
- using namespace std; // namespace std
- bool CheckEverything(int** table, int random, int column, int row); // Check is everything OK
- bool CheckRows(int** table, int random, int column, int row); // Check unique values in a row
- bool CheckColumn(int** table, int random, int column, int row); // Check unique values in a column
- bool CheckSquares(int** table, int random, int column, int row); // check unique values in 3x3 square
- void ReturnValues(int** table); // print values
- void SetTo0(int** table); // set table to 0
- int SquareDetection(int x, int y); // detect square
- // main function
- int main()
- {
- int random = 0; // bufor do losowania liczb przed wstawieniem ich do tabeli
- bool stop; // variable to check
- int Counter = 0; // Counter of critical errors
- // declare 2D table
- int** table = new int* [9]; // create pointer to pointers
- // create table
- for (int i = 0; i < 9; i++) // create pointers
- table[i] = new int[9]; // make them pointers to ints
- // initiate RNG
- srand(time(NULL)); // start RNG
- // set table to 0
- SetTo0(table);
- // get a random numbers
- for (int row = 0; row < 9; row++) { // go through rows
- for (int column = 0; column < 9; column++) { // go through colums
- do {
- random = rand() % 9 + 1; // random number to next table slot
- stop = true;
- stop = CheckEverything(table, random, column, row); // Check if number repeats in current row, column, and square 3x3
- Counter++; // increment every loop cycle
- if (Counter >= 1000) { // if there were 1000 cycles without correct number
- Counter = 0; // try fill new, clear table
- column = 0; // set column to 0
- row = 0; // set row to 0
- break; // and break current cycle
- }
- } while (!stop); // if CheckEverything is correct it returns TRUE, so try opposite argument
- table[row][column] = random; // if everything is correct, fill these table slot
- }
- }
- // return values
- ReturnValues(table); // print values of these table
- // exit program
- return 0; // return integer
- }
- /*////////////////////////////////////////////////////////////////
- FUNCTIONS
- ////////////////////////////////////////////////////////////////*/
- bool CheckRows(int** table, int random, int column, int row) { // Check unique values in a row
- //cout << "================" << endl << "wartosc tabeli komorki: " << table[row][column] << endl << "aktualnie rozpatrywana liczba: " << random << endl << "aktualna kolumna: " << column << endl << "aktualny rzad: "<< row << endl << "==============" << endl;
- for (int i = 0; i < row; i++)
- if (random == table[i][column]) // if there were value like actually random number
- return false; // find new value
- return true; // default its true
- }
- bool CheckColumn(int** table, int random, int column, int row) { // Check unique values in a column
- for (int i = 0; i < column; i++)
- if (random == table[row][i]) // if there were value like actually random number
- return false; // find new value
- return true; // default it's true
- }
- bool CheckSquares(int** table, int random, int column, int row) { // check unique values in 3x3 square
- int square = SquareDetection(row, column); // which field 3x3 it is
- int** square_values = new int* [9]; // create new 9 pointers table
- for (int i = 0; i < 9; i++) // create 9x9 table
- square_values[i] = new int[9];
- square_values[square][(column % 3 + row % 3) % 9] = random; // fill them with current random number
- for (int i = 0; i < (column % 3 + row % 3) % 9; i++)
- if (random = square_values[square][i]) // if there is same number in square 3x3
- return false; // it's false
- return true; // default it's true
- }
- void SetTo0(int** table) { // set default values to 0
- for (int i = 0; i < 9; i++) // rows
- for (int j = 0; j < 9; j++) // columns
- table[i][j] = 0; // set values to 0
- }
- void ReturnValues(int** table) { // print values
- for (int row = 0; row < 9; row++) { // rows
- for (int column = 0; column < 9; column++) // column
- cout << table[row][column] << " "; // print values
- cout << endl; // go to the next row
- }
- }
- bool CheckEverything(int** table, int random, int column, int row) { // check all statements
- if (CheckRows(table, random, column, row) && CheckColumn(table, random, column, row) && CheckSquares(table, random, column, row))
- return true; // everything is true
- else // if not
- return false; // it's not
- }
- int SquareDetection(int x, int y) {
- int square = 0;
- if ((x >= 0 && x <= 2) && (y >= 0 && y <= 2)) {
- square = 0;
- }
- if ((x >= 3 && x <= 5) && (y >= 0 && y <= 2)) {
- square = 3;
- }
- if ((x >= 6 && x <= 8) && (y >= 0 && y <= 2)) {
- square = 6;
- }
- if ((x >= 0 && x <= 2) && (y >= 3 && y <= 5)) {
- square = 1;
- }
- if ((x >= 3 && x <= 5) && (y >= 3 && y <= 5)) {
- square = 4;
- }
- if ((x >= 6 && x <= 8) && (y >= 3 && y <= 5)) {
- square = 7;
- }
- if ((x >= 0 && x <= 2) && (y >= 6 && y <= 8)) {
- square = 2;
- }
- if ((x >= 3 && x <= 5) && (y >= 6 && y <= 8)) {
- square = 5;
- }
- if ((x >= 6 && x <= 8) && (y >= 6 && y <= 8)) {
- square = 8;
- }
- return square;
- }
Advertisement
Add Comment
Please, Sign In to add comment