-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprotocol.cpp
More file actions
67 lines (57 loc) · 1.42 KB
/
protocol.cpp
File metadata and controls
67 lines (57 loc) · 1.42 KB
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
#include <cassert>
#include <iostream>
#include "include/protocol.h"
Protocol::Protocol(GameBoard *gameBoard, Label *statusLabel)
: gameBoard(gameBoard), statusLabel(statusLabel)
{
}
void Protocol::parsePacket(std::string msg) {
assert(msg.size());
switch (msg[0]) {
case 'T': return parseBoard(msg.substr(1));
case 'B': return parseBigGame(msg.substr(1));
case 'C': return parseControl(msg.substr(1));
case 'E': return parseError(msg.substr(1));
default:
std::cerr << "Pacote de tipo desconhecido: " << msg[0] << std::endl;
return;
}
}
void Protocol::parseBoard(std::string msg) {
board = msg;
if (bigGame.size()) {
gameBoard->setBoard(Board(board, bigGame));
}
}
void Protocol::parseBigGame(std::string msg) {
bigGame = msg;
if (board.size()) {
gameBoard->setBoard(Board(board, bigGame));
}
}
void Protocol::parseControl(std::string msg) {
int pos = msg[0] - '0';
int state = msg[1] - '0';
if (pos == 9) {
gameBoard->setSelectedBlock(-1, -1);
} else {
gameBoard->setSelectedBlock(pos / 3, pos % 3);
}
switch (state) {
case 0:
if (pos != 9) statusLabel->setText("Status: sua vez de jogar");
else statusLabel->setText("Status: vez do oponente");
break;
case 1:
statusLabel->setText("Voce perdeu :(");
break;
case 2:
statusLabel->setText("Voce ganhou!!!");
break;
case 3:
statusLabel->setText("Deu velha :/");
break;
}
}
void Protocol::parseError(std::string msg) {
}