Nagyon gyorsak vagytok:D Akkor a parancsok: (make clean) qmake -project qmake make (és ugye ez után jönnek a hibaüzenetek) és csatolom az állományokat és azért static, mert sok gombom van, de csak egy lehet kijelölt,aminek csak egy x és egy y koordinátája lehet és most nyúltam bele, a konstruktorban értéket adtam a _choosenX/Y-nak,és most más üzenet van: indexedbutton.cpp: In constructor 'IndexedButton::IndexedButton(int, int, QWidget*)': indexedbutton.cpp:6: error: 'int IndexedButton::_choosenX' is a static data member; it can only be initialized at its definition indexedbutton.cpp:6: error: 'int IndexedButton::_choosenY' is a static data member; it can only be initialized at its definition ami számomra azért érdekes,mert előtte van a 3. static member datanak érték adva,és azért nem szól
#include "indexedbutton.h" IndexedButton::IndexedButton(int x, int y, QWidget *parent) : QPushButton(parent), _x(x), _y(y), //_ballSmileIcon("smile.jpeg"), //_ballSadIcon("sad.jpeg"), _noBallIcon("noicon.jpeg"), _isolated(false) { connect(this,SIGNAL(clicked()),this,SLOT(clickedSlot())); _isChoosen = false; } int IndexedButton::getX() { return _x; } int IndexedButton::getY() { return _y; } bool IndexedButton::isIsolated() { return _isolated; } void IndexedButton::setIsolation(bool l) { _isolated = l; } void IndexedButton::setBallSmile() { setText("O"); }//setIcon(_ballSmileIcon); } void IndexedButton::setBallSad() { setText("I"); }//setIcon(_ballSadIcon); } void IndexedButton::setBallChoosen() { setText("C"); } void IndexedButton::putBall() { setBallSmile(); } void IndexedButton::pickBall() { setText(" "); }//setIcon(_noBallIcon); } bool IndexedButton::isBall() { return (text() == " " ? false : true); } void IndexedButton::setChoosenX(int x) { _choosenX = x; } void IndexedButton::setChoosenY(int y) { _choosenY = y; } int IndexedButton::getChoosenX() { return _choosenX; } int IndexedButton::getChoosenY() { return _choosenY; } void IndexedButton::setChoosen(bool l) { _isChoosen = l; } bool IndexedButton::getChoosen() { return _isChoosen; } void IndexedButton::clickedSlot() { if(isBall() == false) { if(_isChoosen){ emit moveChoosenBall(_x,_y); } } else { if(!_isChoosen) { setBallChoosen(); setChoosen(true); setChoosenX(_x); setChoosenY(_y); } else { emit moveChoose(_x,_y); } } }
#ifndef INDEXEDBUTTON_H #define INDEXEDBUTTON_H #include <QtGui> class IndexedButton : public QPushButton { Q_OBJECT public: IndexedButton(int x, int y, QWidget *parent=0); int getX(); int getY(); bool isIsolated(); void setIsolation(bool l); void setBallSmile(); void setBallSad(); void setBallChoosen(); void putBall(); void pickBall(); bool isBall(); static void setChoosenX(int x); static void setChoosenY(int y); static int getChoosenX(); static int getChoosenY(); static void setChoosen(bool l); static bool getChoosen(); private slots: void clickedSlot(); signals: void moveChoosenBall(int x, int y); void moveChoose(int x, int y); private: //static QIcon _ballSmileIcon; //static QIcon _ballSadIcon; //static QIcon _noBallIcon; static bool _isChoosen; static int _choosenX; static int _choosenY; int _x; int _y; bool _isolated; }; #endif
#include "jatekform.h" //#include "indexedbutton.h" /*ui void JatekForm::setupUi(QWidget *Form) { Form->setObjectName(QString::fromUtf8("Form")); verticalLayout = new QWidget(Form); verticalLayout->setObjectName(QString::fromUtf8("verticalLayout")); verticalLayout->setGeometry(QRect(0, 0, 301, 361)); vboxLayout = new QVBoxLayout(verticalLayout); vboxLayout->setSpacing(6); vboxLayout->setMargin(0); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); JatektablaGBox = new QGroupBox(verticalLayout); JatektablaGBox->setObjectName(QString::fromUtf8("JatektablaGBox")); JatektablaGBox->setAlignment(Qt::AlignHCenter); vboxLayout->addWidget(JatektablaGBox); hboxLayout = new QHBoxLayout(); hboxLayout->setSpacing(6); hboxLayout->setMargin(0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); JatekosNevLbl = new QLabel(verticalLayout); JatekosNevLbl->setObjectName(QString::fromUtf8("JatekosNevLbl")); hboxLayout->addWidget(JatekosNevLbl); lineEdit = new QLineEdit(verticalLayout); lineEdit->setObjectName(QString::fromUtf8("lineEdit")); lineEdit->setMaxLength(15); hboxLayout->addWidget(lineEdit); vboxLayout->addLayout(hboxLayout); hboxLayout1 = new QHBoxLayout(); hboxLayout1->setSpacing(6); hboxLayout1->setMargin(0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); Legjobb5Btn = new QPushButton(verticalLayout); Legjobb5Btn->setObjectName(QString::fromUtf8("Legjobb5Btn")); hboxLayout1->addWidget(Legjobb5Btn); UjJatekBtn = new QPushButton(verticalLayout); UjJatekBtn->setObjectName(QString::fromUtf8("UjJatekBtn")); hboxLayout1->addWidget(UjJatekBtn); vboxLayout->addLayout(hboxLayout1); retranslateUi(Form); QSize size(304, 362); size = size.expandedTo(Form->minimumSizeHint()); Form->resize(size); QMetaObject::connectSlotsByName(Form); } void JatekForm::retranslateUi(QWidget *Form) { Form->setWindowTitle(QApplication::translate("Form", "J\303\241t\303\251k", 0, QApplication::UnicodeUTF8)); JatektablaGBox->setTitle(QApplication::translate("Form", "J\303\241t\303\251kt\303\241bla", 0, QApplication::UnicodeUTF8)); JatekosNevLbl->setText(QApplication::translate("Form", "J\303\241t\303\251kos neve:", 0, QApplication::UnicodeUTF8)); Legjobb5Btn->setText(QApplication::translate("Form", "Legjobb 5", 0, QApplication::UnicodeUTF8)); UjJatekBtn->setText(QApplication::translate("Form", "\303\232j j\303\241t\303\251k", 0, QApplication::UnicodeUTF8)); Q_UNUSED(Form); } *///ui bool JatekForm::isOnTable(int x, int y) { if( (x>=0 && x<3) || (x>=6 && x<9) ) { if(y>=3 && y<6) return true; } else if(x>=3 && x<6) { if(y>=0 && y<9) return true; } return false; } bool JatekForm::isMovable(int fx, int fy, int tx, int ty) // megvizsgálja, { if( !isOnTable(tx,ty) ){ return false; } if( _table[fx][fy]->isIsolated() ){ return false; } if( !((fx-2==tx && fy==ty) || (fx==tx && fy-2==ty) || (fx+2==tx && fy==ty) || (fx==tx && fy+2==ty)) ) { return false; } return true; } void JatekForm::check(int x, int y) // ellenÅ?rzi, hogy az adott koordinátán milyen a labda "hangulata" { if(isOnTable(x,y)) { bool l=false; // van átugorható szomszéd? for(int i=-1; i<=1; i=i+3) { for(int j=-1; j<=1; j=j+3) { if(isOnTable(x,y)) { _table[x][y]->isBall() ? l=true : l=false; } } } if(!l) { _table[x][y]->setBallSad(); _table[x][y]->setIsolation(true); _isolated++; } // ha a labdának nincs átugorható szomszédja, // akkor szomorúra és magányosra állÃtjuk, és növeljük a magányosok számát } } void JatekForm::checkAll() // az egész táblán ellenÅ?rzi a labdák hangulatát { for(int i=0; i<3; i++) { for(int j=3; j<6; j++) { check(i,j); } } for(int i=3; i<6; i++) { for(int j=0; j<9; j++) { check(i,j); } } for(int i=6; i<9; i++) { for(int j=3; j<6; j++) { check(i,j); } } } void JatekForm::checkArea(int x, int y) // ellenÅ?rzi, hogy az adott koordináta körül okoz-e változást az elvétel { for(int i=-1; i<=1; i=i+3) { for(int j=-1; j<=1; j=j+3) { check(x+i,y+j); } } } void JatekForm::endOrNot() { if(_isolated == _ballsOnBoard) { QMessageBox *mb = new QMessageBox("!","Gratulálok " + lineEdit->text() + ", vége a játéknak! Mindössze " + _ballsOnBoard + " golyó maradt a táblán.", QMessageBox::Information, 1, 0, 0, this, 0); mb->setButtonText(1,"Ok"); mb->exec(); newGame(); } } void JatekForm::looseBall(int fx, int fy, int tx, int ty) { int lx = (fx+tx)/2; int ly = (fy+ty)/2; _table[lx][ly]->pickBall(); _ballsOnBoard--; check(lx,ly); } void JatekForm::moveChoosenBallSlot(int x, int y) { if( isMovable(_ibc->getChoosenX(),_ibc->getChoosenY(),x,y) ) // ha áthelyezhetÅ? a labda, akkor áthelyezés { _table[_ibc->getChoosenX()][_ibc->getChoosenY()]->pickBall(); // elvétel checkArea(_ibc->getChoosenX(),_ibc->getChoosenY()); _table[x][y]->putBall(); // lerakás check(x,y); looseBall(_ibc->getChoosenX(),_ibc->getChoosenY(),x,y); // átugrott labda elvétele _table[x][y]->setChoosen(false); endOrNot(); } else // ha nem helyezhetÅ? át a labda, akkor csak kijelölés áthelyezése { moveChooseSlot(x,y); } } void JatekForm::moveChooseSlot(int x, int y) { check(_ibc->getChoosenX(),_ibc->getChoosenY()); _table[x][y]->setChoosenX(x); _table[x][y]->setChoosenY(y); _table[x][y]->setBallChoosen(); } void JatekForm::newGameSlot() { newGame(); } void JatekForm::newGame() { setupTable(); _isolated=0; _ballsOnBoard=44; } void JatekForm::setupTable() { for(int i=0; i<3; i++) { for(int j=3; j<6; j++) { _table[i][j]->putBall(); _table[i][j]->setIsolation(false); } } for(int i=3; i<6; i++) { for(int j=0; j<9; j++) { _table[i][j]->putBall(); _table[i][j]->setIsolation(false); } } for(int i=6; i<9; i++) { for(int j=3; j<6; j++) { _table[i][j]->putBall(); _table[i][j]->setIsolation(false); } } _table[4][4]->pickBall(); // középsÅ? labda levétele } JatekForm::JatekForm(QWidget *parent) : QWidget(parent), _isolated(0), _ballsOnBoard(44) { setupUi(this); JatektablaGBox->setFixedSize(200,200); //_table = new IndexedButton*[9]; for(int i=0; i<9; i++) // tábla feltöltése üres pointerekkel { for(int j=0; j<9; j++) { _table[i][j] = 0; } } for(int i=0; i<3; i++) // táblán a kereszt felsÅ? részének feltöltése { for(int j=3; j<6; j++) { _table[i][j] = new IndexedButton(i,j,JatektablaGBox); connect(_table[i][j],SIGNAL(moveChoosenBall(int,int)), this,SLOT(moveChoosenBallSlot(int,int))); connect(_table[i][j],SIGNAL(moveChoose(int,int)),this,SLOT(moveChooseSlot(int,int))); _table[i][j]->putBall(); _table[i][j]->setIsolation(false); _table[i][j]->setGeometry(10+61+j*20,10+i*20,20,20); _table[i][j]->show(); } } for(int i=3; i<6; i++) // táblán a kereszt vÃzszintes részének feltöltése { for(int j=0; j<9; j++) { _table[i][j] = new IndexedButton(i,j,JatektablaGBox); connect(_table[i][j],SIGNAL(moveChoosenBall(int,int)), this,SLOT(moveChoosenBallSlot(int,int))); connect(_table[i][j],SIGNAL(moveChoose(int,int)),this,SLOT(moveChooseSlot(int,int))); _table[i][j]->putBall(); _table[i][j]->setIsolation(false); _table[i][j]->setGeometry(10+j*20,10+61+i*20,20,20); _table[i][j]->show(); } } for(int i=6; i<9; i++) // táblán a kereszt alsó részének feltöltése { for(int j=3; j<6; j++) { _table[i][j] = new IndexedButton(i,j,JatektablaGBox); connect(_table[i][j],SIGNAL(moveChoosenBall(int,int)), this,SLOT(moveChoosenBallSlot(int,int))); connect(_table[i][j],SIGNAL(moveChoose(int,int)),this,SLOT(moveChooseSlot(int,int))); _table[i][j]->putBall(); _table[i][j]->setIsolation(false); _table[i][j]->setGeometry(10+61+j*20,10+121+i*20,20,20); _table[i][j]->show(); } } _table[4][4]->pickBall(); // középsÅ? labda levétele _ibc = _table[0][0]; // indexedbutton-ra mutató pointer static memberfüggvények hÃvásához connect(UjJatekBtn,SIGNAL(clicked()),this,SLOT(newGameSlot())); //connect(Legjobb5Btn,SIGNAL(clicked()),this,SLOT(scoresSlot())); TODO }
#include <QWidget> /*ui #include <QtCore/QVariant> #include <QtGui/QAction> #include <QtGui/QApplication> #include <QtGui/QButtonGroup> #include <QtGui/QGroupBox> #include <QtGui/QHBoxLayout> #include <QtGui/QLabel> #include <QtGui/QLineEdit> #include <QtGui/QPushButton> #include <QtGui/QVBoxLayout> #include <QtGui/QWidget>*///ui #include "ui_jatekform.h" #include "indexedbutton.h" class JatekForm : public QWidget, private Ui_JatekForm { Q_OBJECT public: JatekForm(QWidget *parent = 0); /*ui QWidget *verticalLayout; QVBoxLayout *vboxLayout; QGroupBox *JatektablaGBox; QHBoxLayout *hboxLayout; QLabel *JatekosNevLbl; QLineEdit *lineEdit; QHBoxLayout *hboxLayout1; QPushButton *Legjobb5Btn; QPushButton *UjJatekBtn; void setupUi(QWidget *Form); void retranslateUi(QWidget *Form);*///ui private slots: void moveChoosenBallSlot(int x, int y); void moveChooseSlot(int x, int y); void newGameSlot(); private: bool isOnTable(int x, int y); bool isMovable(int fx, int fy, int tx, int ty); void check(int x, int y); void checkArea(int x, int y); void checkAll(); void looseBall(int fx, int fy, int tx, int ty); void endOrNot(); void setupTable(); void newGame(); IndexedButton *_table[9][9]; IndexedButton *_ibc; int _isolated; int _ballsOnBoard; };
#include <QApplication> #include "jatekform.h" int main (int argc, char *argv[]) { QApplication app(argc,argv); JatekForm *form = new JatekForm; form->show(); return app.exec(); }