houcem001 | salut
je suis entrain de programmer le tour de hanoi mais j'ai un probleme au deplacement automatique des disques. je souhaite que les disques se deplace un apres l'autre et non un bloc de disques.
voila les fichiers que je vous ai envoyer "main.cpp" , "MaFenetre.h" et "MaFenetre.cpp".
s'il vous plait compilez ce code et aidez moi à trouver le truc qui cloche.
merci beaucoup
main.cpp:
Code :
- #include<QApplication>
- #include <QTranslator>
- #include <QLocale>
- #include <QLibraryInfo>
- #include"MaFenetre.h"
- int main(int argc,char *argv[])
- {
- QApplication app(argc,argv);
- MaFenetre fenetre;
- fenetre.show();
- fenetre.afficherDisques();
- return app.exec();
- }
|
MaFenetre.h :
Code :
- #ifndef DEF_MAFENETRE
- #define DEF_MAFENETRE
- #include <QApplication>
- #include <QWidget>
- #include <QpaintDevice>
- #include <QPushButton>
- #include <QInputDialog>
- #include <QMessageBox>
- #include <QPainter>
- #include <QRectF>
- #include <QGraphicsRectItem>
- #include <QpaintEngine>
- #include <QPaintEvent>
- #include <QVector>
- #include <QDialog>
- #include <QPointF>
- #include <QRegion>
- #include <QColor>
- #include <QMessageBox>
- #include <QTimer>
- #include <math.h>
- #include <QLCDNumber>
- #include <QLabel>
- class MaFenetre : public QWidget
- {
- Q_OBJECT
- public:
- MaFenetre();
- void afficherDisques();
- void deplacer(int,int);
- void hanoi(int,int,int,int);
- void solution();
- void han();
- void hann();
- public slots:
- void ouvrirDialogue();
- void deplacer111();
- void deplacerAB();
- void deplacerAC();
- void deplacerBA();
- void deplacerBC();
- void deplacerCA();
- void deplacerCB();
- void sol();
- void paintEvent(QPaintEvent*);
- private:
- QPushButton *m_boutonDialogue;
- QPushButton *m_solution;
- QPushButton *m_suivant;
- QPushButton *m_AB;
- QPushButton *m_AC;
- QPushButton *m_BA;
- QPushButton *m_BC;
- QPushButton *m_CA;
- QPushButton *m_CB;
- QPushButton *bouton;
- int m_nbdisques;
- int m_A;
- int m_B;
- int m_C;
- int m_axA[10];
- int m_axB[10];
- int m_axC[10];
- QRectF disque[10];
- int axe1;
- int axe2;
- int axe3;
- int *tableau;
- QTimer *timer;
- int comp;
- int cmp;
- int n;
- };
- #endif
|
MaFenetre.cpp :
Code :
- #include "MaFenetre.h"
- MaFenetre::MaFenetre() : QWidget() {
- m_nbdisques=0;
- setFixedSize(1000, 600);
- m_boutonDialogue = new QPushButton("DEBUT", this);
- m_AB = new QPushButton("vers B", this);
- m_AC = new QPushButton("vers C", this);
- m_BA = new QPushButton("vers A", this);
- m_BC = new QPushButton("vers C", this);
- m_CA = new QPushButton("vers A", this);
- m_CB = new QPushButton("vers B", this);
- m_solution= new QPushButton("solution",this);
- m_solution->move(150,50);
- m_AB->move(165,500);
- m_AC->move(165,530);
- m_BA->move(465,500);
- m_BC->move(465,530);
- m_CA->move(765,500);
- m_CB->move(765,530);
- m_boutonDialogue->move(40, 50);
- for(int i=0;i<10;i++)
- {m_axA[i]=i;
- m_axB[i]=10;
- m_axC[i]=10;
- }
- QObject::connect(m_boutonDialogue, SIGNAL(clicked()), this, SLOT(ouvrirDialogue()));
- QObject::connect(m_AB, SIGNAL(clicked()), this, SLOT(deplacerAB()));
- QObject::connect(m_AC, SIGNAL(clicked()), this, SLOT(deplacerAC()));
- QObject::connect(m_BA, SIGNAL(clicked()), this, SLOT(deplacerBA()));
- QObject::connect(m_BC, SIGNAL(clicked()), this, SLOT(deplacerBC()));
- QObject::connect(m_CA, SIGNAL(clicked()), this, SLOT(deplacerCA()));
- QObject::connect(m_CB, SIGNAL(clicked()), this, SLOT(deplacerCB()));
- QObject::connect(m_solution, SIGNAL(clicked()), this, SLOT(deplacer111()));
- comp=0;
- cmp=0;
- axe1=0;
- axe2=0;
- axe3=0;
- tableau = new int[2046];
- for(int j=0;j<2046;j++)
- tableau[j]=0;
- QLabel *label = new QLabel("Salut", this);
- label->move(30, 20);
- }
- void MaFenetre::ouvrirDialogue() {
- m_nbdisques = QInputDialog::getInteger(this, "Nombres de disques", "Entrez le nombre de disques",5,1,10);
- m_A = m_nbdisques;
- m_B=0;
- m_C=0;
- }
- void MaFenetre::afficherDisques()
- {
- for(int j=0;j<10;j++)
- disque[j] =QRectF(50+10*j,480-20*j,300-20*j,20);
- update();
- }
- void MaFenetre::deplacerAB()
- {
- if(m_A!=0)
- {
- if(m_B==0)
- { m_axB[0]=m_axA[m_A-1];
- QPointF po(500,490);
- disque[m_axA[m_A-1]].moveCenter(po);
- m_A--;
- m_B++;
- }
- else if(m_axB[m_B-1]<m_axA[m_A-1])
- { m_axB[m_B]=m_axA[m_A-1];
- QPointF pt(500,490-m_B*20);
- disque[m_axB[m_B]].moveCenter(pt);
- m_A--;
- m_B++;
- }
- else QMessageBox::critical(this,"tour de hanoii","deplacement non autorise" );
- }
- update();
- }
- void MaFenetre::deplacerAC()
- {
- if(m_A!=0)
- {
- if(m_C==0)
- { m_axC[0]=m_axA[m_A-1];
- QPointF po(800,490);
- disque[m_axA[m_A-1]].moveCenter(po);
- m_A--;
- m_C++;
- }
- else if(m_axC[m_C-1]<m_axA[m_A-1])
- {
- m_axC[m_C]=m_axA[m_A-1];
- QPointF pt(800,490-m_C*20);
- disque[m_axC[m_C]].moveCenter(pt);
- m_A--;
- m_C++;
- }
- else QMessageBox::critical(this,"tour de hanoii","deplacement non autorise" );
- }
- update();
- }
- void MaFenetre::deplacerBA()
- {
- if(m_B!=0)
- {
- if(m_A==0)
- { m_axA[0]=m_axB[m_B-1];
- QPointF po(200,490);
- disque[m_axB[m_B-1]].moveCenter(po);
- m_B--;
- m_A++;
- }
- else if(m_axA[m_A-1]<m_axB[m_B-1])
- { m_axA[m_A]=m_axB[m_B-1];
- QPointF pt(200,490-m_A*20);
- disque[m_axA[m_A]].moveCenter(pt);
- m_B--;
- m_A++;
- }
- else QMessageBox::critical(this,"tour de hanoii","deplacement non autorise" );
- }
- update();
- }
- void MaFenetre::deplacerBC()
- {
- if(m_B!=0)
- {
- if(m_C==0)
- { m_axC[0]=m_axB[m_B-1];
- QPointF po(800,490);
- disque[m_axB[m_B-1]].moveCenter(po);
- m_B--;
- m_C++;
- }
- else if(m_axC[m_C-1]<m_axB[m_B-1])
- { m_axC[m_C]=m_axB[m_B-1];
- QPointF pt(800,490-m_C*20);
- disque[m_axC[m_C]].moveCenter(pt);
- m_B--;
- m_C++;
- }
- else QMessageBox::critical(this,"tour de hanoii","deplacement non autorise" );
- }
- update();
- }
- void MaFenetre::deplacerCA()
- {
- if(m_C!=0)
- {
- if(m_A==0)
- { m_axA[0]=m_axC[m_C-1];
- QPointF po(200,490);
- disque[m_axC[m_C-1]].moveCenter(po);
- m_C--;
- m_A++;
- }
- else if(m_axA[m_A-1]<m_axC[m_C-1])
- { m_axA[m_A]=m_axC[m_C-1];
- QPointF pt(200,490-m_A*20);
- disque[m_axA[m_A]].moveCenter(pt);
- m_C--;
- m_A++;
- }
- else QMessageBox::critical(this,"tour de hanoii","deplacement non autorise" );
- }
- update();
- }
- void MaFenetre::deplacerCB()
- {
- if(m_C!=0)
- {
- if(m_B==0)
- { m_axB[0]=m_axC[m_C-1];
- QPointF po(500,490);
- disque[m_axC[m_C-1]].moveCenter(po);
- m_C--;
- m_B++;
- }
- else if(m_axB[m_B-1]<m_axC[m_C-1])
- { m_axB[m_B]=m_axC[m_C-1];
- QPointF pt(500,490-m_B*20);
- disque[m_axB[m_B]].moveCenter(pt);
- m_C--;
- m_B++;
- }
- else QMessageBox::critical(this,"tour de hanoii","deplacement non autorise" );
- }
- update();
- }
- void MaFenetre::deplacer111()
- {
- hanoi(m_nbdisques,1,3,2);
- for(int j=0;j<10;j++)
- {QPointF po(200,490-20*j);
- disque[j].moveCenter(po);
- update();
- }
- m_A=m_nbdisques;
- m_B=0;
- m_C=0;
- for(int i=0;i<10;i++)
- {m_axA[i]=i;
- m_axB[i]=10;
- m_axC[i]=10;
- }
- solution();
- }
- void MaFenetre::hanoi(int n,int axe1,int axe2,int axe3)
- {
- if(n==1)
- deplacer(axe1,axe2);
- else
- {
- hanoi(n-1,axe1,axe3,axe2);
- deplacer(axe1,axe2);
- hanoi(n-1,axe3,axe2,axe1);
- }
- }
- void MaFenetre::solution()
- {
- if((tableau[cmp]==1)&&(tableau[cmp+1]==2))
- deplacerAB();
- if((tableau[cmp]==1)&&(tableau[cmp+1]==3))
- deplacerAC();
- if((tableau[cmp]==2)&&(tableau[cmp+1]==1))
- deplacerBA();
- if((tableau[cmp]==2)&&(tableau[cmp+1]==3))
- deplacerBC();
- if((tableau[cmp]==3)&&(tableau[cmp+1]==1))
- deplacerCA();
- if((tableau[cmp]==3)&&(tableau[cmp+1]==2))
- deplacerCB();
- QTimer *timer = new QTimer(this);
- connect(timer, SIGNAL(timeout()), this, SLOT(sol()));
- timer->stop();
- timer->start(1000);
- cmp=cmp+2;
- }
- void MaFenetre::sol()
- {if(cmp<pow(2,m_nbdisques)*2)
- { solution();
- }
- }
- void MaFenetre::deplacer(int x,int y)
- {
- tableau[comp]=x;
- tableau[comp+1]=y;
- comp=comp+2;
- if((x==1)&&(y==2))
- deplacerAB();
- if((x==1)&&(y==3))
- deplacerAC();
- if((x==2)&&(y==1))
- deplacerBA();
- if((x==2)&&(y==3))
- deplacerBC();
- if((x==3)&&(y==1))
- deplacerCA();
- if((x==3)&&(y==2))
- deplacerCB();
- }
- void MaFenetre::paintEvent(QPaintEvent*)
- {
- QPainter paintEvent(this);
- for(int i=0;i<m_nbdisques;i++)
- {
- QBrush brush;
- brush.setColor(Qt::blue);
- if(i==0)
- brush.setStyle(Qt::SolidPattern);
- if(i==1)
- brush.setStyle(Qt::Dense1Pattern);
- if(i==2)
- brush.setStyle(Qt::Dense2Pattern);
- if(i==3)
- brush.setStyle(Qt::Dense3Pattern);
- if(i==4)
- brush.setStyle(Qt::Dense4Pattern);
- if(i==5)
- brush.setStyle(Qt::Dense5Pattern);
- if(i==6)
- brush.setStyle(Qt::Dense6Pattern);
- if(i==7)
- brush.setStyle(Qt::Dense7Pattern);
- if(i==8)
- brush.setStyle(Qt::VerPattern);
- if(i==9)
- brush.setStyle(Qt::HorPattern);
- paintEvent.setBrush(brush);
- paintEvent.drawRect(disque[i]);
- }
- QRectF ax1(190,300,20,200);
- QRectF ax2(490,300,20,200);
- QRectF ax3(790,300,20,200);
- QVector<QRectF> axes;
- axes << ax1 << ax2 << ax3 ;
- QPainter myPainter(this);
- myPainter.setBrush(Qt::gray);
- myPainter.drawRects(axes);
- }
|
|