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
- // 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[0][0]);
- // 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[0][0]); // print values of these table
- // end program
- return 0; // return integer
- }
- /*////////////////////////////////////////////////////////////////
- FUNCTIONS
- ////////////////////////////////////////////////////////////////*/
- // check all statements
- bool CheckEverything(int *table,int random,int column,int row){
- if (CheckRows(*table, random,column,row) && CheckColumn(*table, random,column,row) && CheckSquares(*table, random,column,row)) // if everything is true
- return true; // everything is true
- else // if not
- return false; // it's not
- }
- bool CheckRows(int *table, int random,int column, int row){ // Check unique values in a row
- for (int counter = 0; counter < row; counter++) {
- if (random == table[counter][row]) // if there were value like actually random number
- return false; // find new value
- else // if there weren't
- return true; // else it's correct
- }
- }
- bool CheckColumn(int *table, int random,int column, int row){ // Check unique values in a column
- for (int counter = 0; counter < column; counter++) { // if there were value like actually random number
- if (random == *table[row][counter]) // find new value
- return false; // find new value
- else // if there weren't
- return true; // else it's correct
- }
- }
- bool CheckSquares(int *table, int random, int column, int row) { // check unique values in 3x3 square
- return true;
- }
- void SetTo0(int *table)
- {
- 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)
- {
- 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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment