前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C/C++用QT写的五子棋源码

C/C++用QT写的五子棋源码

作者头像
bear_fish
发布2018-09-20 14:31:01
1.4K0
发布2018-09-20 14:31:01
举报

效果图:

FIR.pro

[cpp] view plaincopy

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2012-07-27T01:01:27
  4. #
  5. #-------------------------------------------------
  6. QT       += core gui  
  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets  
  8. TARGET = FIR  
  9. TEMPLATE = app  
  10. SOURCES += main.cpp\  
  11.         mainwindow.cpp  
  12. HEADERS  += mainwindow.h  

mainwindow.h

[cpp] view plaincopy

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QtGui>
  4. class MainWindow : public QMainWindow  
  5. {  
  6.     Q_OBJECT  
  7. public:  
  8.     MainWindow(QWidget *parent = 0);  
  9.     ~MainWindow();  
  10. void paintEvent(QPaintEvent *);  
  11. void mouseReleaseEvent(QMouseEvent *);  
  12. private:  
  13. int a[15][15];  
  14. int isWin(intint);  
  15. int f1(intint);  
  16. int f2(intint);  
  17. int f3(intint);  
  18. int f4(intint);  
  19. int player;  
  20. };  
  21. #endif // MAINWINDOW_H

man.cpp

[cpp] view plaincopy

  1. #include <QApplication>
  2. #include "mainwindow.h"
  3. int main(int argc, char *argv[])  
  4. {  
  5.     QApplication a(argc, argv);  
  6.     MainWindow w;  
  7.     w.show();  
  8. return a.exec();  
  9. }  

mianwindow.cpp

[cpp] view plaincopy

  1. #include "mainwindow.h"
  2. MainWindow::MainWindow(QWidget *parent)  
  3.     : QMainWindow(parent)  
  4. {  
  5.     resize(640, 640);  
  6.     memset(a, 0, 15 * 15 * sizeof(int));  
  7.     player = 0;  
  8. }  
  9. MainWindow::~MainWindow()  
  10. {  
  11. }  
  12. void MainWindow::paintEvent(QPaintEvent *)  
  13. {  
  14.     QPainter p(this);  
  15.     p.setRenderHint(QPainter::Antialiasing, true);  
  16. int i, j;  
  17. for (i = 0; i < 16; i++)  
  18.     {  
  19.         p.drawLine(20, 20 + i * 40, 620, 20 + i * 40);  
  20.         p.drawLine(20 + i * 40, 20, 20 + i * 40, 620);  
  21.     }  
  22.     QBrush brush;  
  23.     brush.setStyle(Qt::SolidPattern);  
  24. for (i = 0; i < 15; i++)  
  25.     {  
  26. for (j = 0; j < 15; j++)  
  27.         {  
  28. if (a[i][j] == 1)  
  29.             {  
  30.                 brush.setColor(Qt::black);  
  31.                 p.setBrush(brush);  
  32.                 p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);  
  33.             }  
  34. else if (a[i][j] == 2)  
  35.             {  
  36.                 brush.setColor(Qt::white);  
  37.                 p.setBrush(brush);  
  38.                 p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);  
  39.             }  
  40.         }  
  41.     }  
  42. }  
  43. void MainWindow::mouseReleaseEvent(QMouseEvent *e)  
  44. {  
  45. int x, y;  
  46. if(e->x() >= 20 && e->x() < 620 && e->y() >= 20 && e->y() < 620)  
  47.     {  
  48.         x = (e->x() - 20) / 40;  
  49.         y = (e->y() - 20) / 40;  
  50. if (!a[x][y])  
  51.         {  
  52.             a[x][y] = player++ % 2 + 1;  
  53.         }  
  54. if(isWin(x, y))  
  55.         {  
  56.             update();  
  57.             setEnabled(false);  
  58.             QMessageBox::information(this, "Win", "Win", QMessageBox::Ok);  
  59.         }  
  60.     }  
  61.     update();  
  62. }  
  63. int MainWindow::isWin(int x, int y)  
  64. {  
  65. return f1(x, y) || f2(x, y) || f3(x, y) || f4(x ,y);  
  66. }  
  67. int MainWindow::f1(int x, int y)  
  68. {  
  69. int i;  
  70. for (i = 0; i < 5; i++)  
  71.     {  
  72. if(y - i >= 0 &&  
  73.            y + 4 - i <= 0xF &&  
  74.            a[x][y - i] == a[x][y + 1 - i] &&  
  75.            a[x][y - i] == a[x][y + 2 - i] &&  
  76.            a[x][y - i] == a[x][y + 3 - i] &&  
  77.            a[x][y - i] == a[x][y + 4 - i])  
  78. return 1;  
  79.     }  
  80. return 0;  
  81. }  
  82. int MainWindow::f2(int x, int y)  
  83. {  
  84. int i;  
  85. for (i = 0; i < 5; i++)  
  86.     {  
  87. if(x - i >= 0 &&  
  88.            x + 4 - i <= 0xF &&  
  89.            a[x - i][y] == a[x + 1 - i][y] &&  
  90.            a[x - i][y] == a[x + 2 - i][y] &&  
  91.            a[x - i][y] == a[x + 3 - i][y] &&  
  92.            a[x - i][y] == a[x + 4 - i][y])  
  93. return 1;  
  94.     }  
  95. return 0;  
  96. }  
  97. int MainWindow::f3(int x, int y)  
  98. {  
  99. int i;  
  100. for (i = 0; i < 5; i++)  
  101.     {  
  102. if(x - i >= 0 &&  
  103.            y - i >= 0 &&  
  104.            x + 4 - i <= 0xF &&  
  105.            y + 4 - i <= 0xF &&  
  106.            a[x - i][y - i] == a[x + 1 - i][y + 1 - i] &&  
  107.            a[x - i][y - i] == a[x + 2 - i][y + 2 - i] &&  
  108.            a[x - i][y - i] == a[x + 3 - i][y + 3 - i] &&  
  109.            a[x - i][y - i] == a[x + 4 - i][y + 4 - i])  
  110. return 1;  
  111.     }  
  112. return 0;  
  113. }  
  114. int MainWindow::f4(int x, int y)  
  115. {  
  116. int i;  
  117. for (i = 0; i < 5; i++)  
  118.     {  
  119. if(x + i <= 0xF &&  
  120.            y - i >= 0 &&  
  121.            x - 4 + i >= 0 &&  
  122.            y + 4 - i <= 0xF &&  
  123.            a[x + i][y - i] == a[x - 1 + i][y + 1 - i] &&  
  124.            a[x + i][y - i] == a[x - 2 + i][y + 2 - i] &&  
  125.            a[x + i][y - i] == a[x - 3 + i][y + 3 - i] &&  
  126.            a[x + i][y - i] == a[x - 4 + i][y + 4 - i])  
  127. return 1;  
  128.     }  
  129. return 0;  
  130. }  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年04月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档