效果图:
FIR.pro
- #-------------------------------------------------
- #
- # Project created by QtCreator 2012-07-27T01:01:27
- #
- #-------------------------------------------------
- QT += core gui
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
- TARGET = FIR
- TEMPLATE = app
- SOURCES += main.cpp\
- mainwindow.cpp
- HEADERS += mainwindow.h
mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QtGui>
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- MainWindow(QWidget *parent = 0);
- ~MainWindow();
- void paintEvent(QPaintEvent *);
- void mouseReleaseEvent(QMouseEvent *);
- private:
- int a[15][15];
- int isWin(int, int);
- int f1(int, int);
- int f2(int, int);
- int f3(int, int);
- int f4(int, int);
- int player;
- };
- #endif // MAINWINDOW_H
man.cpp
- #include <QApplication>
- #include "mainwindow.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
mianwindow.cpp
- #include "mainwindow.h"
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- resize(640, 640);
- memset(a, 0, 15 * 15 * sizeof(int));
- player = 0;
- }
- MainWindow::~MainWindow()
- {
- }
- void MainWindow::paintEvent(QPaintEvent *)
- {
- QPainter p(this);
- p.setRenderHint(QPainter::Antialiasing, true);
- int i, j;
- for (i = 0; i < 16; i++)
- {
- p.drawLine(20, 20 + i * 40, 620, 20 + i * 40);
- p.drawLine(20 + i * 40, 20, 20 + i * 40, 620);
- }
- QBrush brush;
- brush.setStyle(Qt::SolidPattern);
- for (i = 0; i < 15; i++)
- {
- for (j = 0; j < 15; j++)
- {
- if (a[i][j] == 1)
- {
- brush.setColor(Qt::black);
- p.setBrush(brush);
- p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);
- }
- else if (a[i][j] == 2)
- {
- brush.setColor(Qt::white);
- p.setBrush(brush);
- p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);
- }
- }
- }
- }
- void MainWindow::mouseReleaseEvent(QMouseEvent *e)
- {
- int x, y;
- if(e->x() >= 20 && e->x() < 620 && e->y() >= 20 && e->y() < 620)
- {
- x = (e->x() - 20) / 40;
- y = (e->y() - 20) / 40;
- if (!a[x][y])
- {
- a[x][y] = player++ % 2 + 1;
- }
- if(isWin(x, y))
- {
- update();
- setEnabled(false);
- QMessageBox::information(this, "Win", "Win", QMessageBox::Ok);
- }
- }
- update();
- }
- int MainWindow::isWin(int x, int y)
- {
- return f1(x, y) || f2(x, y) || f3(x, y) || f4(x ,y);
- }
- int MainWindow::f1(int x, int y)
- {
- int i;
- for (i = 0; i < 5; i++)
- {
- if(y - i >= 0 &&
- y + 4 - i <= 0xF &&
- a[x][y - i] == a[x][y + 1 - i] &&
- a[x][y - i] == a[x][y + 2 - i] &&
- a[x][y - i] == a[x][y + 3 - i] &&
- a[x][y - i] == a[x][y + 4 - i])
- return 1;
- }
- return 0;
- }
- int MainWindow::f2(int x, int y)
- {
- int i;
- for (i = 0; i < 5; i++)
- {
- if(x - i >= 0 &&
- x + 4 - i <= 0xF &&
- a[x - i][y] == a[x + 1 - i][y] &&
- a[x - i][y] == a[x + 2 - i][y] &&
- a[x - i][y] == a[x + 3 - i][y] &&
- a[x - i][y] == a[x + 4 - i][y])
- return 1;
- }
- return 0;
- }
- int MainWindow::f3(int x, int y)
- {
- int i;
- for (i = 0; i < 5; i++)
- {
- if(x - i >= 0 &&
- y - i >= 0 &&
- x + 4 - i <= 0xF &&
- y + 4 - i <= 0xF &&
- a[x - i][y - i] == a[x + 1 - i][y + 1 - i] &&
- a[x - i][y - i] == a[x + 2 - i][y + 2 - i] &&
- a[x - i][y - i] == a[x + 3 - i][y + 3 - i] &&
- a[x - i][y - i] == a[x + 4 - i][y + 4 - i])
- return 1;
- }
- return 0;
- }
- int MainWindow::f4(int x, int y)
- {
- int i;
- for (i = 0; i < 5; i++)
- {
- if(x + i <= 0xF &&
- y - i >= 0 &&
- x - 4 + i >= 0 &&
- y + 4 - i <= 0xF &&
- a[x + i][y - i] == a[x - 1 + i][y + 1 - i] &&
- a[x + i][y - i] == a[x - 2 + i][y + 2 - i] &&
- a[x + i][y - i] == a[x - 3 + i][y + 3 - i] &&
- a[x + i][y - i] == a[x - 4 + i][y + 4 - i])
- return 1;
- }
- return 0;
- }
from:http://blog.csdn.net/zhangguangyi888/article/details/8192276