79QT 미로 해결(프로세스 및 대상 대상)

55126 단어
프로세스 대상:
클래스 계승 다이어로그, mydialog를 만들고 두 변수를 추가합니다
 1 #ifndef MYDIALOG_H
 2 #define MYDIALOG_H
 3 
 4 #include >
 5 
 6 class mydialog : public QDialog
 7 {
 8 public:
 9     mydialog();
10     int id;// ,1 , ,2 , 
11     int AI;
12 
13 public:
14     void setcolor();
15 };
16 
17 #endif // MYDIALOG_H

 
main.cpp에서 초기화
 1 #include "dialog.h"
 2 #include 
 3 #include "mydialog.h"
 4 
 5 // 
 6 mydialog *p[6][8];
 7 int i=1;
 8 int j = 1;
 9 
10 int main(int argc, char *argv[])
11 {
12     QApplication a(argc, argv);
13     Dialog w;
14     w.show();
15     w.move(1000,300);
16 
17     // ( )
18     QPalette colorit(w.palette());
19     colorit.setColor(QPalette::Background,Qt::red);
20     w.setPalette(colorit);
21 
22     // 
23     for(int i=0;i<6;i++)
24     {
25         for(int j = 0;j<8;j++)
26         {
27             p[i][j] = new mydialog;
28             p[i][j]->resize(80,80);
29             p[i][j]->move(j*100,i*100);
30             p[i][j]->show();
31             p[i][j]->id = 0;
32             p[i][j]->AI=0;
33             QPalette colorit(p[i][j]->palette());
34             colorit.setColor(QPalette::Background,Qt::white);
35             p[i][j]->setPalette(colorit);
36         }
37     }
38     // 
39     {
40         p[i][j]->id = 1;
41         QPalette colorit(p[0][0]->palette());
42         colorit.setColor(QPalette::Background,Qt::red);
43         p[i][j]->setPalette(colorit);
44     }
45     // 
46     {
47         p[4][5]->id = 2;
48         QPalette colorit(p[4][5]->palette());
49         colorit.setColor(QPalette::Background,Qt::black);
50         p[4][5]->setPalette(colorit);
51     }
52     {
53         p[5][6]->id = 2;
54         QPalette colorit(p[4][5]->palette());
55         colorit.setColor(QPalette::Background,Qt::black);
56         p[5][6]->setPalette(colorit);
57     }
58 
59     return a.exec();
60 }

 
dialog.cpp에서 단추를 실행하는 작업
 1 #include "dialog.h"
 2 #include "ui_dialog.h"
 3 #include "mydialog.h"
 4 
 5 // 
 6 extern int i;
 7 extern int j;
 8 extern mydialog *p[6][8];
 9 
