PDA

View Full Version : SUDOKU ( program crashes while trying to randomly generate Sudoku )



MongKong
19th April 2019, 16:32
As the title says, i'm trying to randomly generate Sudoku (in this example 4x4) and i know how to do it, kinda, i believe it should work, but for some reason my program crashes when i run it :p ... Sometimes it works and when it does Sudoku is then spawned like this :

13085
https://imgur.com/Ctf0046

But when it doesn't work, which is like 70% of the time, then it crashes and its like this :

13086
https://imgur.com/2ksNiMQ

I put qDebug() in my while loop and i realized that my loop keeps repeating itself and idk why....
In Igrica.h file i defined stiriRandom as int table of [4][4]. When i press 4x4 button, size is set to 4.



void Igrica::randomTabela(){

int random_number;
qDebug() << "size" << size;
if(size == 4){
qDebug() << "hi";
for(int i=0;i<size;i++){
for(int k=0;k<size;k++){

bool help = false;
while(help == false){
qDebug() << "um stuck here";
random_number = (rand()%4)+1;

// checking if there is the same number as random_number in a row
for(int j=0;j<size;j++){
if(stiriRandom[j][k] == random_number){
help = false;
break;
}else{
help = true;
}
}
// checking is there is another random_number in collumn
if(help == true){
for(int j=0;j<size;j++){
if(stiriRandom[i][j] == random_number){
help = false;
break;
}else{
help = true;
}
}
}

if(help == true)
stiriRandom[i][k] = random_number;
}
}
}
}else if(size == 9){
qDebug() << "hi";
for(int i=0;i<size;i++){
for(int k=0;k<size;k++){

bool help = false;
while(help == false){

random_number = rand()%9+1;

for(int j=0;j<size;j++){
if(devetRandom[j][k] == random_number){
help = false;
break;
}else{
help = true;
}
}

if(help == true){
for(int j=0;j<size;j++){
if(devetRandom[i][j] == random_number){
help = false;
break;
}else{
help = true;
}
}
}

if(help == true)
devetRandom[i][k] = random_number;
}
}
}
}else if(size == 12){
for(int i=0;i<size;i++){
for(int k=0;k<size;k++){

}
}
}
}


Igrica header file



#ifndef IGRICA_H
#define IGRICA_H

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QObject>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsScene>
#include <QWidget>
#include <QKeyEvent>

#include "polje.h"
#include "igralec.h"

class Igrica: public QGraphicsView{
Q_OBJECT
public:
Igrica(QWidget *parent=NULL);
QGraphicsScene *scene;
Igralec *igralec;
void meni();
void zapolniTabelo();
void izpisiTabelo();
void randomTabela();
void narisiOdebeljeneCrte();
void naredimoFinishButton();
Polje *stiri[4][4];
Polje *devet[9][9];
Polje *dvanajst[12][12];
int stiriRandom[4][4];
int devetRandom[9][9];
int dvanajstRandom[12][12];
public slots:
void start();
void meniFour();
void meniNine();
void meniTwelve();
void preveriZmago();

private:
int size;
int level;
};

#endif // IGRICA_H


Any help is appreciated !!!!!!!!!!!!!!!!!!!!!!

anda_skoa
19th April 2019, 18:22
When you run a debug build of the program it should tell you where it crashed, i.e. you should get a so-called "stack trace".

Cheers,
_

MongKong
19th April 2019, 18:57
Hi !
Yes, i tried doing that and the program still stays the same as shown in the first picture plus i cant close it (unless i press the Run button or i close it via Task Manager, which also crashes my qt... )and my debugger doesn't show anything.
And the loop keeps repeating itself i believe, it's still writting in the console the qDebug() i put in the while loop.

d_stranz
19th April 2019, 21:00
When a program runs in debug mode but crashes in release mode that is a good sign that you are using a variable that hasn't been initialized. In most debuggers, variables that do not have values assigned at compile time get initialized to known, "strange" values. When you see one of these in the debugger, you can tell right away that you forgot to assign an initial value. In release mode, this does not happen. Uninitialized variables have whatever value the memory has when the program starts - something completely random. As a result, you get random behavior. If the variable happens to be a pointer, you get a crash. If it happens to be a loop terminating value, then either the loop doesn't run at all, or it runs for some random number of iterations.

Instead of repeatedly posting questions here about how your programs keep crashing and asking us to figure it out, you would become a much better programmer if you learned how to use the debugger effectively to step through and debug your own programs.

For example, if you have a loop that keeps repeating, use the debugger to set a breakpoint just before the start of the loop, then run the program. It will stop when it gets to the breakpoint, and then you can use the debugging features to step through the loop line-by-line. At any point, you can look at, say, the loop counter variable, to see if it has the value you expect for each pass through the loop. If the loop continues past where you think it should have stopped, then the value of the loop terminating variable will probably tell you why.

By the way, in your randomTabels() function, it looks like you do the exact same thing when size = 4, 9, or 12, but you have copied the same code 3 times and the only difference is in the random number you generate. So instead of writing "random_number = (rand()%4)+1;", just write "random_number = (rand()% size)+1;" and get rid of the "if ( size == 4 )", "else if ( size == 9 )" repetitions of identical code.

Or write yourself a little "int randomValue( int size )" function that returns "( rand() % size ) + 1;"

anda_skoa
20th April 2019, 11:19
And the loop keeps repeating itself i believe, it's still writting in the console the qDebug() i put in the while loop.

In which case a fixed text like "um stuck here" won't help you.

If a loop keeps repeating more often than you expect or even indefinitely then it simply doesn't reach its end condition when you expect it would.
So you need to debug why it doesn't.

For example add the value of the random number to the output and see if it stays the same.
And probably the values you compare with.

Cheers,
_

MongKong
20th April 2019, 17:15
d_stranz and anda_skoa thank you for your comments and after using qDebug() for a while i realized that my table is getting saved like this for example:

3 1 4 2
4 2 1 3
2 3

and then it gets stuck... cause program cant put any of numbers from 1 to 4 in there .......
Right now i have for loops for checking collumns, rows and i just added for checking in box (2x2)
Thought it would've been simple -.-, silly me.
I don't know anymore what else i should be checking, was looking on google and i didn't find anything useful aswell, so i'll probably just not make random Sudokus :3

Thank you for your help tho guys,
Really appreciated !

d_stranz
21st April 2019, 00:28
You might find this article (https://medium.com/@rossharrison/generating-sudoku-boards-pt-1-structures-algorithms-a1e62feeb32) interesting and helpful.

As you found, it is easy for a random generator to get stuck, and it gets harder as the size increases. You need to be able to satisfy three constraints simultaneously - row, column, and square - so you can use a random generator, but you also have to be able to backtrack and remove the last step (or N steps) if you find you get stuck.