Design and Implementation of Network wuziqi game QT class (5)
Although the UI of the fourth version of wuziqi game is much more beautiful, it can only be played by itself or by two people. This article has always been advertised as "network", but still does not see its shadow.
Please wait. Let me draw a Tao Fu:
1. The original program is divided into two parts. The qtcpsever and qtcpsocket classes are added to implement network functions. TCP is used to synchronously update the server-side checker data and the client's checker data.
2. The server is a black player. First, open a port and listen to it, which indicates that the white player client sends a connection request. After the connection is established, the black player goes down first.
3. After either party clicks the mouse, the board data and current status should be reported to the other party in a timely manner.
4. If the network of either party is interrupted, the other party shall be able to give a prompt to choose to reconnect or quit.
5. The following is the header file of the main modified class implementation:
------- Heiqi (server side )-------
ChEssBase. h:
Class ChEssBase: Public qdialog
{
Q_object
Public:
ChEssBase (INT, char **);
Public slots:
Void slotturn (bool );
Void slotvictory (bool );
Void slotrestart ();
Void slotstart ();
Void slottransfordata (INT, Int, INT); // data sending slot
Void slotupdateserverdata (); // Update Server Data
Void slotacceptconnection (); // receives the connection
Void slotreporttcperror (q1_actsocket: socketerror); // report a TCP Error
Signals:
Void signalstart ();
Void signalrestart ();
Void signalclear ();
Void signalsetchess (INT, Int, INT); // chess Signal
PRIVATE:
Chessboard * Board;
Mychess * white;
Mychess * black;
Chessview * view;
Qlabel * statuslabel;
Qpushbutton * startbutton;
Qpushbutton * restartbutton;
Qpushbutton * quitbutton;
Qtcpserver tcpserver; // The server tcpsocket is used for listening.
Qtcpsocket * tcpserverconnection; // used to establish a connection with the client
Qint16 CX; // X coordinate of chess
Qint16 Cy;
// Y coordinate of chess
Qint16 status; // the current status.
Qint32 msgsize; // Message Size
, Not used currently
Qstring MSG; // message, not used currently
Qbytearray inblock; // receives data processing
Qbytearray outblock; // send data processing
};
------- BaiQi (Client Side )-------
ChEssBase. h:
Class ChEssBase: Public qdialog
{
Q_object
Public:
ChEssBase (INT, char **);
Public slots:
Void slotturn (bool );
Void slotvictory (bool );
Void slotrestart ();
Void slotstart ();
Void slotconnected (); // TCP connected slot
Void slottransfordata (INT, Int, INT );
// Data transmission slot
Void slotupdateclientdata ();
// Update client data
Void slotreporttcperror (q1_actsocket: socketerror );
// Report TCP errors
Signals:
Void signalstart ();
Void signalrestart ();
Void signalclear ();
Void signalsetchess (INT, Int, INT );
// Chess Signal
PRIVATE:
Chessboard * Board;
Mychess * white;
Mychess * black;
Chessview * view;
Qlabel * statuslabel;
Qpushbutton * startbutton;
Qpushbutton * restartbutton;
Qpushbutton * quitbutton;
Qtcpsocket tcpclient; // client tcpsocket
Qint16 CX; // X coordinate of chess
Qint16 Cy;
// Y coordinate of chess
Qint16 status; // the current status.
Qint32 msgsize; // Message Size
, Not used currently
Qstring MSG; // message, not used currently
Qbytearray inblock; // receives data processing
Qbytearray outblock; // send data processing
};
6. I started to give my WAF a half-covering face! The fifth version of the online version
Wuzi game: