MongKong

19th April 2019, 17: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 !!!!!!!!!!!!!!!!!!!!!!

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 !!!!!!!!!!!!!!!!!!!!!!