Results 1 to 7 of 7

Thread: SUDOKU ( program crashes while trying to randomly generate Sudoku )

  1. #1
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Angry SUDOKU ( program crashes while trying to randomly generate Sudoku )

    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 ... Sometimes it works and when it does Sudoku is then spawned like this :

    sudokuWorking.PNG
    https://imgur.com/Ctf0046

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

    sudokuCrash.jpg
    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.

    Qt Code:
    1. void Igrica::randomTabela(){
    2.  
    3. int random_number;
    4. qDebug() << "size" << size;
    5. if(size == 4){
    6. qDebug() << "hi";
    7. for(int i=0;i<size;i++){
    8. for(int k=0;k<size;k++){
    9.  
    10. bool help = false;
    11. while(help == false){
    12. qDebug() << "um stuck here";
    13. random_number = (rand()%4)+1;
    14.  
    15. // checking if there is the same number as random_number in a row
    16. for(int j=0;j<size;j++){
    17. if(stiriRandom[j][k] == random_number){
    18. help = false;
    19. break;
    20. }else{
    21. help = true;
    22. }
    23. }
    24. // checking is there is another random_number in collumn
    25. if(help == true){
    26. for(int j=0;j<size;j++){
    27. if(stiriRandom[i][j] == random_number){
    28. help = false;
    29. break;
    30. }else{
    31. help = true;
    32. }
    33. }
    34. }
    35.  
    36. if(help == true)
    37. stiriRandom[i][k] = random_number;
    38. }
    39. }
    40. }
    41. }else if(size == 9){
    42. qDebug() << "hi";
    43. for(int i=0;i<size;i++){
    44. for(int k=0;k<size;k++){
    45.  
    46. bool help = false;
    47. while(help == false){
    48.  
    49. random_number = rand()%9+1;
    50.  
    51. for(int j=0;j<size;j++){
    52. if(devetRandom[j][k] == random_number){
    53. help = false;
    54. break;
    55. }else{
    56. help = true;
    57. }
    58. }
    59.  
    60. if(help == true){
    61. for(int j=0;j<size;j++){
    62. if(devetRandom[i][j] == random_number){
    63. help = false;
    64. break;
    65. }else{
    66. help = true;
    67. }
    68. }
    69. }
    70.  
    71. if(help == true)
    72. devetRandom[i][k] = random_number;
    73. }
    74. }
    75. }
    76. }else if(size == 12){
    77. for(int i=0;i<size;i++){
    78. for(int k=0;k<size;k++){
    79.  
    80. }
    81. }
    82. }
    83. }
    To copy to clipboard, switch view to plain text mode 

    Igrica header file

    Qt Code:
    1. #ifndef IGRICA_H
    2. #define IGRICA_H
    3.  
    4. #include <QGraphicsView>
    5. #include <QGraphicsScene>
    6. #include <QObject>
    7. #include <QMouseEvent>
    8. #include <QGraphicsSceneMouseEvent>
    9. #include <QGraphicsScene>
    10. #include <QWidget>
    11. #include <QKeyEvent>
    12.  
    13. #include "polje.h"
    14. #include "igralec.h"
    15.  
    16. class Igrica: public QGraphicsView{
    17. Q_OBJECT
    18. public:
    19. Igrica(QWidget *parent=NULL);
    20. Igralec *igralec;
    21. void meni();
    22. void zapolniTabelo();
    23. void izpisiTabelo();
    24. void randomTabela();
    25. void narisiOdebeljeneCrte();
    26. void naredimoFinishButton();
    27. Polje *stiri[4][4];
    28. Polje *devet[9][9];
    29. Polje *dvanajst[12][12];
    30. int stiriRandom[4][4];
    31. int devetRandom[9][9];
    32. int dvanajstRandom[12][12];
    33. public slots:
    34. void start();
    35. void meniFour();
    36. void meniNine();
    37. void meniTwelve();
    38. void preveriZmago();
    39.  
    40. private:
    41. int size;
    42. int level;
    43. };
    44.  
    45. #endif // IGRICA_H
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: SUDOKU ( program crashes while trying to randomly generate Sudoku )

    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    MongKong (19th April 2019)

  4. #3
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: SUDOKU ( program crashes while trying to randomly generate Sudoku )

    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.
    Last edited by MongKong; 19th April 2019 at 20:12.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: SUDOKU ( program crashes while trying to randomly generate Sudoku )

    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;"
    Last edited by d_stranz; 19th April 2019 at 22:10.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: SUDOKU ( program crashes while trying to randomly generate Sudoku )

    Quote Originally Posted by MongKong View Post
    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,
    _

  7. #6
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: SUDOKU ( program crashes while trying to randomly generate Sudoku )

    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 !

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: SUDOKU ( program crashes while trying to randomly generate Sudoku )

    You might find this article 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.
    Last edited by d_stranz; 21st April 2019 at 01:35.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. The following user says thank you to d_stranz for this useful post:

    MongKong (29th April 2019)

Similar Threads

  1. Sudoku front end
    By tom989 in forum Newbie
    Replies: 1
    Last Post: 20th August 2013, 23:49
  2. Program crashes on 64-bit systems
    By WereWind in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2013, 11:29
  3. Program crashes on OS X Lion
    By fyodor in forum Newbie
    Replies: 1
    Last Post: 31st October 2011, 07:18
  4. program crashing randomly code -1073741819
    By feraudyh in forum General Programming
    Replies: 6
    Last Post: 21st September 2010, 18:07
  5. Program crashes
    By Fallen_ in forum Qt Programming
    Replies: 49
    Last Post: 20th September 2010, 02:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.