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

效果图:

 

FIR.pro

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


 

mainwindow.h

 

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


man.cpp

 

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


 

mianwindow.cpp

[cpp]  view plain copy
  1. #include "mainwindow.h"  
  2.   
  3. MainWindow::MainWindow(QWidget *parent)  
  4.     : QMainWindow(parent)  
  5. {  
  6.     resize(640, 640);  
  7.     memset(a, 0, 15 * 15 * sizeof(int));  
  8.     player = 0;  
  9. }  
  10.   
  11. MainWindow::~MainWindow()  
  12. {  
  13.       
  14. }  
  15.   
  16. void MainWindow::paintEvent(QPaintEvent *)  
  17. {  
  18.     QPainter p(this);  
  19.     p.setRenderHint(QPainter::Antialiasing, true);  
  20.     int i, j;  
  21.     for (i = 0; i < 16; i++)  
  22.     {  
  23.         p.drawLine(20, 20 + i * 40, 620, 20 + i * 40);  
  24.         p.drawLine(20 + i * 40, 20, 20 + i * 40, 620);  
  25.     }  
  26.   
  27.     QBrush brush;  
  28.     brush.setStyle(Qt::SolidPattern);  
  29.     for (i = 0; i < 15; i++)  
  30.     {  
  31.         for (j = 0; j < 15; j++)  
  32.         {  
  33.             if (a[i][j] == 1)  
  34.             {  
  35.                 brush.setColor(Qt::black);  
  36.                 p.setBrush(brush);  
  37.                 p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);  
  38.             }  
  39.             else if (a[i][j] == 2)  
  40.             {  
  41.                 brush.setColor(Qt::white);  
  42.                 p.setBrush(brush);  
  43.                 p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);  
  44.             }  
  45.         }  
  46.     }  
  47. }  
  48.   
  49. void MainWindow::mouseReleaseEvent(QMouseEvent *e)  
  50. {  
  51.     int x, y;  
  52.     if(e->x() >= 20 && e->x() < 620 && e->y() >= 20 && e->y() < 620)  
  53.     {  
  54.         x = (e->x() - 20) / 40;  
  55.         y = (e->y() - 20) / 40;  
  56.         if (!a[x][y])  
  57.         {  
  58.             a[x][y] = player++ % 2 + 1;  
  59.         }  
  60.         if(isWin(x, y))  
  61.         {  
  62.             update();  
  63.             setEnabled(false);  
  64.             QMessageBox::information(this"Win""Win", QMessageBox::Ok);  
  65.         }  
  66.     }  
  67.     update();  
  68. }  
  69.   
  70. int MainWindow::isWin(int x, int y)  
  71. {  
  72.      return f1(x, y) || f2(x, y) || f3(x, y) || f4(x ,y);  
  73. }  
  74.   
  75. int MainWindow::f1(int x, int y)  
  76. {  
  77.     int i;  
  78.     for (i = 0; i < 5; i++)  
  79.     {  
  80.         if(y - i >= 0 &&  
  81.            y + 4 - i <= 0xF &&  
  82.            a[x][y - i] == a[x][y + 1 - i] &&  
  83.            a[x][y - i] == a[x][y + 2 - i] &&  
  84.            a[x][y - i] == a[x][y + 3 - i] &&  
  85.            a[x][y - i] == a[x][y + 4 - i])  
  86.         return 1;  
  87.     }  
  88.     return 0;  
  89. }  
  90.   
  91. int MainWindow::f2(int x, int y)  
  92. {  
  93.     int i;  
  94.     for (i = 0; i < 5; i++)  
  95.     {  
  96.         if(x - i >= 0 &&  
  97.            x + 4 - i <= 0xF &&  
  98.            a[x - i][y] == a[x + 1 - i][y] &&  
  99.            a[x - i][y] == a[x + 2 - i][y] &&  
  100.            a[x - i][y] == a[x + 3 - i][y] &&  
  101.            a[x - i][y] == a[x + 4 - i][y])  
  102.            return 1;  
  103.     }  
  104.     return 0;  
  105. }  
  106.   
  107. int MainWindow::f3(int x, int y)  
  108. {  
  109.     int i;  
  110.     for (i = 0; i < 5; i++)  
  111.     {  
  112.         if(x - i >= 0 &&  
  113.            y - i >= 0 &&  
  114.            x + 4 - i <= 0xF &&  
  115.            y + 4 - i <= 0xF &&  
  116.            a[x - i][y - i] == a[x + 1 - i][y + 1 - i] &&  
  117.            a[x - i][y - i] == a[x + 2 - i][y + 2 - i] &&  
  118.            a[x - i][y - i] == a[x + 3 - i][y + 3 - i] &&  
  119.            a[x - i][y - i] == a[x + 4 - i][y + 4 - i])  
  120.            return 1;  
  121.     }  
  122.     return 0;  
  123. }  
  124.   
  125. int MainWindow::f4(int x, int y)  
  126. {  
  127.     int i;  
  128.     for (i = 0; i < 5; i++)  
  129.     {  
  130.         if(x + i <= 0xF &&  
  131.            y - i >= 0 &&  
  132.            x - 4 + i >= 0 &&  
  133.            y + 4 - i <= 0xF &&  
  134.            a[x + i][y - i] == a[x - 1 + i][y + 1 - i] &&  
  135.            a[x + i][y - i] == a[x - 2 + i][y + 2 - i] &&  
  136.            a[x + i][y - i] == a[x - 3 + i][y + 3 - i] &&  
  137.            a[x + i][y - i] == a[x - 4 + i][y + 4 - i])  
  138.            return 1;  
  139.     }  
  140.     return 0;  
  141. }  


 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值