服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - QT实现简单五子棋游戏

QT实现简单五子棋游戏

2021-11-09 13:35影帝sunny C/C++

这篇文章主要为大家详细介绍了QT实现简单五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了QT实现简单五子棋游戏的具体代码,供大家参考,具体内容如下

FIR.pro

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#-------------------------------------------------
#
# Project created by QtCreator 2012-09-01T15:09:11
#
#-------------------------------------------------
 
QT       += core gui
 
TARGET = FIR
TEMPLATE = app
 
 
SOURCES += main.cpp\
        widget.cpp
HEADERS  += widget.h
 
/******************************/

wight.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef WIDGET_H
#define WIDGET_H
 
#include <QtGui>
 
class Widget : public QWidget
{
    Q_OBJECT
private:
    int a[15][15];
    int player;
 
    bool isWin(int, int);
    bool f1(int, int);
    bool f2(int, int);
    bool f3(int, int);
    bool f4(int, int);
 
public:
    Widget(QWidget *parent = 0);
    ~Widget();
 
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent * e);
};
 
#endif // WIDGET_H
 
/****************************************/

widget.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "widget.h"
 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    resize(640, 640);
    player = 0;
    memset(a, 0, 15 * 15 * sizeof(int));
}
 
Widget::~Widget()
{
   
}
 
  //画方格:
 
void Widget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    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);
    }
 
    int m = 5;
    int n = 0;
    while(1)
    {
                QBrush brush(Qt::SolidPattern);
                brush.setColor(Qt::blue);
                p.setBrush(brush);
           //     p.drawRect((i+1)*20,(j+1)*20,80,80);//zheng fang xing
            //    p.drawRect((i+1)*20,(j+1)*20,40,160);//chang fang xing
                p.drawRect((m+1)*20,(n+1)*20,80,40);
                p.drawRect((m+3)*20,(n+3)*20,80,40);
 
                //p.drawRect();
                //p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);
    }
 
}
 
 
 
//鼠标响应:
 
void Widget::mousePressEvent(QMouseEvent * e)
{
    /*
    setWindowTitle(QString::number(e->x()) +
                   " " +
                   QString::number(e->y()));
                   */
    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);
        }
    }
    update();
}
 
 
 
//判断输赢:
bool Widget::isWin(int x, int y)
{
    return f1(x, y) || f2(x, y) || f3(x, y) || f4(x ,y);
}
 
 
 
//判断四个边界:
bool Widget::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 true;
    }
    return false;
}
 
bool Widget::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 true;
    }
    return false;
}
 
bool Widget::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 true;
    }
    return false;
}
 
bool Widget::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 true;
    }
    return false;
}

main.cpp

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_33360036/article/details/117149071

延伸 · 阅读

精彩推荐