博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
79.QT解决迷宫问题(面向过程与面向对象)
阅读量:5360 次
发布时间:2019-06-15

本文共 10832 字,大约阅读时间需要 36 分钟。

面向过程:


创建一个类继承dialog,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     //up25     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     //down44     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     //left63     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     //right82     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
    depth;i++) 10 { 11 this->ppp[i] = new mydialog *[this->width]; 12 //j代表的是每一行的第几个 13 for(int j=0;j
    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
    depth;i++) 41 { 42 for(int j=0;j
    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
    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
    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
    width) && (y>=0 && y
    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 QDialog12 {13 Q_OBJECT14 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     //up28    //m->up();29     pm->up();30 }31 32 void Dialog::on_pushButton_4_clicked()33 {34     //down35    //m->down();36     pm->down();37 }38 39 void Dialog::on_pushButton_2_clicked()40 {41     //left42    // m->left();43     pm->left();44 }45 46 void Dialog::on_pushButton_3_clicked()47 {48     //right49     // 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

你可能感兴趣的文章
Windows下文件夹扩展名
查看>>
今天早上6:00起来,每天晚上回来6点多已经天黑
查看>>
debian开启cgroup memory子系统
查看>>
信息收集
查看>>
SQL Server 中使用Convert来取得datetime数据类型样式(全)
查看>>
Python中list的拷贝问题
查看>>
Java学习第二周学习笔记
查看>>
SQL基本语句
查看>>
linux-Centos7安装python3并与python2共存
查看>>
Thread和ThreadPool的应用解析
查看>>
Django部署问题
查看>>
UE4开发神秘海域类游戏原型 初阶(二):动画资源的整合
查看>>
关于二维码的简单使用
查看>>
LeetCode:Regular Expression Matching
查看>>
ToadforDB2_Freeware_6.x 安装报错“Toad did not find a default DB2 client”
查看>>
div 溢出隐藏 div文字溢出用点(省略号)代替
查看>>
关于获取基站信息总结
查看>>
BZOJ.4650.[NOI2016]优秀的拆分(后缀数组 思路)
查看>>
IPMI学习资料
查看>>
CODE[VS] 1497 取余运算
查看>>