-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetGame.cpp
106 lines (90 loc) · 1.99 KB
/
NetGame.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
#include "NetGame.h"
NetGame::NetGame(bool server, QWidget *parent) : Board(parent)
{
_server = NULL;
_socket = NULL;
_bServer = server;
if(_bServer)
{
_server = new QTcpServer(this);
_server->listen(QHostAddress::Any, 9899);
connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
}
else
{
_socket = new QTcpSocket(this);
_socket->connectToHost(QHostAddress("127.0.0.1"), 9899);
connect(_socket, SIGNAL(readyRead()), this, SLOT(slotDataArrive()));
}
}
void NetGame::slotNewConnection()
{
if(_socket) return;
_socket = _server->nextPendingConnection();
connect(_socket, SIGNAL(readyRead()), this, SLOT(slotDataArrive()));
/* 产生随机数来决定谁走红色 */
bool bRedSide = qrand()%2>0;
init(bRedSide);
/* 发送给对方 */
QByteArray buf;
buf.append(1);
buf.append(bRedSide?0:1);
_socket->write(buf);
}
void NetGame::back()
{
if(_bRedTurn != _bSide)
return;
backOne();
backOne();
QByteArray buf;
buf.append(3);
_socket->write(buf);
}
void NetGame::click(int id, int row, int col)
{
if(_bRedTurn != _bSide)
return;
Board::click(id, row, col);
/* 发送给对方 */
QByteArray buf;
buf.append(2);
buf.append(id);
buf.append(row);
buf.append(col);
_socket->write(buf);
}
void NetGame::backFromNetwork(QByteArray)
{
backOne();
backOne();
}
void NetGame::clickFromNetwork(QByteArray buf)
{
Board::click(buf[1], 9-buf[2], 8-buf[3]);
}
void NetGame::initFromNetwork(QByteArray buf)
{
bool bRedSide = buf.at(1)>0?true:false;
init(bRedSide);
}
void NetGame::slotDataArrive()
{
QByteArray buf = _socket->readAll();
switch (buf.at(0)) {
case 1:
initFromNetwork(buf);
break;
case 2:
clickFromNetwork(buf);
break;
case 3:
backFromNetwork(buf);
break;
default:
break;
}
}
NetGame::~NetGame()
{
}