From d73d7b71f5d1d0834c08d89525964021d7ba69f5 Mon Sep 17 00:00:00 2001 From: alphagocc Date: Sat, 4 May 2019 11:37:44 +0800 Subject: [PATCH] =?UTF-8?q?NetworkGame=20OK=EF=BC=81=EF=BC=81=EF=BC=81?= =?UTF-8?q?=EF=BC=81=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FiveChess.cpp | 6 ++++- FiveChess.pro | 6 +++-- FiveChess.vcxproj | 26 ++++++++++++++----- FiveChess.vcxproj.filters | 14 +++++++--- battlegui.cpp | 38 +++++++++++---------------- chessBoardCore.cpp | 46 +++++++++++++++++++++------------ clientthread.cpp | 6 ++++- en_US.ts | 50 ++++++++++++++++++++++++++++-------- networksettingdialog.cpp | 38 ++++++++------------------- networksettingdialog.h | 10 ++++---- networksettings.cpp | 3 +++ networksettings.h | 5 ++++ serverthread.cpp | 4 ++- zh_CN.ts | 54 ++++++++++++++++++++++++++++++++------- 14 files changed, 198 insertions(+), 108 deletions(-) create mode 100644 networksettings.cpp create mode 100644 networksettings.h diff --git a/FiveChess.cpp b/FiveChess.cpp index d894291..2cef990 100644 --- a/FiveChess.cpp +++ b/FiveChess.cpp @@ -49,7 +49,10 @@ void FiveChess::loadFiveChessGame() void FiveChess::newNetworkFiveChessGame() { - QDialog* chooseDialog = new QDialog(this); + NetworkSettingDialog settingDialog; + settingDialog.exec(); + + QDialog* chooseDialog = new QDialog(this); Ui::ChooseCorSDialog* chooseDialogUi = new Ui::ChooseCorSDialog; chooseDialogUi->setupUi(chooseDialog); @@ -66,6 +69,7 @@ void FiveChess::newNetworkFiveChessGame() chooseDialog->close(); }); chooseDialog->exec(); + newFiveChessGame(); } diff --git a/FiveChess.pro b/FiveChess.pro index b40ae6e..d26994a 100644 --- a/FiveChess.pro +++ b/FiveChess.pro @@ -32,7 +32,8 @@ SOURCES += \ networksettingdialog.cpp \ serverthread.cpp \ clientthread.cpp \ - chessboardcore.cpp + chessboardcore.cpp \ + networksettings.cpp HEADERS += \ battlegui.h \ @@ -42,7 +43,8 @@ HEADERS += \ networksettingdialog.h \ serverthread.h \ clientthread.h \ - chessboardcore.h + chessboardcore.h \ + networksettings.h FORMS += \ battlegui.ui \ diff --git a/FiveChess.vcxproj b/FiveChess.vcxproj index 8aa1766..8e1fc5d 100644 --- a/FiveChess.vcxproj +++ b/FiveChess.vcxproj @@ -56,12 +56,12 @@ release\ - release\ + $(Platform)\$(Configuration)\ FiveChess true false debug\ - debug\ + $(Platform)\$(Configuration)\ FiveChess true @@ -74,12 +74,11 @@ None 4577;4467;%(DisableSpecificWarnings) Sync - release\ + $(IntDir) MaxSpeed _WINDOWS;UNICODE;_UNICODE;WIN32;_ENABLE_EXTENDED_ALIGNED_STORAGE;QT_DEPRECATED_WARNINGS;QT_NO_DEBUG;QT_WIDGETS_LIB;QT_GUI_LIB;QT_NETWORK_LIB;QT_CORE_LIB;NDEBUG;%(PreprocessorDefinitions) false - - + $(IntDir)vc$(PlatformToolsetVersion).pdb MultiThreadedDLL true true @@ -141,7 +140,7 @@ ProgramDatabase 4577;4467;%(DisableSpecificWarnings) Sync - debug\ + $(IntDir) Disabled _WINDOWS;UNICODE;_UNICODE;WIN32;_ENABLE_EXTENDED_ALIGNED_STORAGE;QT_DEPRECATED_WARNINGS;QT_WIDGETS_LIB;QT_GUI_LIB;QT_NETWORK_LIB;QT_CORE_LIB;%(PreprocessorDefinitions) false @@ -150,6 +149,7 @@ true Level3 true + $(IntDir)vc$(PlatformToolsetVersion).pdb $(QTDIR)\lib\Qt5Widgetsd.lib;$(QTDIR)\lib\Qt5Guid.lib;$(QTDIR)\lib\Qt5Networkd.lib;$(QTDIR)\lib\Qt5Cored.lib;$(QTDIR)\lib\qtmaind.lib;%(AdditionalDependencies) @@ -205,6 +205,7 @@ + @@ -218,7 +219,13 @@ - + + + .\GeneratedFiles\$(ConfigurationName);.\GeneratedFiles;.;$(QTDIR)\include;$(QTDIR)\include\QtWidgets;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtNetwork;$(QTDIR)\include\QtCore;.\debug;C:\VulkanSDK\1.0.51.0\include;$(QTDIR)\mkspecs\win32-msvc + _WINDOWS;UNICODE;_UNICODE;WIN32;_ENABLE_EXTENDED_ALIGNED_STORAGE;QT_DEPRECATED_WARNINGS;QT_WIDGETS_LIB;QT_GUI_LIB;QT_NETWORK_LIB;QT_CORE_LIB;%(PreprocessorDefinitions) + .\GeneratedFiles\$(ConfigurationName);.\GeneratedFiles;.;$(QTDIR)\include;$(QTDIR)\include\QtWidgets;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtNetwork;$(QTDIR)\include\QtCore;.\release;C:\VulkanSDK\1.0.51.0\include;$(QTDIR)\mkspecs\win32-msvc + _WINDOWS;UNICODE;_UNICODE;WIN32;_ENABLE_EXTENDED_ALIGNED_STORAGE;QT_DEPRECATED_WARNINGS;QT_NO_DEBUG;QT_WIDGETS_LIB;QT_GUI_LIB;QT_NETWORK_LIB;QT_CORE_LIB;NDEBUG;%(PreprocessorDefinitions) + @@ -294,4 +301,9 @@ + + + + + \ No newline at end of file diff --git a/FiveChess.vcxproj.filters b/FiveChess.vcxproj.filters index b9e88f1..5252b94 100644 --- a/FiveChess.vcxproj.filters +++ b/FiveChess.vcxproj.filters @@ -78,6 +78,9 @@ Source Files + + Source Files + Source Files @@ -101,7 +104,7 @@ Header Files - + Header Files @@ -110,6 +113,9 @@ Header Files + + Generated Files + @@ -152,13 +158,13 @@ Form Files - + Form Files - + Form Files - + Form Files diff --git a/battlegui.cpp b/battlegui.cpp index d325c0d..a39c4bd 100644 --- a/battlegui.cpp +++ b/battlegui.cpp @@ -2,39 +2,31 @@ #include "ui_battlegui.h" #include -battleGui::battleGui(QWidget *parent) : - QWidget(parent), - ui(new Ui::battleGui), - timer(new QTimer) +battleGui::battleGui(QWidget* parent) + : QWidget(parent), ui(new Ui::battleGui), timer(new QTimer) { - qDebug()<<"OK2"<setupUi(this); - ui->labelTime->setText(tr("Time:%1 Second").arg(0)); + ui->labelTime->setText(tr("Time:%1 Second").arg(0)); timer->start(1000); - connect(ui->pushButtonSave,&QPushButton::clicked,this,[]{ - chessBoard.saveBoard(); - }); - connect(timer,&QTimer::timeout,this,[&]{ + connect(ui->pushButtonSave, &QPushButton::clicked, this, + [] { chessBoard.saveBoard(); }); + connect(timer, &QTimer::timeout, this, [&] { chessBoard.addUsedTime(); ui->labelTime->setText(tr("Time:%1 Second").arg(chessBoard.getUsedTime())); }); - connect(&chessBoard, &ChessBoardCore::dataChanged, this, [&](int x, int y, ChessBoardCore::DataType d) { - ui->labelColor->setText(tr("Waiting:%1").arg(d == ChessBoardCore::DataType::black ? "White" : "Black")); - }); + connect(&chessBoard, &ChessBoardCore::dataChanged, this, + [&](int x, int y, ChessBoardCore::DataType d) { + ui->labelColor->setText(tr("Waiting:%1").arg(d == ChessBoardCore::DataType::black ? tr("White") : tr("Black"))); + }); } -battleGui::~battleGui() -{ - delete ui; -} +battleGui::~battleGui() { delete ui; } bool battleGui::close() { - QWidget *p = parentWidget(); - p->show(); - return true; + QWidget* p = parentWidget(); + p->show(); + return true; } - - - diff --git a/chessBoardCore.cpp b/chessBoardCore.cpp index fbb435d..c3f35f7 100644 --- a/chessBoardCore.cpp +++ b/chessBoardCore.cpp @@ -33,24 +33,28 @@ bool ChessBoardCore::searchWin(ChessBoardCore::DataType chess) FOR(i, 0, 11) FOR(j, 0, 15) if (m_data[i][j] == chess && m_data[i + 1][j] == chess && m_data[i + 2][j] == chess && - m_data[i + 3][j] == chess && m_data[i + 4][j] == chess) return true; + m_data[i + 3][j] == chess && m_data[i + 4][j] == chess) + return true; FOR(i, 0, 15) FOR(j, 0, 11) if (m_data[i][j] == chess && m_data[i][j + 1] == chess && m_data[i][j + 2] == chess && - m_data[i][j + 3] == chess && m_data[i][j + 4] == chess) return true; + m_data[i][j + 3] == chess && m_data[i][j + 4] == chess) + return true; FOR(i, 0, 11) FOR(j, 0, 11) if (m_data[i][j] == chess && m_data[i + 1][j + 1] == chess && m_data[i + 2][j + 2] == chess && m_data[i + 3][j + 3] == chess && - m_data[i + 4][j + 4] == chess) return true; + m_data[i + 4][j + 4] == chess) + return true; FOR(i, 0, 11) FOR(j, 4, 15) if (m_data[i][j] == chess && m_data[i + 1][j - 1] == chess && m_data[i + 2][j - 2] == chess && m_data[i + 3][j - 3] == chess && - m_data[i + 4][j - 4] == chess) return true; + m_data[i + 4][j - 4] == chess) + return true; return false; } @@ -68,15 +72,19 @@ bool ChessBoardCore::saveBoard() qDebug() << file.errorString() << endl; return false; } - file.write(QString("%1\n").arg(m_usedTime).toUtf8()); - file.write(QString("%1\n").arg(m_flag).toUtf8()); + QJsonObject jsonObj; + jsonObj.insert("usedTime", m_usedTime); + jsonObj.insert("flag", m_flag); + QString tempString; FOR(i, 0, 15) { - FOR(j, 0, 15) - { - file.write(QString("%1").arg(static_cast(m_data[i][j])).toUtf8()); - } + FOR(j, 0, 15) { tempString.append(static_cast(m_data[i][j]) + 48); } } + jsonObj.insert("chessBoard", tempString); + QJsonDocument jsonDoc; + jsonDoc.setObject(jsonObj); + qDebug() << jsonDoc.toJson(); + file.write(jsonDoc.toJson()); file.close(); } qDebug() << fileName << endl; @@ -96,17 +104,23 @@ bool ChessBoardCore::loadBoard() qDebug() << file.errorString() << endl; return false; } - m_usedTime = file.readLine().toInt(); - m_flag = file.readLine().toInt(); + QByteArray ba = file.readAll(); + // qDebug() << ba; + QJsonDocument jsonDoc; + jsonDoc = QJsonDocument::fromJson(ba); + QJsonObject jsonObj = jsonDoc.object(); + qDebug() << jsonDoc.toJson(); + m_usedTime = jsonObj["usedTime"].toInt(); + m_flag = jsonDoc["flag"].toInt(); qDebug() << m_usedTime; qDebug() << m_flag; - QString tempStr; - tempStr = file.readLine(); + QString tempStr = jsonObj["chessBoard"].toString(); FOR(i, 0, 15) { FOR(j, 0, 15) { - int tempData = tempStr[j].toLatin1(); + int tempData = tempStr[i * 15 + j].toLatin1(); + qDebug() << tempData; if (!std::isdigit(tempData)) { file.close(); @@ -116,7 +130,7 @@ bool ChessBoardCore::loadBoard() m_data[i][j] = static_cast(tempData - 48); } } - dataPrint(); + // dataPrint(); file.close(); return true; } diff --git a/clientthread.cpp b/clientthread.cpp index e883158..68ac868 100644 --- a/clientthread.cpp +++ b/clientthread.cpp @@ -1,5 +1,6 @@ #include "clientthread.h" #include "chessboardcore.h" +#include "networksettings.h" #include ClientThread* clientThread; @@ -10,7 +11,10 @@ void ClientThread::run() { m_tcpSocket = new QTcpSocket(); m_tcpSocket->abort(); - m_tcpSocket->connectToHost("127.0.0.1", 23333); + m_tcpSocket->connectToHost(networkSettings["host"].toString(), + networkSettings["port"].toInt()); + qDebug() << networkSettings["host"].toString() + << networkSettings["port"].toInt(); tcpSocketInit(); } diff --git a/en_US.ts b/en_US.ts index 7e8b9de..03de1be 100644 --- a/en_US.ts +++ b/en_US.ts @@ -4,22 +4,40 @@ ChessBoardCore - + Save Chess Board File - - + + ChessBoardFile(*.chessbrd) - + Open Chess Board File + + ChooseCorSDialog + + + Dialog + + + + + Server + + + + + Client + + + FiveChess @@ -78,8 +96,8 @@ - Now: - + Waiting: + @@ -92,15 +110,25 @@ - - + + Time:%1 Second - - Now:%1 - + + Waiting:%1 + + + + + White + + + + + Black + diff --git a/networksettingdialog.cpp b/networksettingdialog.cpp index c65bdbd..70b49d1 100644 --- a/networksettingdialog.cpp +++ b/networksettingdialog.cpp @@ -1,35 +1,17 @@ -/*#include "networksettingdialog.h" +#include "networksettingdialog.h" +#include "networksettings.h" #include "ui_networksettingdialog.h" -#include -#include +#include -networkSettingDialog::networkSettingDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::networkSettingDialog) +NetworkSettingDialog::NetworkSettingDialog(QWidget* parent) + : QDialog(parent), ui(new Ui::networkSettingDialog) { ui->setupUi(this); - connect(ui->buttonBox,&QDialogButtonBox::accepted,this,[&]() - { - QFile *file=new QFile(QString("conf.conf")); - if (!file->open(QFile::WriteOnly|QFile::Text)) - { - file->write(QByteArray(ui->lineEdit->text()+"\n"+ui->lineEdit_2->text())); - file->close(); - }else - { - QMessageBox::critical(this, - tr("Error!The Configuration File Can't Write!"), - tr("The Configuration File Can't Write!\n" - "Please check and try again."), - QMessageBox::Ok, - QMessageBox::Cancel); - } - parent->networkFiveChessGame(); + connect(ui->buttonBox, &QDialogButtonBox::accepted, this, [&] { + qDebug() << ui->lineEdit->text() << " " << ui->lineEdit_2->text().toInt(); + networkSettings["port"] = ui->lineEdit_2->text().toInt(); + networkSettings["host"] = ui->lineEdit->text(); }); } -networkSettingDialog::~networkSettingDialog() -{ - delete ui; -} -*/ +NetworkSettingDialog::~NetworkSettingDialog() { delete ui; } \ No newline at end of file diff --git a/networksettingdialog.h b/networksettingdialog.h index b3730e3..0bddde0 100644 --- a/networksettingdialog.h +++ b/networksettingdialog.h @@ -1,4 +1,4 @@ -/*#ifndef NETWORKSETTINGDIALOG_H +#ifndef NETWORKSETTINGDIALOG_H #define NETWORKSETTINGDIALOG_H #include @@ -7,17 +7,17 @@ namespace Ui { class networkSettingDialog; } -class networkSettingDialog : public QDialog +class NetworkSettingDialog : public QDialog { Q_OBJECT public: - explicit networkSettingDialog(QWidget *parent = 0); - ~networkSettingDialog(); + explicit NetworkSettingDialog(QWidget *parent = 0); + ~NetworkSettingDialog(); private: Ui::networkSettingDialog *ui; }; #endif // NETWORKSETTINGDIALOG_H -*/ + diff --git a/networksettings.cpp b/networksettings.cpp new file mode 100644 index 0000000..e0008e4 --- /dev/null +++ b/networksettings.cpp @@ -0,0 +1,3 @@ +#include "networksettings.h" + +QMap networkSettings; \ No newline at end of file diff --git a/networksettings.h b/networksettings.h new file mode 100644 index 0000000..7ce2e98 --- /dev/null +++ b/networksettings.h @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +extern QMap networkSettings; \ No newline at end of file diff --git a/serverthread.cpp b/serverthread.cpp index 3dda19f..cd5fbef 100644 --- a/serverthread.cpp +++ b/serverthread.cpp @@ -1,5 +1,6 @@ #include "serverthread.h" #include "chessboardcore.h" +#include "networksettings.h" #include ServerThread* serverThread; @@ -10,7 +11,8 @@ void ServerThread::run() { m_tcpServer = new QTcpServer(); connect(m_tcpServer, &QTcpServer::newConnection, this, &ServerThread::newConnect); - m_tcpServer->listen(QHostAddress::Any, 23333); + m_tcpServer->listen(QHostAddress::Any, networkSettings["port"].toInt()); + qDebug() << networkSettings["port"].toInt(); } void ServerThread::newConnect() diff --git a/zh_CN.ts b/zh_CN.ts index 70f6c8e..ab01fb0 100644 --- a/zh_CN.ts +++ b/zh_CN.ts @@ -4,22 +4,40 @@ ChessBoardCore - + Save Chess Board File 保存棋盘文件 - - + + ChessBoardFile(*.chessbrd) 棋盘文件(*.chessbrd) - + Open Chess Board File 打开棋盘文件 + + ChooseCorSDialog + + + Dialog + + + + + Server + + + + + Client + + + FiveChess @@ -78,8 +96,12 @@ + Waiting: + + + Now: - 当前执棋: + 当前执棋: @@ -96,15 +118,29 @@ 时间:%1 秒 - - + + Time:%1 Second 时间:%1 秒 - + + Waiting:%1 + + + + + White + + + + + Black + + + Now:%1 - 当前执棋:%1 + 当前执棋:%1