diff --git a/NineChess/src/gamecontroller.cpp b/NineChess/src/gamecontroller.cpp index 868c3f99..1b5a43d7 100644 --- a/NineChess/src/gamecontroller.cpp +++ b/NineChess/src/gamecontroller.cpp @@ -37,6 +37,8 @@ #include "gamecontroller.h" #include "graphicsconst.h" #include "boarditem.h" +#include "server.h" +#include "client.h" GameController::GameController(GameScene & scene, QObject * parent) : QObject(parent), @@ -74,6 +76,10 @@ GameController::GameController(GameScene & scene, QObject * parent) : // 安装事件过滤器监视scene的各个事件, // 由于我重载了QGraphicsScene,相关事件在重载函数中已设定,不必安装监视器。 //scene.installEventFilter(this); + + // 网络 + server = new Server(); + client = new Client(); } GameController::~GameController() @@ -88,6 +94,10 @@ GameController::~GameController() ai1.wait(); ai2.wait(); + // 网络相关 + delete server; + delete client; + #ifdef BOOK_LEARNING NineChessAi_ab::recordOpeningBookHashMapToFile(); #endif /* BOOK_LEARNING */ @@ -790,7 +800,7 @@ bool GameController::giveUp() } // 关键槽函数,棋谱的命令行执行,与actionPiece独立 -bool GameController::command(const QString &cmd, bool update /*= true*/) +bool GameController::command(const QString &cmd, bool update /* = true */) { Q_UNUSED(hasSound) @@ -1062,3 +1072,9 @@ bool GameController::updateScence(NineChess &chess) return true; } + +void GameController::showNetworkWindow() +{ + server->show(); + client->show(); +} diff --git a/NineChess/src/gamecontroller.h b/NineChess/src/gamecontroller.h index 629a82ce..c797d893 100644 --- a/NineChess/src/gamecontroller.h +++ b/NineChess/src/gamecontroller.h @@ -41,6 +41,8 @@ #include "gamescene.h" #include "pieceitem.h" #include "aithread.h" +#include "server.h" +#include "client.h" class GameController : public QObject { @@ -162,6 +164,9 @@ public slots: bool updateScence(); bool updateScence(NineChess &chess); + // 显示网络配置窗口 + void showNetworkWindow(); + protected: //bool eventFilter(QObject * watched, QEvent * event); // 定时器 @@ -242,6 +247,10 @@ private: // 棋谱字符串列表模型 QStringListModel manualListModel; + + // 网络 + Server *server; + Client *client; }; #endif // GAMECONTROLLER_H diff --git a/NineChess/src/ninechesswindow.cpp b/NineChess/src/ninechesswindow.cpp index 2cebe415..01fcacc0 100644 --- a/NineChess/src/ninechesswindow.cpp +++ b/NineChess/src/ninechesswindow.cpp @@ -118,9 +118,6 @@ NineChessWindow::~NineChessWindow() } qDeleteAll(ruleActionList); - - delete server; - delete client; } void NineChessWindow::closeEvent(QCloseEvent *event) @@ -293,10 +290,6 @@ void NineChessWindow::initialize() QWidget::setWindowFlags(Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint); #endif // SHOW_MAXIMIZED_ON_LOAD - - // 网络 - server = new Server(); - client = new Client(); } void NineChessWindow::ruleInfo() @@ -760,9 +753,7 @@ void NineChessWindow::on_actionInternet_I_triggered() ui.actionLocal_L->setChecked(false); ui.actionInternet_I->setChecked(true); - - server->show(); - client->show(); + game->showNetworkWindow(); } void NineChessWindow::on_actionEngine_E_triggered() diff --git a/NineChess/src/ninechesswindow.h b/NineChess/src/ninechesswindow.h index dfaa0737..87350507 100644 --- a/NineChess/src/ninechesswindow.h +++ b/NineChess/src/ninechesswindow.h @@ -115,10 +115,6 @@ private: // 定时器 QTimer autoRunTimer; - - // 网络 - Server *server; - Client *client; }; #endif // NINECHESSWINDOW_H diff --git a/NineChess/src/server.cpp b/NineChess/src/server.cpp index 483d32b1..71df4563 100644 --- a/NineChess/src/server.cpp +++ b/NineChess/src/server.cpp @@ -144,6 +144,10 @@ void Server::sessionOpened() .arg(ipAddress).arg(tcpServer->serverPort())); } +void Server::setAction(QString action) +{ + this->action = action; +} void Server::sendAction() { @@ -151,7 +155,7 @@ void Server::sendAction() QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_10); - out << actions[QRandomGenerator::global()->bounded(actions.size())]; + out << action; QTcpSocket *clientConnection = tcpServer->nextPendingConnection(); diff --git a/NineChess/src/server.h b/NineChess/src/server.h index 06e362ea..2b764b76 100644 --- a/NineChess/src/server.h +++ b/NineChess/src/server.h @@ -40,12 +40,14 @@ public: private slots: void sessionOpened(); + void setAction(QString action); void sendAction(); private: QLabel *statusLabel = nullptr; QTcpServer *tcpServer = nullptr; QVector actions; + QString action; QNetworkSession *networkSession = nullptr; };