Results 1 to 5 of 5

Thread: TicTacToe class that creates a board doesnt work properly

  1. #1
    Join Date
    Apr 2020
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default TicTacToe class that creates a board doesnt work properly

    So the thing is, that my function that is spupposed to dynamically create a board of empty buttons and then save each one of them in an array of buttons isnt working;
    To be precise, it is supposed to create 2 types of board: 3on3 and 20on20;
    3on3 is working but 20on20 isnt and i dont know why becouse the code is the same in both variables;

    Here is my code:

    TicTacToe.h

    Qt Code:
    1. #ifndef RYSUJ_PLANSZE_H
    2. #define RYSUJ_PLANSZE_H
    3.  
    4. #include <QWidget>
    5.  
    6. class TicTacToe :public QWidget
    7. {
    8. public:
    9. void create(); //input od gracza czy mapa 3na3 czy 5na5
    10. TicTacToe();
    11. int height; //rz?dy
    12. int width; //kolumny
    13. int winning_length;
    14. bool button_array[];
    15. };
    16.  
    17.  
    18.  
    19.  
    20.  
    21.  
    22.  
    23.  
    24.  
    25.  
    26. #endif // RYSUJ_PLANSZE_H
    To copy to clipboard, switch view to plain text mode 

    TicTacToe.cpp

    Qt Code:
    1. #include "TicTacToe.h"
    2. #include <QApplication>
    3. #include <QLabel>
    4. #include <QtWidgets>
    5. #include <QtCore>
    6. #include <QMessageBox>
    7. #include <QDialog>
    8. #include <QGridLayout>
    9.  
    10.  
    11.  
    12. QMessageBox::StandardButton reply;
    13.  
    14.  
    15.  
    16. TicTacToe::TicTacToe()
    17. {
    18. reply = QMessageBox::question(this, "Question", "If You want to play 3on3 map click 'Yes', "
    19. "if You want to play 5on5 map click 'No'");
    20. }
    21.  
    22. void TicTacToe::create()
    23. {
    24.  
    25. QWidget *window = new QWidget;
    26. QGridLayout *layout = new QGridLayout;
    27. window->setWindowTitle("Tic-Tac-Toe");
    28. //-------------------------------------------------------------------------------------------------------
    29.  
    30. if(reply==QMessageBox::Yes)
    31. {
    32. int w = 3;
    33. int h = 3;
    34.  
    35. TicTacToe::width = w;
    36. TicTacToe::height = h;
    37. TicTacToe::winning_length = 3;
    38.  
    39. for(int i = 0; i<w; i++)
    40. {
    41. for(int j = 0; j<h; j++)
    42. {
    43. QPushButton *button[i][j];
    44. button[i][j] = new QPushButton();
    45. layout->addWidget(button[i][j], i, j);
    46. button[i][j]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    47. button_array[i,j] = false;
    48.  
    49. }
    50. }
    51. }
    52. else
    53. {
    54. int w = 20;
    55. int h = 20;
    56.  
    57. TicTacToe::width = w;
    58. TicTacToe::height = h;
    59. TicTacToe::winning_length = 5;
    60.  
    61. for(int i = 0; i<h; i++)
    62. {
    63. for(int j = 0; j<w; j++)
    64. {
    65. QPushButton *button[i][j];
    66. button[i][j] = new QPushButton();
    67. layout->addWidget(button[i][j], i, j);
    68. button[i][j]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    69. button_array[i,j] = false;
    70. }
    71. }
    72. }
    73. //---------------------------------------------------------------------------------------------------
    74. window->setLayout(layout);
    75. window->show();
    76. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp

    Qt Code:
    1. #include "TicTacToe.h"
    2. #include <QApplication>
    3. #include <QLabel>
    4. #include <QtWidgets>
    5. #include <QtCore>
    6. #include <QMessageBox>
    7. #include <QDialog>
    8. #include <QGridLayout>
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication app(argc, argv);
    13. //--------------------------------------------------------------
    14.  
    15.  
    16. TicTacToe *R = new TicTacToe;
    17. R->create();
    18.  
    19.  
    20.  
    21. //--------------------------------------------------------------
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    And my question rn is --> What i going on? The error i get is --> The program has unexpectedly finished

  2. #2
    Join Date
    Jul 2008
    Posts
    439
    Thanks
    9
    Thanked 57 Times in 55 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TicTacToe class that creates a board doesnt work properly

    Well, try a debugger and check where it crashes.

    Ginsengelf

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,518
    Thanks
    252
    Thanked 721 Times in 711 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: TicTacToe class that creates a board doesnt work properly

    For one thing, TicTacToe is derived from QWidget, but it does not contain the Q_OBJECT macro in the class documentation.

    It would probably be a good idea to study some of the many Qt examples and tutorials. Your code is full of mistakes and very strange things which don't follow the usual Qt app development conventions.

    QPushButton *button[i][j];
    I have no idea what you think this is supposed to do (or mean). It declares a variable named "button" that is a two-dimensional array of pointers to QPushButton, where the first dimension has size "i" and the second has size "j". As soon as the loop over j exits, this array goes out of scope and is destroyed.

    You also try to assign values to button_array[i][j], but button_array is never initialized.

    To add to what Ginsengelf said, first learn some C++, and then learn how to use the debugger.
    <=== 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.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,713
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: TicTacToe class that creates a board doesnt work properly

    There are soooo many things wrong with this code...
    But what catches the eye the most is your for loop where you create the buttons.
    I am actually surprised the compiler is not complaining, I'd expect ' QPushButton *button[i][j];' to not be legal, since to my knowledge i and j in an array decleratio must be constants.
    Even if I am wrong, this line creates the first time an array of 0 x 0 elements.
    But you access the first element (element 0) - which should crash.
    Then, for each iteration of the loop you redefine the array with new dimensions.

    Ha, beat to it by d_tranz:-)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,518
    Thanks
    252
    Thanked 721 Times in 711 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: TicTacToe class that creates a board doesnt work properly

    Yes, I would be surprised if the button[i][j] statement compiles, but if this is the real code maybe it does. I always thought array dimensions had to be constants, too, but C++ is changing faster than I can keep up with.
    <=== 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.

Similar Threads

  1. QT_TR_NOOP doesnt work
    By vhptt in forum Newbie
    Replies: 1
    Last Post: 1st August 2012, 09:17
  2. Qwt Tex Text Doesnt work for me
    By gbmtoday in forum Qwt
    Replies: 1
    Last Post: 21st November 2010, 19:48
  3. connect doesnt work
    By john_god in forum Newbie
    Replies: 4
    Last Post: 23rd November 2008, 09:36
  4. ldapsearch doesnt work.
    By shamik in forum General Discussion
    Replies: 0
    Last Post: 3rd April 2008, 07:25
  5. How come this doesnt work?
    By ShaChris23 in forum Newbie
    Replies: 8
    Last Post: 16th June 2007, 04:43

Tags for this Thread

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.