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
Code:
#ifndef RYSUJ_PLANSZE_H
#define RYSUJ_PLANSZE_H
#include <QWidget>
{
public:
void create(); //input od gracza czy mapa 3na3 czy 5na5
TicTacToe();
int height; //rz?dy
int width; //kolumny
int winning_length;
bool button_array[];
};
#endif // RYSUJ_PLANSZE_H
TicTacToe.cpp
Code:
#include "TicTacToe.h"
#include <QApplication>
#include <QLabel>
#include <QtWidgets>
#include <QtCore>
#include <QMessageBox>
#include <QDialog>
#include <QGridLayout>
TicTacToe::TicTacToe()
{
reply
= QMessageBox::question(this,
"Question",
"If You want to play 3on3 map click 'Yes', " "if You want to play 5on5 map click 'No'");
}
void TicTacToe::create()
{
window->setWindowTitle("Tic-Tac-Toe");
//-------------------------------------------------------------------------------------------------------
{
int w = 3;
int h = 3;
TicTacToe::width = w;
TicTacToe::height = h;
TicTacToe::winning_length = 3;
for(int i = 0; i<w; i++)
{
for(int j = 0; j<h; j++)
{
layout->addWidget(button[i][j], i, j);
button_array[i,j] = false;
}
}
}
else
{
int w = 20;
int h = 20;
TicTacToe::width = w;
TicTacToe::height = h;
TicTacToe::winning_length = 5;
for(int i = 0; i<h; i++)
{
for(int j = 0; j<w; j++)
{
layout->addWidget(button[i][j], i, j);
button_array[i,j] = false;
}
}
}
//---------------------------------------------------------------------------------------------------
window->setLayout(layout);
window->show();
}
main.cpp
Code:
#include "TicTacToe.h"
#include <QApplication>
#include <QLabel>
#include <QtWidgets>
#include <QtCore>
#include <QMessageBox>
#include <QDialog>
#include <QGridLayout>
int main(int argc, char *argv[])
{
//--------------------------------------------------------------
TicTacToe *R = new TicTacToe;
R->create();
//--------------------------------------------------------------
return app.exec();
}
And my question rn is --> What i going on? The error i get is --> The program has unexpectedly finished
Re: TicTacToe class that creates a board doesnt work properly
Well, try a debugger and check where it crashes.
Ginsengelf
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.
Quote:
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.
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:-)
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.