-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.h
221 lines (159 loc) · 4.72 KB
/
mainwindow.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QJsonDocument>
#include <QLabel>
#include <QList>
#include <QMainWindow>
#include <QPushButton>
#include <QTime>
#include <QtNetwork>
// #include <QtTest>
#include "cardlabel.h"
#include "cards.h"
class PlayerA : public QMainWindow {
Q_OBJECT
public:
PlayerA(QWidget *parent = nullptr);
~PlayerA();
void restart();
void initGiveUpInfoLabels();
void initDizhuNongminLabels();
void initBuhefaLabel();
template <typename Data>
void boardCast(Data data) {
if (!socketAB_ || socketAC_) qDebug() << "Connection lost!";
socketAB_->write(data);
socketAC_->write(data);
}
template <typename Name, typename Value>
void boardCast(Name str, Value v) {
QJsonObject jsonObject;
jsonObject.insert(str, v);
QJsonDocument jsonDocument;
jsonDocument.setObject(jsonObject);
QByteArray dataArray = jsonDocument.toJson();
socketAB_->write(dataArray);
socketAC_->write(dataArray);
}
template <typename Name, typename Value>
void castToB(Name str, Value v) {
QJsonObject jsonObject;
jsonObject.insert(str, v);
QJsonDocument jsonDocument;
jsonDocument.setObject(jsonObject);
QByteArray dataArray = jsonDocument.toJson();
socketAB_->write(dataArray);
}
template <typename QBA, typename Str>
QString readFromBuffer(QBA buffer, Str key) {
qDebug() << "buffer:" << buffer;
// 转化为Json
QJsonDocument document = QJsonDocument::fromJson(buffer);
qDebug() << "document:" << document;
QJsonObject rootObj = document.object();
qDebug() << "rootObj:" << rootObj;
return rootObj.value(key).toString();
}
template <typename Name, typename Value>
void castToC(Name str, Value v) {
QJsonObject jsonObject;
jsonObject.insert(str, v);
QJsonDocument jsonDocument;
jsonDocument.setObject(jsonObject);
QByteArray dataArray = jsonDocument.toJson();
socketAC_->write(dataArray);
}
void firstCampaign(int n);
void ifWantToCampaign(int, bool); // send to b and c
void startPlaying();
void showChuOrBuchuBtns();
void AHasJustPushedCards(QList<int>);
void giveUp(int);
void displayGiveUpInfo(int n);
void showWinOrLoseInfo();
bool checkIfGameOver();
private:
// 图形界面
QPushButton *buttonStartListening;
// 连接
QTcpServer *serverA_ = nullptr;
QTcpSocket *socketAB_ = nullptr;
QTcpSocket *socketAC_ = nullptr;
const int portA_ = 10080;
int personIndexToPosition_[3] = {0, 1, 2};
QList<int> cardsOfA_;
QList<int> commonCards_;
QList<int> cardsOnTable_;
QList<CardLabel *> cardLabels_;
QList<CardLabel *> tableCardLabels_;
QList<CardLabel *> commonCardLabels_;
QList<QLabel *> jiaoORbujiaoLabels_;
QList<QLabel *> giveupInfoLabels_;
QList<QLabel *> dizhuNongminLabels_;
QLabel *buhefaLabel_;
// 出牌顺序是...->A->B->C->A->...
// 地主为A:0,B:1,C:2
int currentLandlord_;
int currentCallNumber_ = 0;
void sleep(int t) {
QTime time;
time.start();
while (time.elapsed() < t) //等待时间流逝50ms
QCoreApplication::processEvents(); //不停地处理事件,让程序保持响应
}
QList<int> stringToIntArray(const QString &str) {
QList<int> l;
for (QString str : str.split(".")) {
if (!str.isEmpty()) {
l.append(str.toInt());
}
}
return l;
}
QString intArrayToString(const QList<int> &list) {
QString str;
for (int i = 0; i < list.size(); ++i) {
str.push_back(QString::number(list[i]));
str.push_back('.');
}
return str;
}
void displayCards();
void displayCommonCards(const QList<int> &);
void showTableOnSelfScreen(const QList<int> &);
void showRestartOrExitBtnsOnSelfScreen();
int lastPushCardPerson_ = 0;
bool landlordWins_;
QList<int> cardsNum_;
QList<QLabel *> cardsNumLabel_;
void updateCardNumber(int n, int cardsSize) {
cardsNum_[n] -= cardsSize;
cardsNumLabel_[personIndexToPosition_[n]]->setText(
QString::number(cardsNum_[n]));
}
bool BWantsToRestart_ = false;
bool CWantsToRestart_ = false;
bool gameStarted_ = false;
// Components used when gameover
QLabel *winOrLoseLabel_;
QPushButton *restartBtn_;
QPushButton *exitBtn_;
public slots:
void startListening();
void handleConnections();
void socketReadDataFromB();
void socketReadDataFromC();
void socketDisconnectedFromB();
void socketDisconnectedFromC();
void startCampaign();
void askIfCallLandlord(int n);
void showOthersIfCampaignInfo(int, bool);
void afterAllHaveCalled();
// void pushCards();
signals:
// 所有人已经叫过地主了
void allHaveCalled(); // send to b and c
void nextOneToCampaign();
void BCBothWantToRestart();
};
#endif // MAINWINDOW_H