10 Dialog::Dialog(QWidget *parent) :
11     QDialog(parent),
12     ui(new Ui::Dialog)
13 {
14     ui->setupUi(this);
15 }
16 
17 Dialog::~Dialog()
18 {
19     delete ui;
20 }
21 
22 void Dialog::on_pushButton_clicked()
23 {
24     //up
25     if(i-1>=0 && p[i-1][j]->id<1)
26     {
27             QPalette colorit(p[i][j]->palette());
28             colorit.setColor(QPalette::Background,Qt::white);
29             p[i][j]->setPalette(colorit);
30             p[i][j]->id = 0;
31             i-=1;
32             {
33                 QPalette colorit(p[i][j]->palette());
34                 colorit.setColor(QPalette::Background,Qt::red);
35                 p[i][j]->setPalette(colorit);
36                 p[i][j]->id = 1;
37             }
38     }
39 }
40 
41 void Dialog::on_pushButton_4_clicked()
42 {
43     //down
44     if(i+1<6 && p[i+1][j]->id<1)
45     {
46             QPalette colorit(p[i][j]->palette());
47             colorit.setColor(QPalette::Background,Qt::white);
48             p[i][j]->setPalette(colorit);
49             p[i][j]->id = 0;
50             i+=1;
51             {
52                 QPalette colorit(p[i][j]->palette());
53                 colorit.setColor(QPalette::Background,Qt::red);
54                 p[i][j]->setPalette(colorit);
55                 p[i][j]->id = 1;
56             }
57     }
58 }
59 
60 void Dialog::on_pushButton_2_clicked()
61 {
62     //left
63     if(j-1>=0 && p[i][j-1]->id<1)
64     {
65             QPalette colorit(p[i][j]->palette());
66             colorit.setColor(QPalette::Background,Qt::white);
67             p[i][j]->setPalette(colorit);
68             p[i][j]->id = 0;
69             j-=1;
70             {
71                 QPalette colorit(p[i][j]->palette());
72                 colorit.setColor(QPalette::Background,Qt::red);
73                 p[i][j]->setPalette(colorit);
74                 p[i][j]->id = 1;
75             }
76     }
77 }
78 
79 void Dialog::on_pushButton_3_clicked()
80 {
81     //right
82     if(j+1<8 && p[i][j+1]->id<1)
83     {
84             QPalette colorit(p[i][j]->palette());
85             colorit.setColor(QPalette::Background,Qt::white);
86             p[i][j]->setPalette(colorit);
87             p[i][j]->id = 0;
88             j+=1;
89             {
90                 QPalette colorit(p[i][j]->palette());
91                 colorit.setColor(QPalette::Background,Qt::red);
92                 p[i][j]->setPalette(colorit);
93                 p[i][j]->id = 1;
94             }
95     }
96 }

 
객체 대상:
  • mg.h
     1 #ifndef MG_H
     2 #define MG_H
     3 #include "mydialog.h"
     4 
     5 class mg
     6 {
     7 private:
     8     // 
     9     int i;
    10     int j;
    11     // , 
    12     int width;
    13     int depth;
    14     mydialog ***ppp;
    15 public:
    16     mg(int w,int d);
    17     ~mg();
    18     void up();
    19     void down();
    20     void left();
    21     void right();
    22     void setnotrun(int x,int y);
    23 
    24 };
    25 
    26 #endif // MG_H

     
  • mg.cpp
      1 #include "mg.h"
      2 
      3 mg::mg(int w,int d)
      4 {
      5     // 
      6     this->width = w;
      7     this->depth = d;
      8     this->ppp = new mydialog **[this->depth];// 
      9     for(int i=0;i<this->depth;i++)
     10     {
     11         this->ppp[i] = new mydialog *[this->width];
     12         //j 
     13         for(int j=0;j<this->width;j++)
     14         {
     15             this->ppp[i][j] = new mydialog;
     16             this->ppp[i][j]->resize(80,80);
     17             //              
     18             this->ppp[i][j]->move(i*100,j*100);
     19             this->ppp[i][j]->show();
     20 
     21             QPalette colorit(this->ppp[i][j]->palette());
     22             colorit.setColor(QPalette::Background,Qt::white);
     23            this->ppp[i][j]->setPalette(colorit);
     24            this->ppp[i][j]->id = 0;
     25         }
     26     }
     27 
     28     // 
     29     QPalette colorit(this->ppp[1][1]->palette());
     30     colorit.setColor(QPalette::Background,Qt::red);
     31     this->ppp[1][1]->setPalette(colorit);
     32     this->ppp[1][1]->id = 1;
     33     // 
     34     this->i = 1;
     35     this->j = 1;
     36 }
     37 
     38 mg::~mg()
     39 {
     40     for(int i=0;i<this->depth;i++)
     41     {
     42         for(int j=0;j<this->width;j++)
     43         {
     44             delete this->ppp[i][j];
     45         }
     46         delete [] this->ppp[i];
     47     }
     48     delete[] this->ppp;
     49 }
     50 
     51 void mg::up()
     52 {
     53     if(j-1>=0 && ppp[i][j-1]->id<1)
     54     {
     55             QPalette colorit(ppp[i][j]->palette());
     56             colorit.setColor(QPalette::Background,Qt::white);
     57             ppp[i][j]->setPalette(colorit);
     58             ppp[i][j]->id = 0;
     59             j-=1;
     60             {
     61                 QPalette colorit(ppp[i][j]->palette());
     62                 colorit.setColor(QPalette::Background,Qt::red);
     63                 ppp[i][j]->setPalette(colorit);
     64                 ppp[i][j]->id = 1;
     65             }
     66     }
     67 
     68 }
     69 
     70 void mg::down()
     71 {
     72     if(j+1<this->width && ppp[i][j+1]->id<1)
     73     {
     74             QPalette colorit(ppp[i][j]->palette());
     75             colorit.setColor(QPalette::Background,Qt::white);
     76             ppp[i][j]->setPalette(colorit);
     77             ppp[i][j]->id = 0;
     78             j+=1;
     79             {
     80                 QPalette colorit(ppp[i][j]->palette());
     81                 colorit.setColor(QPalette::Background,Qt::red);
     82                 ppp[i][j]->setPalette(colorit);
     83                 ppp[i][j]->id = 1;
     84             }
     85     }
     86 }
     87 
     88 void mg::left()
     89 {
     90     if(i-1>=0 && ppp[i-1][j]->id<1)
     91     {
     92             QPalette colorit(ppp[i][j]->palette());
     93             colorit.setColor(QPalette::Background,Qt::white);
     94             ppp[i][j]->setPalette(colorit);
     95             ppp[i][j]->id = 0;
     96             i-=1;
     97             {
     98                 QPalette colorit(ppp[i][j]->palette());
     99                 colorit.setColor(QPalette::Background,Qt::red);
    100                 ppp[i][j]->setPalette(colorit);
    101                 ppp[i][j]->id = 1;
    102             }
    103     }
    104 }
    105 
    106 void mg::right()
    107 {
    108 
    109     if(i+1<this->depth && ppp[i+1][j]->id<1)
    110     {
    111             QPalette colorit(ppp[i][j]->palette());
    112             colorit.setColor(QPalette::Background,Qt::white);
    113             ppp[i][j]->setPalette(colorit);
    114             ppp[i][j]->id = 0;
    115             i+=1;
    116             {
    117                 QPalette colorit(ppp[i][j]->palette());
    118                 colorit.setColor(QPalette::Background,Qt::red);
    119                 ppp[i][j]->setPalette(colorit);
    120                 ppp[i][j]->id = 1;
    121             }
    122     }
    123 }
    124 
    125 // 
    126 void mg::setnotrun(int x, int y)
    127 {
    128     if((x>=0 && x<this->width) && (y>=0 && y<this->depth))
    129     {
    130         QPalette colorit(ppp[x][y]->palette());
    131         colorit.setColor(QPalette::Background,Qt::black);
    132         ppp[y][x]->setPalette(colorit);
    133         ppp[y][x]->id = 2;
    134     }
    135 }

     
  • mydialog.h
     1 #ifndef MYDIALOG_H
     2 #define MYDIALOG_H
     3 
     4 #include >
     5 
     6 class mydialog : public QDialog
     7 {
     8 public:
     9     mydialog();
    10     int id;// ,1 , ,2 , 
    11     int AI;
    12 
    13 public:
    14     void setcolor();
    15 };
    16 
    17 #endif // MYDIALOG_H

     
  • mydialog.cpp
    1 #include "mydialog.h"
    2 
    3 mydialog::mydialog()
    4 {
    5 
    6 }

     
  • dialog.h
     1 #ifndef DIALOG_H
     2 #define DIALOG_H
     3 
     4 #include 
     5 #include"mg.h"
     6 
     7 namespace Ui {
     8 class Dialog;
     9 }
    10 
    11 class Dialog : public QDialog
    12 {
    13     Q_OBJECT
    14 
    15 public:
    16     explicit Dialog(QWidget *parent = 0);
    17     ~Dialog();
    18 
    19 private slots:
    20     void on_pushButton_clicked();
    21 
    22     void on_pushButton_4_clicked();
    23 
    24     void on_pushButton_2_clicked();
    25 
    26     void on_pushButton_3_clicked();
    27 
    28 private:
    29     Ui::Dialog *ui;
    30     mg *pm;
    31 };
    32 
    33 #endif // DIALOG_H

     
  • dialog.cpp
     1 #include "dialog.h"
     2 #include "ui_dialog.h"
     3 #include "mydialog.h"
     4 #include "mg.h"
     5 
     6 // 
     7 extern mg *m;
     8 
     9 
    10 Dialog::Dialog(QWidget *parent) :
    11     QDialog(parent),
    12     ui(new Ui::Dialog)
    13 {
    14     pm = new mg(6,8);
    15     pm->setnotrun(3,4);
    16     pm->setnotrun(4,3);
    17     ui->setupUi(this);
    18 }
    19 
    20 Dialog::~Dialog()
    21 {
    22     delete ui;
    23 }
    24 
    25 void Dialog::on_pushButton_clicked()
    26 {
    27     //up
    28    //m->up();
    29     pm->up();
    30 }
    31 
    32 void Dialog::on_pushButton_4_clicked()
    33 {
    34     //down
    35    //m->down();
    36     pm->down();
    37 }
    38 
    39 void Dialog::on_pushButton_2_clicked()
    40 {
    41     //left
    42    // m->left();
    43     pm->left();
    44 }
    45 
    46 void Dialog::on_pushButton_3_clicked()
    47 {
    48     //right
    49     // m->right();
    50     pm->right();
    51 }

     
  • main.cpp
     1 #include "dialog.h"
     2 #include 
     3 #include "mydialog.h"
     4 #include "mg.h"
     5 
     6 mg *m;
     7 
     8 int main(int argc, char *argv[])
     9 {
    10     QApplication a(argc, argv);
    11     Dialog w;
    12     w.show();
    13     w.move(1000,300);
    14 
    15     // ( )
    16     QPalette colorit(w.palette());
    17     colorit.setColor(QPalette::Background,Qt::red);
    18     w.setPalette(colorit);
    19 
    20     // 
    21     //mg *m = new mg(6,8);
    22 
    23     return a.exec();
    24 }

     

  •  
    전재 대상:https://www.cnblogs.com/xiaochi/p/8581075.html

    좋은 웹페이지 즐겨찾기