-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
192 lines (177 loc) · 6.24 KB
/
Copy pathmainwindow.cpp
File metadata and controls
192 lines (177 loc) · 6.24 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "const.h"
#include "logic.h"
#include <QErrorMessage>
#include <QByteArray>
#include <QInputDialog>
#include <QtNetwork>
#include <QKeyEvent>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
logic = new Logic(this);
timer = new QTimer(this);
mapper = new QSignalMapper(this);
for (int i = 0;i < 10;i++){
for (int j = 0;j < 10;j++){
grid[i][j] = new QPushButton(ui->frame);
grid[i][j]->setGeometry(j*SIZE, i*SIZE, SIZE, SIZE);
grid[i][j]->setIconSize(QSize(SIZE, SIZE));
grid[i][j] -> show();
connect(grid[i][j], SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(grid[i][j], i*10+j);
}
}
connect(mapper, SIGNAL(mapped(int)), logic, SLOT(numberPressed(int)));
connect(ui->actionNewGame, SIGNAL(triggered()), this, SLOT(newGame()));
connect(ui->actionConnectToGame, SIGNAL(triggered()), this, SLOT(connectToGame()));
connect(ui->btnSend, SIGNAL(clicked()), this, SLOT(sendMessage()));
connect(ui->actionExit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(ui->actionTest, SIGNAL(triggered()), logic, SLOT(test()));
connect(ui->btnDrawGame, SIGNAL(clicked()), logic, SLOT(drawGame()));
connect(ui->btnSurrender, SIGNAL(clicked()), logic, SLOT(surrender()));
connect(ui->btnPause, SIGNAL(clicked()), this, SLOT(pause()));
ui->lineEdit->installEventFilter(this);
logic -> updateFrame();
sound = new QSound("C:\\Users\\david\\Desktop\\programs\\Qt\\checkGame\\1.wav");
sound->play();
playing = 1;
networkStatus = None;
}
void MainWindow::pause(){
if (playing == 0){
sound->play();
}else{
sound->stop();
}
playing ^= 1;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newGame(){
if (networkStatus != Server){
QInputDialog dialog(this);
dialog.setInputMode(QInputDialog::TextInput);
dialog.setTextValue("8888");
dialog.setLabelText("Please Select The Port You Want!");
dialog.show();
if (dialog.exec() != QInputDialog::Accepted){
return;
}
QString str = dialog.textValue();
networkStatus = Server;
server = new QTcpServer();
server->listen(QHostAddress::Any, str.toInt());
int cur = server->isListening();
if (cur){
QString localHostName = QHostInfo::localHostName();
QHostInfo info = QHostInfo::fromName(localHostName);
QString address;
foreach(QHostAddress add, info.addresses()){
if (add.protocol() == QAbstractSocket::IPv4Protocol)
address = add.toString();
}
showErrorMessage("Successfully Build: Your Address :\n" + address + ":" + str, this);
QObject::connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
return;
}
}
showMessageBox("Unsuccessfuly Connected!", this);
}
void MainWindow::newConnection(){
qDebug("Connect!!1");
if (networkStatus == Server){
socket = server->nextPendingConnection();
networkStatus = ConnectedServer;
connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnect()));
logic -> startGame(-1);
showMessageBox("Connected!, Game Start!", this);
}
}
int MainWindow::processPackage(QString &info)
{
QString start = info.mid(0, 4);
QString mid = info.mid(4, 1);
QString last = info.mid(5);
int num = start.toInt();
if (mid != QString(":")){
num = -1;
}
info = last;
ui->chatBrowser->append("Recieve: "+start+mid+info);
return num;
}
int MainWindow::isConnected(){
return networkStatus == ConnectedClient || networkStatus == ConnectedServer;
}
void MainWindow::readSocket(){
QString info;
info += socket->readAll();
MessageType num = (MessageType)processPackage(info);
if (num == ChatInfo){
ui->chatBrowser->append("Ohter: " + info);
}else{
if (num == -1) return;
logic -> recieve(num, info);
}
}
void MainWindow::sendMessage(){
if (networkStatus == ConnectedClient || networkStatus == ConnectedServer){
QByteArray *array = new QByteArray;
array->clear();
array->append(QString::number(ChatInfo)+":"+ui->lineEdit->text());
socket->write(array->data());
ui->chatBrowser->append("I: "+ui->lineEdit->text());
ui->lineEdit->clear();
}else{
showMessageBox("You have to Connect To The Server or Build a Game Wait for connect!", this);
ui->lineEdit->clear();
}
}
void MainWindow::connectToGame(){
qDebug("Connect To Game");
socket = new QTcpSocket;
QInputDialog dialog;
dialog.setLabelText("Please Input the String Of your friend's server message");
dialog.setInputMode(QInputDialog::TextInput);
dialog.show();
networkStatus = Client;
if (dialog.exec()==QInputDialog::Accepted){
QString cur = dialog.textValue();
QStringList list = cur.split(':');
socket->connectToHost(QHostAddress(list.at(0)), list.at(1).toInt());
int status = socket->waitForConnected();
if (status){
connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
showMessageBox("Successfully Connected: ", this);
networkStatus = ConnectedClient;
logic -> startGame(1);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnect()));
}else{
networkStatus = None;
showMessageBox("Unsuccessfully Connect, Please Check the IP you input or Build a game", this);
}
}
}
bool MainWindow::eventFilter(QObject *object, QEvent *event){
if (object == ui->lineEdit && event -> type() == QEvent::KeyPress){
QKeyEvent *key = (QKeyEvent *)event;
if (key -> key() == Qt::Key_Return || key->key() == Qt::Key_Enter){
sendMessage();
return true;
}
return false;
}
return false;
}
void MainWindow::disconnect(){
showMessageBox("Disconnect! Restart the game Please", this);
networkStatus = None;
}