forked from XMuli/ChineseChess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkGame.cpp
executable file
·138 lines (112 loc) · 3.73 KB
/
NetworkGame.cpp
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
/*
* Copyright (C) 2019~2020 偕臧 All rights reserved.
*
* Author: xmuli(偕臧) [email protected]
*
* github: https://github.com/xmuli
* blogs: https://xmuli.tech
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://touwoyimuli.github.io/>.
*/
#include "NetworkGame.h"
#include <QDebug>
NetworkGame::NetworkGame(bool isServer)
{
m_tcpServer = NULL;
m_tcpSocket = NULL;
if(isServer) //作为服务器端
{
m_bIsTcpServer = true;
m_tcpServer = new QTcpServer(this);
m_tcpServer->listen(QHostAddress::Any, 9999);
connect(m_tcpServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
}
else //作为客户端
{
m_bIsTcpServer = false;
m_tcpSocket = new QTcpSocket(this);
m_tcpSocket->connectToHost(QHostAddress("127.0.0.1"), 9999);
connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(slotRecv()));
}
}
NetworkGame::~NetworkGame()
{
}
//void NetworkGame::mousePressEvent(QMouseEvent *ev)
//{
// QPoint pt = ev->pos();
// //将pt转化成棋盘的像行列值
// //判断这个行列值上面有没有棋子
// int row, col;
// //点击棋盘外面就不做处理
// if(!isChecked(pt, row, col))
// return;
// //判断是哪一个棋子被选中,根据ID(这里的局部i)来记录下来
// int i;
// m_nCheckedID = -1;
// for(i = 0; i <= 31; i++)
// {
// if(m_ChessPieces[i].m_nRow == row && m_ChessPieces[i].m_nCol == col && m_ChessPieces[i].m_bDead == false)
// break;
// }
// if(0<=i && i<32)
// m_nCheckedID = i; //选中的棋子的ID
// clickPieces(m_nCheckedID, row, col);
// update();
//}
void NetworkGame::clickPieces(int checkedID, int &row, int &col)
{
//不能够替对方选棋和下棋
if(m_bIsTcpServer) //作为服务器一方 不能替黑棋下棋
{
//选棋[非下棋]这一步过程,使得其无法选择中黑棋
if(m_nSelectID == -1 && m_nCheckedID != -1 )
{
if(m_bIsTcpServer != m_ChessPieces[checkedID].m_bRed )
return ;
}
}
else //作为客户端一方 不能替红棋下棋
{
//选棋[非下棋]这一步过程,使得其无法选择中红棋
if(m_nSelectID == -1 && m_nCheckedID != -1)
{
if(m_bIsTcpServer != m_ChessPieces[checkedID].m_bRed )
return ;
}
}
whoWin();
ChessBoard::clickPieces(checkedID, row, col);
char arry[3];
arry[0] = checkedID;
arry[1] = row;
arry[2] = col;
m_tcpSocket->write(arry, 3);
}
void NetworkGame::slotNewConnection()
{
if(m_tcpSocket)
return;
m_tcpSocket = m_tcpServer->nextPendingConnection();
connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(slotRecv()));
}
void NetworkGame::slotRecv()
{
QByteArray arry = m_tcpSocket->readAll();
int nCheckedID = arry[0];
int nRow = arry[1];
int nCol = arry[2];
//qDebug()<<nCheckedID<<" "<<nRow<<" "<<nCol<<" ";
ChessBoard::clickPieces(nCheckedID, nRow, nCol);
}