面向过程:


创建一个类继承dialog,mydialog,添加两个变量

  1. #ifndef MYDIALOG_H
  2. #define MYDIALOG_H
  3.  
  4. #include <QDialog>>
  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 <QApplication>
  3. #include "mydialog.h"
  4.  
  5. //矩阵数组
  6. mydialog *p[][];
  7. int i=;
  8. int j = ;
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. QApplication a(argc, argv);
  13. Dialog w;
  14. w.show();
  15. w.move(,);
  16.  
  17. //设置颜色(背景为红色)
  18. QPalette colorit(w.palette());
  19. colorit.setColor(QPalette::Background,Qt::red);
  20. w.setPalette(colorit);
  21.  
  22. //绘制窗口
  23. for(int i=;i<;i++)
  24. {
  25. for(int j = ;j<;j++)
  26. {
  27. p[i][j] = new mydialog;
  28. p[i][j]->resize(,);
  29. p[i][j]->move(j*,i*);
  30. p[i][j]->show();
  31. p[i][j]->id = ;
  32. p[i][j]->AI=;
  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 = ;
  41. QPalette colorit(p[][]->palette());
  42. colorit.setColor(QPalette::Background,Qt::red);
  43. p[i][j]->setPalette(colorit);
  44. }
  45. //设置障碍
  46. {
  47. p[][]->id = ;
  48. QPalette colorit(p[][]->palette());
  49. colorit.setColor(QPalette::Background,Qt::black);
  50. p[][]->setPalette(colorit);
  51. }
  52. {
  53. p[][]->id = ;
  54. QPalette colorit(p[][]->palette());
  55. colorit.setColor(QPalette::Background,Qt::black);
  56. p[][]->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[][];
  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->= && p[i-][j]->id<)
  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 = ;
  31. i-=;
  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 = ;
  37. }
  38. }
  39. }
  40.  
  41. void Dialog::on_pushButton_4_clicked()
  42. {
  43. //down
  44. if(i+< && p[i+][j]->id<)
  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 = ;
  50. i+=;
  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 = ;
  56. }
  57. }
  58. }
  59.  
  60. void Dialog::on_pushButton_2_clicked()
  61. {
  62. //left
  63. if(j->= && p[i][j-]->id<)
  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 = ;
  69. j-=;
  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 = ;
  75. }
  76. }
  77. }
  78.  
  79. void Dialog::on_pushButton_3_clicked()
  80. {
  81. //right
  82. if(j+< && p[i][j+]->id<)
  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 = ;
  88. j+=;
  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 = ;
  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=;i<this->depth;i++)
    10. {
    11. this->ppp[i] = new mydialog *[this->width];
    12. //j代表的是每一行的第几个
    13. for(int j=;j<this->width;j++)
    14. {
    15. this->ppp[i][j] = new mydialog;
    16. this->ppp[i][j]->resize(,);
    17. // 前面乘的是列 后面乘的是行
    18. this->ppp[i][j]->move(i*,j*);
    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 = ;
    25. }
    26. }
    27.  
    28. //初始化初始位置
    29. QPalette colorit(this->ppp[][]->palette());
    30. colorit.setColor(QPalette::Background,Qt::red);
    31. this->ppp[][]->setPalette(colorit);
    32. this->ppp[][]->id = ;
    33. //标识位置
    34. this->i = ;
    35. this->j = ;
    36. }
    37.  
    38. mg::~mg()
    39. {
    40. for(int i=;i<this->depth;i++)
    41. {
    42. for(int j=;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->= && ppp[i][j-]->id<)
    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 = ;
    59. j-=;
    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 = ;
    65. }
    66. }
    67.  
    68. }
    69.  
    70. void mg::down()
    71. {
    72. if(j+<this->width && ppp[i][j+]->id<)
    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 = ;
    78. j+=;
    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 = ;
    84. }
    85. }
    86. }
    87.  
    88. void mg::left()
    89. {
    90. if(i->= && ppp[i-][j]->id<)
    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 = ;
    96. i-=;
    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 = ;
    102. }
    103. }
    104. }
    105.  
    106. void mg::right()
    107. {
    108.  
    109. if(i+<this->depth && ppp[i+][j]->id<)
    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 = ;
    115. i+=;
    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 = ;
    121. }
    122. }
    123. }
    124.  
    125. //设置障碍
    126. void mg::setnotrun(int x, int y)
    127. {
    128. if((x>= && x<this->width) && (y>= && 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 = ;
    134. }
    135. }
  • mydialog.h
    1. #ifndef MYDIALOG_H
    2. #define MYDIALOG_H
    3.  
    4. #include <QDialog>>
    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 <QDialog>
    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 = );
    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. Dialog::Dialog(QWidget *parent) :
    10. QDialog(parent),
    11. ui(new Ui::Dialog)
    12. {
    13. pm = new mg(,);
    14. pm->setnotrun(,);
    15. pm->setnotrun(,);
    16. ui->setupUi(this);
    17. }
    18.  
    19. Dialog::~Dialog()
    20. {
    21. delete ui;
    22. }
    23.  
    24. void Dialog::on_pushButton_clicked()
    25. {
    26. //up
    27. //m->up();
    28. pm->up();
    29. }
    30.  
    31. void Dialog::on_pushButton_4_clicked()
    32. {
    33. //down
    34. //m->down();
    35. pm->down();
    36. }
    37.  
    38. void Dialog::on_pushButton_2_clicked()
    39. {
    40. //left
    41. // m->left();
    42. pm->left();
    43. }
    44.  
    45. void Dialog::on_pushButton_3_clicked()
    46. {
    47. //right
    48. // m->right();
    49. pm->right();
    50. }
  • main.cpp
    1. #include "dialog.h"
    2. #include <QApplication>
    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(,);
    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. }

79.QT解决迷宫问题(面向过程与面向对象)的更多相关文章

  1. 面向过程 vs 面向对象

    从网上摘录了一些面向过程vs.面向对象的分析,先简单记录如下,稍后会继续整理. 为什么会出现面向对象分析方法? 因为现实世界太复杂多变,面向过程的分析方法无法实现. 面向过程 采用面向过程必须了解整个 ...

  2. python 面向过程和面向对象比较

    面向过程 VS 面向对象 面向过程的程序设计:核心是过程二字,过程指的是解决问题的步骤,即先干什么再干什么......面向过程的设计就好比精心设计好一条流水线,是一种机械式的思维方式. 优点是:复杂度 ...

  3. python基础(23):面向过程与面向对象的优劣、初识面向对象

    1. 面向过程与面向对象的优劣 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了写程 ...

  4. C语言学习系列(二)面向过程和面向对象

    一.基本定义 (一).面向过程(procedure oriented programming POP) 面向过程是分析解决问题的步骤,然后用函数把这些步骤一步一步的实现,然后在使用的时候一一调用则可. ...

  5. JS是面向过程、面向对象还是基于对象?面向对象的代码体现

    一.问题 javascript是面向对象的,还是面向过程的?基于对象是什么意思? 对象: 指的是对某一类事物进行抽象,抽象出这一类事物共同的特征以及行为(也就是属性和方法),那些拥有这一共同属性和方法 ...

  6. C++笔记005:用面向过程和面向对象方法求解圆形面积

    原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 结束了第一个hello world程序后,我们来用面向过程和面向对象两个方法来求解圆的面积这个问题,以能够更清晰的体会面向对象和面向过程. ...

  7. PHP面向过程和面向对象

    php程序编写分为面向过程和面向对象.两者在功能实现上没有区别,但是在代码编写上区别很大,面向过程的代码很乱,不易管理,而面向对象把常用的功能封装为一个类,这样代码清楚多了. 下面举个小例子说明一下: ...

  8. essential C++中关于面向过程和面向对象的说明

    昨天在阅读essential C++中看到了一个关于面向过程和面向对象的区别的例子,感觉挺好的.记录下来.... 这个例子是关于照相机的.照相机有三个性质,一个是控制位置:通常使用3个浮点数据来表示其 ...

  9. PHP mysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理

    相关文章:PHP的mysql扩展整理,操作数据库的实现过程分析  PHP PDO扩展整理,包括环境配置\基本增删改查\事务\预处理 介绍 mysqli是PHP程序与mysql数据库进行数据交互的桥梁, ...

随机推荐

  1. codeforces 544 D Destroying Roads 【最短路】

    题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs ...

  2. 事件代理(event的target属性)

    event的target属性 一个题:请通过事件代理实现当点击每一个li标签,弹出相应li标签内的内容 Event对象提供了一个属性叫target,可以返回事件的目标节点,我们称为事件源,也就是说,t ...

  3. iOS开发——根据数组中的字典中的某一元素排序

    数组中的元素是字典,字典中的某一个元素,比如说姓名,现在需要按照姓名的首字母来排序,怎么搞? 做法很简单,在字典中加一个元素,保存姓名的首字母,然后用下面的方法排序. - (void)sortWifi ...

  4. C# 快捷使用自定义配置节点

    C#除了appSettings和connectionStrings默认配置外还允许用户自定义使用配置.C# 提供3中简单的自定义配置,配置文件如下 <?xml version="1.0 ...

  5. 【 D3.js 入门系列 --- 2.1 】 关于怎样选择,插入,删除元素

    本人的个人博客首页为: http://www.ourd3js.com/  ,csdn博客首页为:http://blog.csdn.net/lzhlzz/. 转载请注明出处,谢谢. 在D3.js中,选择 ...

  6. android 选取部分 log 的两种方法

    Grep多个条件: android logcat -v time | grep -e A -e B 选取多个android log tag: android logcat -v time -s TAG ...

  7. 英语影视台词---三、Cinema Paradiso

    英语影视台词---三.Cinema Paradiso 一.总结 一句话总结:天堂电影院 1.Alfredo: No, Toto. Nobody said it. This time it's all ...

  8. Python(五) 包、模块、函数与变量作用域

    一.while循环与使用场景 CONDITION=1 while CONDITION <=5 : CONDITION +=1 print("hello") else: pri ...

  9. Json 序列化以及反序列化的三种方式(二)

    1.什么是JSON? Json[javascript对象表示方法],它是一个轻量级的数据交换格式,我们可以很简单的来读取和写它,并且它很容易被计算机转化和生成,它是完全独立于语言的 2.Json支持下 ...

  10. .net 项目分层及规范

       1.解决方案命名:公司简称+产品名称.如ABCSOft.BBS 2.解决方案文件夹:以数字排序例如:01.Web表示页面层:02.IBusinessLogic表示业务逻辑接口:03.Bussin ...